-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.c
More file actions
54 lines (44 loc) · 857 Bytes
/
bubbleSort.c
File metadata and controls
54 lines (44 loc) · 857 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
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <time.h>
#define MAX_SIZE 10
void printArray(char* name, int arr[], int size) {
printf("%s[%d] : {", name, size);
for (int i=0; i<size; i++) {
printf("%d", arr[i]);
if (i+1!=size)
printf(", ");
}
printf("}\n");
}
void arraySetup(int arr[], int size) {
srand(time(NULL));
for (int i=0; i<size; i++) {
arr[i] = rand()%100;
}
}
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void bubbleSort(int arr[], int size) {
int i, j, flag;
for (i=size; i>0; i--) {
flag = 0;
for (j=0; j<i; j++) {
if (arr[j]>arr[j+1]) {
swap(&arr[j], &arr[j+1]);
flag = 1;
}
}
printArray("arr", arr, MAX_SIZE);
if (!flag)
break;
}
}
void main(int argc, char* argv[]) {
int arr[MAX_SIZE];
arraySetup(arr, MAX_SIZE);
printArray("arr", arr, MAX_SIZE);
bubbleSort(arr, MAX_SIZE);
}