Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3ba0403

Browse files
Merge pull request fnplus#494 from Rex1911/bubble-sort-c
Added Bubble Sort in C
2 parents 04674af + 83171d7 commit 3ba0403

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<stdio.h>
2+
3+
int main() {
4+
int n, arr[100];
5+
printf("Enter no of elements: ");
6+
scanf("%d", &n);
7+
printf("Enter %d elements now: ", n);
8+
for(int i = 0; i < n; i++) {
9+
scanf("%d", &arr[i]);
10+
}
11+
12+
printf("Before the sort: \n");
13+
for(int i = 0; i < n; i++) {
14+
printf("%d ", arr[i]);
15+
}
16+
17+
//Bubble sort algo
18+
for(int i = 0; i < n-1; i++) {
19+
for(int j = 0; j < n-i-1; j++) {
20+
if(arr[j] > arr[j+1]) {
21+
//Swapping the elements
22+
int temp = arr[j];
23+
arr[j] = arr[j+1];
24+
arr[j+1] = temp;
25+
}
26+
}
27+
}
28+
29+
printf("\nAfter the sort: \n");
30+
for(int i = 0; i < n; i++) {
31+
printf("%d ", arr[i]);
32+
}
33+
printf("\n");
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /