Skip to content

Commit cd59b18

Browse files
author
CheerfulBear22
committed
Added sorting/slow_sort.c
1 parent e5dad3f commit cd59b18

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

sorting/slow_sort.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
5+
void display(int arr[], int size)
6+
{
7+
for(int i = 0; i < size; i++)
8+
{
9+
printf("%d ", arr[i]);
10+
}
11+
12+
printf("\n");
13+
}
14+
15+
void swap(int *num1, int *num2)
16+
{
17+
int temp = *num1;
18+
*num1 = *num2;
19+
*num2 = *num1;
20+
}
21+
22+
void slowsort(int arr[], int low, int high)
23+
{
24+
if (low >= high)
25+
return;
26+
27+
int middle = (low + high) / 2;
28+
29+
slowsort(arr, low, middle);
30+
slowsort(arr, middle+1, high);
31+
32+
if (arr[high] < arr[middle])
33+
swap(&arr[high], &arr[middle]);
34+
35+
slowsort(arr, low, high - 1);
36+
}
37+
38+
void test()
39+
{
40+
const int size = 20;
41+
42+
int *arr = (int *)calloc(size, sizeof(int));
43+
44+
for (int i = 0; i < size; i++)
45+
{
46+
arr[i] = rand() % 100;
47+
}
48+
49+
slowsort(arr, 0, size-1);
50+
51+
display(arr, size);
52+
53+
free(arr);
54+
}
55+
56+
int main()
57+
{
58+
srand(time(NULL));
59+
test();
60+
return 0;
61+
}

0 commit comments

Comments
 (0)