-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrick_sort.c
39 lines (34 loc) · 919 Bytes
/
brick_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
#include <stdio.h>
#include <stdlib.h>
void brick_sort(int *arr, int n) {
int temp;
int is_sorted = 0;
while (!is_sorted) {
is_sorted = 1;
for (int i = 1; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
is_sorted = 0;
}
}
for (int i = 0; i <= n - 2; i += 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
is_sorted = 0;
}
}
}
}
int main(int argc, char** argv) {
int arr[] = { 5, 4, 3, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
brick_sort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}