Skip to content

Instantly share code, notes, and snippets.

@eric-vader
Last active October 22, 2024 14:03
Show Gist options
  • Save eric-vader/6bfda71ad233e262d7e076cc3cdcf5d0 to your computer and use it in GitHub Desktop.
Save eric-vader/6bfda71ad233e262d7e076cc3cdcf5d0 to your computer and use it in GitHub Desktop.
problem22.1.c
// Online C compiler to run C program online
#include <stdio.h>
#include <stdbool.h>
void swap(long a[], size_t i, size_t j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
bool bubble_pass(size_t last, long a[]) {
bool has_swapped = false;
for (size_t i = 0; i < last; i += 1) {
if (a[i] > a[i+1]) {
swap(a, i, i+1);
has_swapped = true;
}
}
return has_swapped;
}
void bubble_sort(size_t n, long a[n]) {
bool has_swapped = true;
for (size_t last = n - 1; last > 0 && has_swapped; last -= 1) {
has_swapped = bubble_pass(last, a);
}
}
int main() {
long list[] = {25, 50, 75, 100};
bubble_sort(4,list);
for(int i = 0; i < 4; i++) {
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment