-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathSort Bubble.c
More file actions
48 lines (41 loc) · 751 Bytes
/
Sort Bubble.c
File metadata and controls
48 lines (41 loc) · 751 Bytes
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
42
43
44
45
46
47
48
/*Program of sorting using bubble sort*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main()
{
int arr[MAX],i,j,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
if(n > MAX)
{
printf("number of elements must not exceed %d\n", MAX);
exit(1);
}
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
/*Bubble sort*/
for(i=0; i<n-1; i++)
{
xchanges=0;
for(j=0; j<n-1-i; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
xchanges++;
}
}
if(xchanges==0) /*If list is sorted*/
break;
}
printf("Sorted list is :\n");
for(i=0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
}/*End of main()*/