Skip to content

Commit f31e937

Browse files
committed
added unstable bubble sorts in c
1 parent de0effc commit f31e937

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

C/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* BUBBLE SORT
2222
* [ASCENDING AND STABLE](Sorting/BUBBLE-SORT/bubblesort.c)
2323
* [DESCENDING AND STABLE](Sorting/BUBBLE-SORT/bubble.c)
24+
* [ASCENDING AND UNSTABLE](Sorting/BUBBLE-SORT/ascendunbubble.c)
25+
* [DESCENDING AND UNSTABLE](Sorting/BUBBLE-SORT/descendunbubble.c)
2426

2527
### EXECUTION OF C CODE
2628

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//BUBBLE SORT
2+
//ASCENDING ORDER
3+
//UNSTABLE SORT
4+
#include <stdio.h>
5+
void bubblesort(int arr[],int n)
6+
{
7+
for(register int i=n-1;i>=0;i--)
8+
{
9+
for(register int j=0;j<i;j++)
10+
{
11+
if(arr[j]>=arr[j+1])
12+
{
13+
//swap
14+
int temp = arr[j+1];
15+
arr[j+1] = arr[j];
16+
arr[j] = temp;
17+
}
18+
}
19+
}
20+
}
21+
int main()
22+
{
23+
printf("Enter the size of the array\n");
24+
int n;
25+
scanf("%d",&n);
26+
int arr[n];
27+
printf("Enter the elements of the array\n");
28+
for(register int i=0;i<n;i++)
29+
{
30+
scanf("%d",&arr[i]);
31+
}
32+
bubblesort(arr,n);
33+
printf("The sorted array:\n");
34+
for(register int i=0;i<n;i++)
35+
{
36+
printf("%d ",arr[i]);
37+
}
38+
printf("\n");
39+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//BUBBLE SORT
2+
//DESCENDING ORDER
3+
//UNSTABLE SORT
4+
#include <stdio.h>
5+
void bubblesort(int arr[],int n)
6+
{
7+
for(register int i=n-1;i>=0;i--)
8+
{
9+
for(register int j=0;j<i;j++)
10+
{
11+
if(arr[j]<=arr[j+1])
12+
{
13+
//swap
14+
int temp = arr[j+1];
15+
arr[j+1] = arr[j];
16+
arr[j] = temp;
17+
}
18+
}
19+
}
20+
}
21+
int main()
22+
{
23+
printf("Enter the size of the array\n");
24+
int n;
25+
scanf("%d",&n);
26+
int arr[n];
27+
printf("Enter the elements of the array\n");
28+
for(register int i=0;i<n;i++)
29+
{
30+
scanf("%d",&arr[i]);
31+
}
32+
bubblesort(arr,n);
33+
printf("The sorted array:\n");
34+
for(register int i=0;i<n;i++)
35+
{
36+
printf("%d ",arr[i]);
37+
}
38+
printf("\n");
39+
}

0 commit comments

Comments
 (0)