-
Notifications
You must be signed in to change notification settings - Fork 1
/
bubble.c
56 lines (56 loc) · 1.38 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<stdlib.h>
void main() //bubble sort
{
int i,j,x,swap,choice;
printf("Enter 1 for Sorting in Ascending order\nEnter 2 for Sorting in Descending Order\n");
printf("Enter your choice: ");
scanf("%d",&choice);
printf("Enter the size of the array: ");
scanf("%d",&x);
int a[x];
printf("Enter the numbers to be sorted in the array below:-\n");
for(i=0;i<x;i++)
scanf("%d",&a[i]);
for(i=0;i<x-1;i++)
{
for(j=0;j<x-i-1;j++)
{
if(choice==1)
{
if(a[j] > a[j+1])
{
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;
}
}
else if(choice==2)
{
if(a[j] < a[j+1])
{
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;
}
}
else
{
printf("Invalid Input");
exit(1);
}
}
}
if(choice==1)
{
printf("The sorted array in ascending order: ");
for(i=0;i<x;i++)
printf("%d ",a[i]);
}
if(choice==2)
{
printf("The sorted array in descending order: ");
for(i=0;i<x;i++)
printf("%d ",a[i]);
}
}