forked from aryanhimanshu/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort
More file actions
36 lines (34 loc) · 736 Bytes
/
Copy pathbubble_sort
File metadata and controls
36 lines (34 loc) · 736 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
#include<stdio.h>
#define MAX 100
int main(void)
{
int arr[MAX],i,j,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]); //Asks user to enter the next element
}
for(i=0; i<n-1 ;i++)
{
xchanges = 0;
for(j=0; j<n-1-i; j++)
{
if(arr[j] > arr[j+1]) //If array element j is larger than j+1
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp; //Swaps array element j with the next element
xchanges++;
}
}
if(xchanges==0) /*If list is sorted*/
break;
}
printf("Sorted list is :\n"); //Displays the sorted list
for(i=0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
return 0;
}