-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-sort.c
53 lines (45 loc) · 922 Bytes
/
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
#include <stdio.h>
#include <stdbool.h>
#define SIZE 5
int list[SIZE] = {0,1,2,3,4};
void sort()
{
int temp;
for(int i = 0; i < SIZE - 1; i++)
{
bool swapped = true;
for(int j = 0; j < SIZE - (i+1); j++)
{
if(list[j] > list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
swapped = false;
}
printf("%d times \n ", i);
}
if(swapped == true)
{
printf("Already Sorted \n");
break;
}
else
{
printf("NOt Already SOrted \n");
}
}
}
void display(){
printf("\n\n ====================== \n\n");
for(int i = 0; i < SIZE; i++)
{
printf("list[%d] = %d \n",i, list[i]);
}
}
int main(){
display();
sort();
display();
return 0;
}