Skip to content

Commit

Permalink
Merge pull request #52 from Shourya1911/patch-6
Browse files Browse the repository at this point in the history
Create bubble.c
  • Loading branch information
Amit-S-Sahu authored Oct 26, 2024
2 parents 175e253 + f1f0064 commit e776a47
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions shouryasinha/bubble.c
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;
}

0 comments on commit e776a47

Please sign in to comment.