-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble Sort
57 lines (47 loc) · 1.26 KB
/
Bubble Sort
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
#include<stdio.h>
#include<stdlib.h>
int array[500];
int comparisons=0;
int exchanges=0;
void print (int *array, int size)
{
int i=0;
printf( "Ending array ");
for(i=0; i < size; i++) {
printf("%d ",array[i] );
}
printf("\n");
}
void bubblesort(int *array, int size) {
int i;
int j;
int temp;
for(i=size-2; i>=0; i--)
for(j=0; j<=i; j++)
if (array[j] > array[j+1])
{
comparisons++;
temp = array[j];
exchanges++;
array[j] = array[j+1];
exchanges++;
array[j+1] = temp;
exchanges++;
}
}
void main()
{
int size;
printf("Insertion Sort: \n");
printf("Enter the number of elements you want in: " );
scanf("%d",&size);
printf("Enter the elements: ");
for(int i=0;i<size;i++)
{
scanf("%d",&array[i]);
}
insertionsort(array,size);
print(array,size);
printf("Comparisons:%d \n" ,comparisons ) ;
printf("Exchanges:%d \n", exchanges );
}