mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Create StoogeSort.c
This commit is contained in:
parent
b895794da8
commit
2d75886f46
37
Sorts/StoogeSort.c
Normal file
37
Sorts/StoogeSort.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
void stoogesort(int [], int, int);
|
||||
|
||||
void main()
|
||||
{
|
||||
int arr[100], i, n;
|
||||
|
||||
printf("How many elements do you want to sort: ");
|
||||
scanf("%d", &n);
|
||||
for (i = 0;i < n; i++)
|
||||
scanf(" %d", &arr[i]);
|
||||
stoogesort(arr, 0, n - 1);
|
||||
printf("Sorted array : \n");
|
||||
for (i = 0;i < n;i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user