-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8960 - Optimized Bubble Sort.c
67 lines (55 loc) · 1.66 KB
/
8960 - Optimized Bubble Sort.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
57
58
59
60
61
62
63
64
65
66
67
/**********************************************************************************************************
NAME: CANDIDA RUTH NORONHA
CLASS: SE COMPS B
ROLL NO. : 8960
BATCH: C
TITLE: OPTIMIZED BUBBLE SORT
SUBMISSION DATE : 22nd February, 2021
**********************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
void modBubbleSort(int a[],int n)
{
int pass,j,t;
int exchange = 1;//boolean variable to denote exchange has happened or not in the previous pass
for(pass=1; pass<= n-1 && exchange==1 ; pass++)
{
exchange=0; //assuming exchange won't happen in this pass
for(j=0 ; j<n-pass ; j++)
{
if (a[j] > a[j+1]) //if consecutive elements not in order then swap
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
exchange=1;//exchange has occured
}
}
}
}
int main()
{
int arr[SIZE], i, n;
printf("\n Enter the number of elements in the array : ");
scanf("%d",&n);
printf("\n Enter the elements of the array : ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
modBubbleSort(arr,n);
printf("\n The sorted array is :");
for(i=0;i<n;i++)
{
printf("\t%d",arr[i]);
}
printf("\n");
return 0;
}
/**********************************************************************************************************
OUTPUT :
Enter the number of elements in the array : 5
Enter the elements of the array : 8 6 -3 1 2
The sorted array is : -3 1 2 6 8
**********************************************************************************************************/