-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbubble.c
39 lines (39 loc) · 778 Bytes
/
bubble.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
//BUBBLE SORT
//DESCENDING ORDER
//STABLE SORT
#include <stdio.h>
void bubblesort(int arr[],int n)
{
for(register int i=n-1;i>=0;i--)
{
for(register int j=0;j<i;j++)
{
if(arr[j]<arr[j+1])
{
//swap
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
printf("Enter the size of the array\n");
int n;
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array\n");
for(register int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
bubblesort(arr,n);
printf("The sorted array:\n");
for(register int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}