Docsity

Shellsort-Algorithm Design and Analysis for Sorting-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Shellsort-Algorithm Design and Analysis for Sorting-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Shellsort

Shellsort

 Founded by Donald Shell and named the

sorting algorithm after himself in 1959.

st

algorithm to break the quadratic time barrier

but few years later, a sub quadratic time bound

was proven

 Shellsort works by comparing elements that

are distant rather than adjacent elements in an

array or list where adjacent elements are

compared.

Shellsort

 Shellsort makes multiple passes through a list and sorts a number of equally sized sets using the insertion sort.

Shellsort

 Shellsort improves on the efficiency of insertion sort by quickly shifting values to their destination.

Shellsort

 After each phase and some increment h k , for every i , we have a[ i ] ≤ a [ i + h k ] all elements spaced h k apart are sorted.  The file is said to be h k

  • sorted.

Empirical Analysis of Shellsort

Source: http://linux.wku.edu/~lamonml/algor/sort/shell.html

Empirical Analysis of Shellsort

(Disadvantage)

 Disadvantage of Shellsort is that it is a complex

algorithm and its not nearly as efficient as the

merge, heap, and quick sorts.

 The shell sort is still significantly slower than

the merge, heap, and quick sorts, but its

relatively simple algorithm makes it a good

choice for sorting lists of less than 5000 items

unless speed important. It's also an excellent

choice for repetitive sorting of smaller lists.

Shellsort Best Case

 Best Case: The best case in the shell sort is when the array is already sorted in the right order. The number of comparisons is less.

Shellsort Examples

 Sort: 18 32 12 5 38 33 16 2 8 Numbers to be sorted, Shell’s increment will be floor(n/2) *** floor(8/2)**  floor(4) = 4 increment 4: 1 2 3 4 18 32 12 5 38 33 16 2 (visualize underlining) Step 1 ) Only look at 18 and 38 and sort in order ; 18 and 38 stays at its current position because they are in order. Step 2 ) Only look at 32 and 33 and sort in order ; 32 and 33 stays at its current position because they are in order. Step 3 ) Only look at 12 and 16 and sort in order ; 12 and 16 stays at its current position because they are in order. Step 4 ) Only look at 5 and 2 and sort in order ; 2 and 5 need to be switched to be in order.

Shellsort Examples (con’t)  Sort: 18 32 12 5 38 33 16 2 Resulting numbers after increment 4 pass: 18 32 12 2 38 33 16 5 *** floor(4/2)**  floor(2) = 2 increment 2: 1 2 18 32 12 2 38 33 16 5 Step 1 ) Look at 18 , 12 , 38 , 16 and sort them in their appropriate location: 12 38 16 2 18 33 38 5 Step 2 ) Look at 32 , 2 , 33 , 5 and sort them in their appropriate location: 12 2 16 5 18 32 38 33