-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstoogeSort.c
More file actions
20 lines (18 loc) · 834 Bytes
/
stoogeSort.c
File metadata and controls
20 lines (18 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "esotericFunMiscellaneous.h"
// Worst case performance O(n**2.709) or O(n**(log 3 / log 1.5))
// Best case performance O(n**2.709) or O(n**(log 3 / log 1.5))
// Average case performance O(n**2.709) or O(n**(log 3 / log 1.5))
void stoogeSort(long int *array, int i, int j){
long int aux;
if(*(array + i) > *(array + j)){ // Swap initially the fist and last elements
aux = *(array + i);
*(array + i) = *(array + j);
*(array + j) = aux;
}
if(j - i > 1){
aux = (j - i + 1) / 3;
stoogeSort(array, i, j - aux); // Swap recursively the first 2/3 elements of the array
stoogeSort(array, i + aux, j); // Swap recursively the last 2/3 elements of the array
stoogeSort(array, i, j - aux); // Swap recursively again the first 2/3 elements of the array
}
}