Skip to content

Instantly share code, notes, and snippets.

@rajan-31
Last active January 5, 2023 08:58
Show Gist options
  • Save rajan-31/add1072848aac3ce2224fa15638bd39e to your computer and use it in GitHub Desktop.
Save rajan-31/add1072848aac3ce2224fa15638bd39e to your computer and use it in GitHub Desktop.
Sorting Algorithms
#include <stdio.h>
void printA(int * A, int LB, int UB) {
for (int i = LB; i <= UB; i++)
printf("%d\n", A[i]);
}
void swap(int * x, int * y) {
int t = * x;
* x = * y;
* y = t;
}
void bubble_sort(int * A, int LB, int UB) {
int k = 0;
for (int i = LB; i < UB; i++) {
k++;
int flag = 0;
for (int j = LB; j <= UB - k; j++) {
if (A[j] > A[j + 1]) {
swap( & A[j], & A[j + 1]);
flag = 1;
}
}
if (flag == 0) return;
}
}
int main() {
int A[] = { 5, 4, 3, 6, 2, 1};
int LB = 0, UB = 5;
bubble_sort(A, LB, UB);
printA(A, LB, UB);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment