-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble sort in c
41 lines (39 loc) · 919 Bytes
/
bubble sort in c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
int main() {
printf("Apoorv Saxena - 2301920100069\n");
printf("BUBBLE SORT\n");
printf("Enter the size of array : ");
int n;
scanf("%d",&n);
printf("\n");
int arr[100];
for(int i=0;i<n;i++){
printf("Enter the element: \t");
scanf("%d" ,&arr[i]);
}
//print array before sorting
printf("Before sorting \n");
for(int i=0;i<n;i++){
printf("%d",arr[i]);
printf("\t");
}
printf("\n");
//Bubble sort
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(arr[j]>arr[j+1]){
//swap
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
//print array after sorting
printf("After sorting \n");
for(int i=0;i<n;i++){
printf("%d",arr[i]);
printf("\t");
}
return 0;
}