-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbubble_sort.c
51 lines (43 loc) · 1.09 KB
/
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
#include <stdio.h>
#include <cs50.h>
void swap(int *xp, int *yp);
void printArray(int arr[], int size);
void bubbleSort(int arr[], int n);
int main(){
int myArray[] = {1, 2, 8, 3, 9, 5, 4, 3, 23, 10, 0};
//int myArray[] = {1, 2, 3, 3, 4, 5};
int n = sizeof(myArray)/sizeof(myArray[0]); // length of the array
printf("Before: ");
printArray(myArray, n);
bubbleSort(myArray, n);
printf("After: ");
printArray(myArray, n);
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void swap(int *xp, int *yp){
int tmp = *xp;
*xp = *yp;
*yp = tmp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped = false;
for (i = 0; i < n-1; i++){
swapped = false;
// Last i elements are already in place
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}
if(swapped == false) break; // array is now sorted, break loop
}
}