Shell Sort Data Structures Students Names Maged F










![Shell. Sort -Trace (gap = 4) [0] [2] [3] [4] [5] [6] the. Array Shell. Sort -Trace (gap = 4) [0] [2] [3] [4] [5] [6] the. Array](https://slidetodoc.com/presentation_image_h2/1c3a9719a8b7cb8e2254bf451894df8e/image-11.jpg)
![Shell. Sort -Trace (gap = 2) [0] [1] [2] [3] [4] [5] [6] the. Shell. Sort -Trace (gap = 2) [0] [1] [2] [3] [4] [5] [6] the.](https://slidetodoc.com/presentation_image_h2/1c3a9719a8b7cb8e2254bf451894df8e/image-12.jpg)
![Shell. Sort -Trace (gap = 1) [0] [1] [2] [3] [4] [5] [6] the. Shell. Sort -Trace (gap = 1) [0] [1] [2] [3] [4] [5] [6] the.](https://slidetodoc.com/presentation_image_h2/1c3a9719a8b7cb8e2254bf451894df8e/image-13.jpg)
- Slides: 13
Shell Sort Data Structures Students Names: Maged F. Shwkah, 201503133 Abubaker B. Osman, 201503137 Meshal Nafe Alanazi, 201600449 1
Shell Sort - Introduction More properly, Shell’s Sort Created in 1959 by Donald Shell Originally Shell built his idea on top of Bubble Sort, but it has since been transported over to Insertion Sort. 2
Shell Sort -General Description Essentially a segmented insertion sort Divides an array into several smaller noncontiguous segments The distance between successive elements in one segment is called a gap. Each segment is sorted within itself using insertion sort. Then resegment into larger segments (smaller gaps) and repeat sort. Continue until only one segment (gap = 1) - final sort finishes array sorting. 3
Shell Sort -Background General Theory: Makes use of the intrinsic strengths of Insertion sort is fastest when: The array is nearly sorted. The array contains only a small number of data items. Shell sort works well because: It always deals with a small number of elements. Elements are moved a long way through array with each swap and this leaves it more nearly sorted. 4
Shell Sort - example Initial Segmenting Gap = 4 80 93 60 12 42 30 68 85 10 10 30 60 12 42 93 68 85 80 5
Shell Sort - example (2) Resegmenting Gap = 2 10 30 60 12 42 93 68 85 80 10 12 42 30 68 93 80 60 85 6
Shell Sort - example (3) Resegmenting Gap = 1 10 12 42 30 60 85 68 93 80 10 12 30 42 60 68 80 85 93 7
Shell Sort - Time Complexity Shell’s Sort Visualization https: //www. youtube. com/watch? v=n 4 sk. Sz. Gv. ZA Time complexity: O(nr) with 1 < r < 2 This is better than O(n 2) but generally worse than O(n log 2 n). 8
Shellsort - Code class Shell. Sort { /* A function to print the array static void print. Array(int arr[]) { int n = arr. length; for (int i=0; i<n; ++i) System. out. print(arr[i] + " "); System. out. println(); } 9
Shellsort - Code 10
Shell. Sort -Trace (gap = 4) [0] [2] [3] [4] [5] [6] the. Array 80 93 60 12 42 30 68 n: 9 gap: 4 [1] [7] 85 [8] 10 i: j: int Shell. Sort(int arr[]) { int n = arr. length; for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i += 1) { int temp = arr[i]; int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) arr[j] = arr[j - gap]; arr[j] = temp; }} return 0; } 11
Shell. Sort -Trace (gap = 2) [0] [1] [2] [3] [4] [5] [6] the. Array 10 30 60 12 42 93 68 n: 9 gap: 2 i: j: int Shell. Sort(int arr[]) {int n = arr. length; for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i += 1) { int temp = arr[i]; int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) arr[j] = arr[j - gap]; arr[j] = temp; }} return 0; } [7] 85 [8] 80 12
Shell. Sort -Trace (gap = 1) [0] [1] [2] [3] [4] [5] [6] the. Array 10 12 42 30 60 85 68 n: 9 gap: 1 i: j: int Shell. Sort(int arr[]) { int n = arr. length; for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i += 1) { int temp = arr[i]; int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) arr[j] = arr[j - gap]; arr[j] = temp; }} return 0; } [7] 93 [8] 80 13