-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.c
89 lines (72 loc) · 2.45 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stddef.h>
void xor_swap(int *x, int *y) {
if (*x == *y)
return;
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
void bubble_sort(int *arr, size_t size) {
int swapped = 1;
while (swapped) {
/* After each iteration, the largest element in the unsorted portion of the array
* is guaranteed to be in its correct position at the end of the array.
* Therefore, we can safely reduce the size of the unsorted portion by 1. */
size--;
swapped = 0;
for (size_t i = 0; i < size; i++) {
if (arr[i] > arr[i + 1]) {
xor_swap(&arr[i], &arr[i + 1]);
swapped = 1;
}
}
}
}
void bubble_sort_recursive(int *arr, size_t size) {
int swapped = 0;
for (size_t i = 0; i < size - 1; i++) {
if (arr[i] > arr[i + 1]) {
xor_swap(&arr[i], &arr[i + 1]);
swapped = 1;
}
}
// If any elements were swapped, recursively call the function with a reduced size
if (swapped)
bubble_sort_recursive(arr, size - 1);
}
int main(void) {
// Test case 1: Bubble Sort
int arr1[] = {5, 2, 8, 1, 4};
int expected1[] = {1, 2, 4, 5, 8};
size_t size1 = sizeof(arr1) / sizeof(arr1[0]);
bubble_sort(arr1, size1);
printf("Testing Bubble Sort:\n");
printf("Expected: ");
for (size_t i = 0; i < size1; i++) {
printf("%d ", expected1[i]);
}
printf("\n");
printf("Actual: ");
for (size_t i = 0; i < size1; i++) {
printf("%d ", arr1[i]);
}
printf("\n\n");
// Test case 2: Recursive Bubble Sort
int arr2[] = {7, 3, 2, 9, 1};
int expected2[] = {1, 2, 3, 7, 9};
size_t size2 = sizeof(arr2) / sizeof(arr2[0]);
bubble_sort_recursive(arr2, size2);
printf("Testing Recursive Bubble Sort:\n");
printf("Expected: ");
for (size_t i = 0; i < size2; i++) {
printf("%d ", expected2[i]);
}
printf("\n");
printf("Actual: ");
for (size_t i = 0; i < size2; i++) {
printf("%d ", arr2[i]);
}
printf("\n\n");
return 0;
}