Last active
March 23, 2023 15:15
-
-
Save hackrio1/20bd70ac3f4b3659baa1175b4cbd29f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void bubble_sort(int a[], int n) { | |
int i = 0, j = 0, tmp; | |
for (i = 0; i < n; i++) { // loop n times - 1 per element | |
for (j = 0; j < n - i - 1; j++) { // last i elements are sorted already | |
if (a[j] > a[j + 1]) { // swop if order is broken | |
tmp = a[j]; | |
a[j] = a[j + 1]; | |
a[j + 1] = tmp; | |
} | |
} | |
} | |
} | |
int main() { | |
int a[100], n, i, d, swap; | |
printf("Enter number of elements in the array:\n"); | |
scanf("%d", &n); | |
printf("Enter %d integers\n", n); | |
for (i = 0; i < n; i++) | |
scanf("%d", &a[i]); | |
bubble_sort(a, n); | |
printf("Printing the sorted array:\n"); | |
for (i = 0; i < n; i++) | |
printf("%d\n", a[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment