Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
83 views

Merge Sort, Radix Sort, Shell Sort

The document discusses merge sort and radix sort algorithms. It explains that merge sort divides an array into halves recursively until single elements are reached, then merges the sorted halves back together. Radix sort sorts numbers by sorting based on the individual digits in multiple passes. It has O(n) average and worst-case time complexity, but requires additional space and may be slower than other algorithms if digit operations are inefficient. Shell sort improves on insertion sort by sorting elements with larger gaps first to avoid large shifts.

Uploaded by

Anonymous CoIQPD
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
83 views

Merge Sort, Radix Sort, Shell Sort

The document discusses merge sort and radix sort algorithms. It explains that merge sort divides an array into halves recursively until single elements are reached, then merges the sorted halves back together. Radix sort sorts numbers by sorting based on the individual digits in multiple passes. It has O(n) average and worst-case time complexity, but requires additional space and may be slower than other algorithms if digit operations are inefficient. Shell sort improves on insertion sort by sorting elements with larger gaps first to avoid large shifts.

Uploaded by

Anonymous CoIQPD
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Sorting

Ms. P. S. Shete
Assistant Professor CSE
SITCOE, Yadrav
Merge Sort Algorithm
• Merge sort is a sorting technique based on
divide and conquer technique. With Average
case and worst-case time complexity being
Ο(n log n), it is one of the most respected
algorithms.
• Merge sort first divides the array into equal
halves and then combines them in a sorted
manner.

Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,


2
Yadrav
How merge sort works
• To understand merge sort, we take an
unsorted array as depicted below −

• We know that merge sort first divides the


whole array iteratively into equal halves
unless the atomic values are achieved. We see
here that an array of 8 items is divided into
two arrays of size 4.
Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,
3
Yadrav
• This does not change the sequence of
appearance of items in the original. Now we
divide these two arrays into halves.

• We further divide these arrays and we achieve


atomic value which can no more be divided.

Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,


4
Yadrav
• Now, we combine them in exactly same
manner they were broken down.
• We first compare the element for each list and
then combine them into another list in sorted
manner. We see that 14 and 33 are in sorted
positions. We compare 27 and 10 and in the
target list of 2 values we put 10 first, followed
by 27. We change the order 19 and 35. 42 and
44 are placed sequentially.

Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,


5
Yadrav
• In next iteration of combining phase, we
compare lists of two data values, and merge
them into a list of four data values placing all
in sorted order.

• After final merging, the list should look like


this −

Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,


6
Yadrav
Algorithm
• Merge sort keeps on dividing the list into equal
halves until it can no more be divided. By
definition, if it is only one element in the list, it
is sorted. Then merge sort combines smaller
sorted lists keeping the new list sorted too.
– Step 1 − divide the list recursively into two halves
until it can no more be divided.
– Step 2 − if it is only one element in the list it is
already sorted, return.
– Step 3 − merge the smaller lists into new list in
sorted order.
Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,
7
Yadrav
Radix Sort
• Radix Sort is generalization of Bucket Sort
• To sort Decimal Numbers radix/base will be
used as 10. so we need 10 buckets.
• Buckets are numbered as 0,1,2,3,…,9
• Sorting is Done in the passes
• Number of Passes required for sorting is
number of digits in the largest number in the
list.
Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,
9
Yadrav
Ex.
Range Passes
0 to 99 2 Passes
0 to 999 3 Passes
0 to 9999 4 Passes
• In First Pass number sorted based on Least
Significant Digit and number will be kept in same
bucket.
• In 2nd Pass, Numbers are sorted on second least
significant bit and process continues.
• At the end of every pass, numbers in buckets are
merged to produce common list.

Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,


10
Yadrav
Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,
11
Yadrav
Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,
12
Yadrav
• Radix Sort is very simple, and a computer can do
it fast. When it is programmed properly, Radix
Sort is in fact one of the fastest sorting
algorithms for numbers or strings of letters.
• Average case and Worst case Complexity - O(n)

Disadvantages
• Still, there are some tradeoffs for Radix Sort that
can make it less preferable than other sorts.
• The speed of Radix Sort largely depends on the
inner basic operations, and if the operations are
not efficient enough, Radix Sort can be slower
than some other algorithms such as Quick Sort
and Merge Sort.
Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,
13
Yadrav
Disadvantages
• In the example above, the numbers were all of
equal length, but many times, this is not the case.
If the numbers are not of the same length, then a
test is needed to check for additional digits that
need sorting. This can be one of the slowest parts
of Radix Sort, and it is one of the hardest to make
efficient.
• Radix Sort can also take up more space than
other sorting algorithms, since in addition to the
array that will be sorted, you need to have a
sublist for each of the possible digits or letters.

Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,


14
Yadrav
Data Structure - Shell Sort
• Shell sort is a highly efficient sorting algorithm
and is based on insertion sort algorithm. This
algorithm avoids large shifts as in case of
insertion sort if smaller value is very far right and
have to move to far left.
• This algorithm uses insertion sort on widely
spread elements first to sort them and then sorts
the less widely spaced elements. This spacing is
termed as interval. This interval is calculated
based on Knuth's formula as −
• h=h*3+1
where − h is interval with initial value 1

This algorithm is quite efficient for medium


sized data sets as its average and worst case
complexity are of O(n^2) where n are no. of
items.
How shell sort works
• We take the below example to have an idea, how
shell sort works?

• We take the same array we have used in our


previous examples. {35,33,42,10,14,19,27,44}
• For our example and ease of understanding we
take the interval of 4.
• And make a virtual sublist of all values located at
the interval of 4 positions. Here these values are
{35, 14}, {33, 19}, {42, 27} and {10, 14}
We compare values in each sub-list and swap them (if necessary) in the original
array. After this step, new array should look like this −
Then we take interval of 2 and this gap generates two sublists - {14, 27, 35, 42}, {19,
10, 33, 44}

We compare and swap the values, if required, in the original array. After this step,
this array should look like this −

And finally, we sort the rest of the array using interval of value 1. Shell sort uses
insertion sort to sort the array. The step by step depiction is shown below −
Algorithm
• We shall now see the algorithm for shell sort.
• Step 1 − Initialize the value of h
• Step 2 − Divide the list into smaller sub-list of
equal interval h
• Step 3 − Sort these sub-lists using insertion
sort
• Step 4 − Repeat until complete list is sorted
Thank You!!!!!!

Ms. P. S. Shete, Asst. Prof, CSE, SITCOE,


22
Yadrav

You might also like