TheAlgorithms-C/sorting/stooge_sort.c

37 lines
722 B
C
Raw Normal View History

2018-10-02 07:01:26 +03:00
#include <stdio.h>
2020-04-08 16:41:12 +03:00
void stoogesort(int[], int, int);
int main()
2018-10-02 07:01:26 +03:00
{
int arr[100], i, n;
2020-04-08 16:41:12 +03:00
2018-10-02 07:01:26 +03:00
printf("How many elements do you want to sort: ");
scanf("%d", &n);
for (i = 0; i < n; i++) scanf(" %d", &arr[i]);
2018-10-02 07:01:26 +03:00
stoogesort(arr, 0, n - 1);
printf("Sorted array : \n");
2020-04-08 16:41:12 +03:00
for (i = 0; i < n; i++)
2018-10-02 07:01:26 +03:00
{
printf("%d ", arr[i]);
}
printf("\n");
2020-04-08 16:41:12 +03:00
return 0;
2018-10-02 07:01:26 +03:00
}
2020-04-08 16:41:12 +03:00
2018-10-02 07:01:26 +03:00
void stoogesort(int arr[], int i, int j)
{
int temp, k;
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
if ((i + 1) >= j)
return;
k = (int)((j - i + 1) / 3);
stoogesort(arr, i, j - k);
stoogesort(arr, i + k, j);
stoogesort(arr, i, j - k);
}