-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Shourya1911/patch-6
Create bubble.c
- Loading branch information
Showing
1 changed file
with
44 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,44 @@ | ||
#include <stdio.h> | ||
int bubbleSort(int arr[], int n) { | ||
for (int i = 0; i < n - 1; i++) { | ||
for (int j = 0; j < n - i - 1; j++) { | ||
// Swap if the element found is greater than the next element | ||
if (arr[j] > arr[j + 1]) { | ||
int temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Function to print the array | ||
int printArray(int arr[], int n) { | ||
for (int i = 0; i < n; i++) { | ||
printf("%d ", arr[i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
int main() { | ||
int arr[100], n; | ||
|
||
// Input number of elements | ||
printf("Enter the number of elements: "); | ||
scanf("%d", &n); | ||
|
||
// Input the array elements from user | ||
printf("Enter the elements:\n"); | ||
for (int i = 0; i < n; i++) { | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
// Sorting the array using Bubble Sort | ||
bubbleSort(arr, n); | ||
|
||
// Print the sorted array | ||
printf("Sorted array: "); | ||
printArray(arr, n); | ||
|
||
return 0; | ||
} |