Sorting/Shell Sort
comparisonin-placeunstable
Press play to start
1void insercaoPorCor(int* A, int cor, int h, int n) {
2 int i, j, tmp;
3 for (i = cor+h; i < n; i += h) {
4 tmp = A[i];
5 j = i - h;
6 while (j >= cor && A[j] > tmp) {
7 A[j+h] = A[j];
8 j -= h;
9 }
10 A[j+h] = tmp;
11 }
12}
13void shellSort(int* A, int n) {
14 int h = 1;
15 while (h < n) h = h*3+1;
16 for (h /= 3; h >= 1; h /= 3)
17 for (int cor=0; cor<h; cor++)
18 insercaoPorCor(A, cor, h, n);
19}
Step 1/0

Practice

LeetCode·#912 Sort an ArrayMediumHackerRank·Bubble SortMedium
BestO(n log n)
AverageO(n^1.5)
WorstO(n²)
SpaceO(1)