diff --git a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md index 75eca27..2b10048 100644 --- a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md +++ b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md @@ -1,2 +1,47 @@ -|OEIS|General term $`(k \ge 1)`$|Concrete gaps|Worst-case time complexity|Author and year of publication| -|:---|:-------------------------|:------------|:-------------------------|:-----------------------------| +# Shell Sort Handout + +## What is shell sort? +- Combination of sorting by insertion (insertion sort) and sorting by exchange (bubble sort) +- Starts off by comparing two elements that are far apart and gradually reduce the gap between them +- It works better than regular insertion sort as it moves certain out of place elements into place faster + +## Time Complexity (Shell's Original Sequence): +**Best Case:** $`O(NLogN)`$
+**Average Case:** $`O(N^\frac{5}{4})`$
+**Worst Case:** $`O(N^2)`$ + +## Space Complexity: +The space complexity of shell sort will always be **O(1)** as it does not create another array + +## Pros & Cons: +|Pros|Cons| +|:---|:---| +|- Efficient sorting algorithm for medium sized arrays
- Good when memory restraint is tight (Merge and Quicksort will sometimes MLE)
- The fastest non-recursive sorting algorithm
- Much faster than insertion with basically the same amount of code
- Easy to get working and only requires knowledge of iterations and conditional statements|- Heap, merge and quick sort are both more efficient
- Not widely used as it is difficult to understand and implement
- Many people often use the built in version ```Arrays.sort()``` which uses quick sort
- Unstable sorting algorithm (Equal elements will appear in different keys)
- Not as effective for extremely large data sets| + +## Gap Sequences: +|General term $`(k \ge 1)`$|Concrete gaps|Worst-case time complexity|Author and year of publication| +|:------------------------:|:-----------:|:------------------------:|:----------------------------:| +|$`\Bigl\lfloor\dfrac{N}{2^K}\Bigr\rfloor`$|$`\dfrac{N}{2}`$, $`\dfrac{N}{4}`$, $`\dfrac{N}{2}`$|O(N^2)|Shell - 1959| +|$`2\Bigl\lfloor\dfrac{N}{2^{k+1}}\Bigr\rfloor+1`$|$`2\Bigl\lfloor\dfrac{N}{4}\Bigr\rfloor+1`$, $`3`$, $`1`$|$`O(N^\frac{3}{2})`$|Franz & Lazarus - 1960| +|$`2^k - 1`$|$`1, 3, 7, 15, 31, 63`$|$`O(N^\frac{3}{2})`$|Hibbard - 1963| +|$`2^k + 1`$|$`3, 5, 9, 17, 33, 65`$|$`O(N^\frac{3}{2})`$|Papernov & Stasevich - 1965| +|$`2^p`$$`3^q`$|$`1, 2, 3, 4, 6, 8, 9`$|$`O(NLog^{2}N)`$|Pratt - 1971| +|$`\dfrac{3^{k}-1}{2}`$|$`1, 4, 13, 40, 121`$|$`O(N^\frac{3}{2})`$|Pratt & Knuth - 1973| +|$`4^k+3\times2^{k-1}+1`$|$`1, 8, 23, 77, 281`$|$`O(N^\frac{4}{3})`$|Sedgewick - 1982| + +## Sample Program +``` +public static void shellSort(int arr[]) // Method Header + { + for(int gap = arr.length / 2; gap > 0; gap /= 2){ // Loop through the length of the gap sequences + for(int i = gap; i < arr.length; i++){ // Loop through from the gap length to the length of the array + int temp = arr[i], j; // Set the temp variable as the ith element in the array + for(j = i; j >= gap && arr[j - gap] > temp; j -= gap){ // Loop through all the element to swap + arr[j] = arr[j - gap]; // Insert the jth element at the j - gap index + } + arr[j] = temp; // Insert temp at the jth element where the other element was swapped + } + } + } + +``` \ No newline at end of file