Skip to content

Commit

Permalink
Create bubblesort.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan-Bhendarkar authored and x0lg0n committed Oct 29, 2024
1 parent 85d5341 commit 0f48885
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions C/bubblesort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>

void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array:\n");
printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array:\n");
printArray(arr, n);

return 0;
}

0 comments on commit 0f48885

Please sign in to comment.