SHELL SORT ALGORITHM
Shell Sort
▶ Founded by Donald Shell in 1959
▶ 1st algorithm to break the quadratic time barrier
▶ a highly efficient sorting algorithm
▶ known as diminishing increment sort
▶ Generalization of insertion short
How does Shell Sort Works?
▶ works by comparing elements that are distant rather than
adjacent elements in an array or list where adjacent elements are
compared
▶ uses increment sequence
▶ makes multiple passes through a list
▶ improves on the efficiency of insertion sort
▶ decreases distance between comparisons
Example
Here these values are {35, 14}, {33, 19}, {42, 27} and {10, 44}
Example
Compare values in each sub-list and swap them (if necessary) in the
original array. After this step, the new array should look like this −
Example
Then, take interval of 2 and this gap generates two sub-lists - {14, 27,
35, 42}, {19, 10, 33, 44}
Example
Again, compare and swap the values, if required, in the original array.
After this step, the array should look like this −
Finally, the rest of the array sorted using interval
of value 1. Shell sort uses insertion sort to sort the
array.
Example
We see that it
required only four
swaps to sort the rest
of the array.
Empirical Analysis of Shell Sort
Empirical Analysis of Shell Sort
(Advantage)
▶ Fastest of all O (N^2) sorting algorithms
▶ it’s only efficient for medium size lists
▶ Five (5) times faster than the bubble sort
▶ a little over twice as fast as the insertion sort
Empirical Analysis of Shell Sort
(Dis-advantage)
▶ it is a complex algorithm
▶ it’s not nearly as efficient as the merge, heap, and quick sorts
▶ still significantly slower than the merge, heap, and quick sorts
▶ also an excellent choice for repetitive sorting of smaller lists.
Shell Sort Best Case
▶ when the array is already sorted in the right order
▶ the number of comparisons is less.
Shell Sort Worst Case
▶ running time of Shell sort depends on the choice of increment
sequence.
▶ pairs of increments are not necessarily relatively prime and smaller
increments can have little effect.
RADIX SORT ALGORITHM
Radix Sort
▶ Founded by Harold H. Seward in 1954.
▶ Radix sort is a non-comparative integer sorting algorithm that sorts
data with integer keys by grouping keys by the individual digits which
share the same significant position and value.
▶ Unlike other sorting methods, radix sort considers the structure of the
keys
▶ Sorting is done by comparing bits in the same position
▶ Extension to keys that are alphanumeric strings
How does Radix Sort Works?
▶ sorts by grouping numbers by their individual digits (or by
their radix).
▶ It uses each radix digit as a key, and implements counting sort or
bucket sort under the hood in order to do the work of sorting.
▶ Take the least significant digit of each key.
▶ Group the keys based on that digit, but otherwise keep the original
order of keys.
▶ Repeat the grouping process with each more significant digit.
Example
Original, unsorted list:
170, 45, 75, 90, 802, 24, 2, 66
Complexity
▶ The complexity of radix sort is linear, which in terms of omega means
O(n). That is a great benefit in performance compared to O(n.log(n))
or even worse with O(n2) as we can see on the following chart.
Why using radix sort?
1. It’s fast
Radix sort is very fast compared to other sorting algorithms as we saw on
the diagram above. This algorithm is very useful in practice because in
practice we often sort sets of integers.
2. It’s easy to understand and implement
Even a beginner can understand and implement radix sort, which is
great. You need no more than few loops to implement it.
Why NOT using radix sort?
1. Works only with integers
If you’re not sure about the input better do not use radix sort. We may
think that our input consists only of integers and we can go for radix
sort, but what if in the future someone passes floats or strings to our
routine.
2. Requires additional space
Radix sort needs additional space – at least as much as the input.
Empirical Analysis of Radix Sort
Final Words
▶ Radix sort is restricted by the input’s domain, but I must say that in
practice there are tons of cases where only integers are sorted. This is
when we get some data from the based on primary keys – typically
primary in database tables are integers as well. So practically there
are lots of cases of sorting integers, so radix sort may be one very,
very useful algorithm and it is so cool that it is also easy to
implement.

