-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85d5341
commit 0f48885
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
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
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; | ||
} |