From 491fe8805fcc3b2c4919108976d6a267aa7e4455 Mon Sep 17 00:00:00 2001 From: "Pete ._. Mango" Date: Thu, 7 Nov 2019 16:57:09 +0000 Subject: [PATCH 1/6] Add new file --- .../Computer Science/ICS4U1/Sorting Methods/Shellsort List.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md diff --git a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md new file mode 100644 index 0000000..75eca27 --- /dev/null +++ b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md @@ -0,0 +1,2 @@ +|OEIS|General term $`(k \ge 1)`$|Concrete gaps|Worst-case time complexity|Author and year of publication| +|:---|:-------------------------|:------------|:-------------------------|:-----------------------------| From 2c0612b30e7ddd64a5e906720aabce72196b4716 Mon Sep 17 00:00:00 2001 From: "Pete ._. Mango" Date: Thu, 7 Nov 2019 19:55:54 +0000 Subject: [PATCH 2/6] Update Shellsort List.md --- .../ICS4U1/Sorting Methods/Shellsort List.md | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) 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 From 3e06aaa4036eff67bdd228eb45056a1c1e6b75fb Mon Sep 17 00:00:00 2001 From: "Pete ._. Mango" Date: Thu, 7 Nov 2019 20:19:54 +0000 Subject: [PATCH 3/6] Update Shellsort List.md --- .../ICS4U1/Sorting Methods/Shellsort List.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 2b10048..2c3f643 100644 --- a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md +++ b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md @@ -1,6 +1,7 @@ # Shell Sort Handout ## What is shell sort? +- "Diminishing Increment Sort", better known as comb 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 @@ -44,4 +45,12 @@ public static void shellSort(int arr[]) // Method Header } } -``` \ No newline at end of file +``` + +## Enhanced Shell Sort +- A method to calculate the gap sequence and can decrease the number of comparisons by up to 60% +- Published by Basit Shahzad and Muhammad Tanvir Afzal from the World Academy of Science +- The gap sequence would be h1 and hn+1 = 3hn+1 +- For 100 elements, shell's original gap seuqence will make 170 comparisons but the Enhanced Shell Sort will make 85 comparisons + + From 2a6d9b2977e41192ac55fdd8d2726f2aa97f4f57 Mon Sep 17 00:00:00 2001 From: "Pete ._. Mango" Date: Thu, 7 Nov 2019 20:39:42 +0000 Subject: [PATCH 4/6] Update Shellsort List.md --- .../ICS4U1/Sorting Methods/Shellsort List.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 2c3f643..fb99fee 100644 --- a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md +++ b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md @@ -31,7 +31,7 @@ The space complexity of shell sort will always be **O(1)** as it does not create |$`4^k+3\times2^{k-1}+1`$|$`1, 8, 23, 77, 281`$|$`O(N^\frac{4}{3})`$|Sedgewick - 1982| ## Sample Program -``` +```java 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 @@ -53,4 +53,15 @@ public static void shellSort(int arr[]) // Method Header - The gap sequence would be h1 and hn+1 = 3hn+1 - For 100 elements, shell's original gap seuqence will make 170 comparisons but the Enhanced Shell Sort will make 85 comparisons +## Testing +`10 Elements: 100, 37, 12, 86, 2, 127, 62, 14, 3, 9`
+**Shell Sort:** 30
+**Enhanced Shell Sort:** 12 + +`25 Elements: 100, 37, 12, 86, 2, 127, 62, 14, 3, 9, 30, 14, 90, 1, 20, 30, 74, 48, 37, 40, 7, 101, 200`
+**Shell Sort:** 47
+**Ehanced Shell Sort:** 31 + +`35 Elements: 1,2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14, 90,1,20,30,74,48,37,40,7,101,200,4,9 `
+ From 1c9c5b9e662a0e3f823bda05169494dde9e9d88e Mon Sep 17 00:00:00 2001 From: "Pete ._. Mango" Date: Thu, 7 Nov 2019 21:21:02 +0000 Subject: [PATCH 5/6] Update Shellsort List.md --- .../ICS4U1/Sorting Methods/Shellsort List.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 fb99fee..de90f32 100644 --- a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md +++ b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md @@ -63,5 +63,8 @@ public static void shellSort(int arr[]) // Method Header **Ehanced Shell Sort:** 31 `35 Elements: 1,2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14, 90,1,20,30,74,48,37,40,7,101,200,4,9 `
+**Shell Sort:** 202 +**Enhanced Shell Sort:** 105 - +## Number of Comparisons to Array Size Chart +![alt text](https://i.imgur.com/SNfzwpU.png) From 98aab86a5e3489325afe8899f7fe7cf52600ddcf Mon Sep 17 00:00:00 2001 From: "Pete ._. Mango" Date: Thu, 7 Nov 2019 21:40:25 +0000 Subject: [PATCH 6/6] Update Shellsort List.md --- .../ICS4U1/Sorting Methods/Shellsort List.md | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) 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 de90f32..c74628b 100644 --- a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md +++ b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Shellsort List.md @@ -12,7 +12,7 @@ **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 +The space complexity of shell sort will always be __O(1)__ as it does not create another array ## Pros & Cons: |Pros|Cons| @@ -47,6 +47,19 @@ public static void shellSort(int arr[]) // Method Header ``` +## Comparison to Other Sorting Algorithms: +### Shell vs Insertion +Shell sort can be seen as an improvement to insertion sort as it uses a variety of gap sequences and moves out of place elements into their correct positions faster whereas insertion sort will check each one by one. However, if the array is partially or nearly sorted, then insertion would still be the better option. Like selection sort, shell sort should be used for larger data sets. + +### Shell vs Selection +Both insertion and shell sort can be used when the memory restraint is super tight and does not allow for auxillary memory. However, shell sort has a best case of $`O(NLogN)`$ while selection sort has a best time complexity of $`O(N^2)`$. For larger data sets, shell sort would obviously be more appealing than selection. + +### Shell vs Quick +Shell sort and quick sort does not serve the same purpose, but many people tends to use quicksort as it is known as the "Queen of All Sorts" for its ability to sort randomized list in a short amount of time. However, shell sort is more favorable with strict space complexity as quick sort requires $`O(NLogN)`$ auxillary space complexity. + +### Shell vs Merge +Merge sort is often regarded as one of the best sorting algorithms with consistent $`O(NLogN)`$ time complexity. However, like quick sort, it requires $`O(2N)`$ memory, which sometimes will result in MLE (Memory Limit Exceeded). For larger data sets, merge sort would, most of the time, be the better sorting algorithm. + ## Enhanced Shell Sort - A method to calculate the gap sequence and can decrease the number of comparisons by up to 60% - Published by Basit Shahzad and Muhammad Tanvir Afzal from the World Academy of Science @@ -55,16 +68,16 @@ public static void shellSort(int arr[]) // Method Header ## Testing `10 Elements: 100, 37, 12, 86, 2, 127, 62, 14, 3, 9`
-**Shell Sort:** 30
-**Enhanced Shell Sort:** 12 +Shell Sort: *30*
+Enhanced Shell Sort: *12* `25 Elements: 100, 37, 12, 86, 2, 127, 62, 14, 3, 9, 30, 14, 90, 1, 20, 30, 74, 48, 37, 40, 7, 101, 200`
-**Shell Sort:** 47
-**Ehanced Shell Sort:** 31 +Shell Sort: *47*
+Ehanced Shell Sort: *31* `35 Elements: 1,2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14, 90,1,20,30,74,48,37,40,7,101,200,4,9 `
-**Shell Sort:** 202 -**Enhanced Shell Sort:** 105 +Shell Sort: *202*
+Enhanced Shell Sort: *105* ## Number of Comparisons to Array Size Chart ![alt text](https://i.imgur.com/SNfzwpU.png)