Sorting Techniques for Data Structures.pptx

  • 1.
  • 2.
    Shell Sort ▶ Foundedby Donald Shell in 1959 ▶ 1st algorithm to break the quadratic time barrier ▶ a highly efficient sorting algorithm ▶ known as diminishing increment sort ▶ Generalization of insertion short
  • 3.
    How does ShellSort Works? ▶ works by comparing elements that are distant rather than adjacent elements in an array or list where adjacent elements are compared ▶ uses increment sequence ▶ makes multiple passes through a list ▶ improves on the efficiency of insertion sort ▶ decreases distance between comparisons
  • 4.
    Example Here these valuesare {35, 14}, {33, 19}, {42, 27} and {10, 44}
  • 5.
    Example Compare values ineach sub-list and swap them (if necessary) in the original array. After this step, the new array should look like this −
  • 6.
    Example Then, take intervalof 2 and this gap generates two sub-lists - {14, 27, 35, 42}, {19, 10, 33, 44}
  • 7.
    Example Again, compare andswap the values, if required, in the original array. After this step, the array should look like this − Finally, the rest of the array sorted using interval of value 1. Shell sort uses insertion sort to sort the array.
  • 8.
    Example We see thatit required only four swaps to sort the rest of the array.
  • 9.
  • 10.
    Empirical Analysis ofShell Sort (Advantage) ▶ Fastest of all O (N^2) sorting algorithms ▶ it’s only efficient for medium size lists ▶ Five (5) times faster than the bubble sort ▶ a little over twice as fast as the insertion sort
  • 11.
    Empirical Analysis ofShell Sort (Dis-advantage) ▶ it is a complex algorithm ▶ it’s not nearly as efficient as the merge, heap, and quick sorts ▶ still significantly slower than the merge, heap, and quick sorts ▶ also an excellent choice for repetitive sorting of smaller lists.
  • 12.
    Shell Sort BestCase ▶ when the array is already sorted in the right order ▶ the number of comparisons is less.
  • 13.
    Shell Sort WorstCase ▶ running time of Shell sort depends on the choice of increment sequence. ▶ pairs of increments are not necessarily relatively prime and smaller increments can have little effect.
  • 14.
  • 15.
    Radix Sort ▶ Foundedby Harold H. Seward in 1954. ▶ Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. ▶ Unlike other sorting methods, radix sort considers the structure of the keys ▶ Sorting is done by comparing bits in the same position ▶ Extension to keys that are alphanumeric strings
  • 16.
    How does RadixSort Works? ▶ sorts by grouping numbers by their individual digits (or by their radix). ▶ It uses each radix digit as a key, and implements counting sort or bucket sort under the hood in order to do the work of sorting. ▶ Take the least significant digit of each key. ▶ Group the keys based on that digit, but otherwise keep the original order of keys. ▶ Repeat the grouping process with each more significant digit.
  • 17.
    Example Original, unsorted list: 170,45, 75, 90, 802, 24, 2, 66
  • 20.
    Complexity ▶ The complexityof radix sort is linear, which in terms of omega means O(n). That is a great benefit in performance compared to O(n.log(n)) or even worse with O(n2) as we can see on the following chart.
  • 21.
    Why using radixsort? 1. It’s fast Radix sort is very fast compared to other sorting algorithms as we saw on the diagram above. This algorithm is very useful in practice because in practice we often sort sets of integers. 2. It’s easy to understand and implement Even a beginner can understand and implement radix sort, which is great. You need no more than few loops to implement it.
  • 22.
    Why NOT usingradix sort? 1. Works only with integers If you’re not sure about the input better do not use radix sort. We may think that our input consists only of integers and we can go for radix sort, but what if in the future someone passes floats or strings to our routine. 2. Requires additional space Radix sort needs additional space – at least as much as the input.
  • 23.
  • 24.
    Final Words ▶ Radixsort is restricted by the input’s domain, but I must say that in practice there are tons of cases where only integers are sorted. This is when we get some data from the based on primary keys – typically primary in database tables are integers as well. So practically there are lots of cases of sorting integers, so radix sort may be one very, very useful algorithm and it is so cool that it is also easy to implement.