-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort_integers.c
More file actions
44 lines (36 loc) · 921 Bytes
/
BubbleSort_integers.c
File metadata and controls
44 lines (36 loc) · 921 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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,temp,a[100],swap_count=0;
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
printf("\n\nThe bubble sorted array looks like : \n\n");
for(i=0; i<n-1; i++) //pass
{
for(j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1]) // comparison...NOTE : for descending order just replace '>' by '<'
{
// interchange
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
swap_count++;
}
}
printf("\n\nAfter Pass %d : ",i);
for(int k=0;k<n;k++)
{
printf("%d\t",a[k]);
}
}
printf("\n\nTotal number of swaps undergone is equal to %d\n",swap_count);
printf("\n");
return 0;
}