Sorting Algorithms: Welcome To CS221: Programming & Data Structures
Sorting Algorithms: Welcome To CS221: Programming & Data Structures
Sorting Algorithms
• In the class, we have discussed a few of the sorting algorithms namely Bubble sort, Insertion sort and Quick Sort only.
• In these slides, you will find the algorithms of Merge sort, Radix sort and Shell sort. Each of the algorithms are shown with a
working example.
• Note: The slides are adapted from the textbook “Data Structures Using C” by Reema Theraja, Oxford University Press.
• The textbook “Data Structures Using C” is available online
Merge Sort
Merge sort is a sorting algorithm that uses the divide, The basic steps of a merge sort algorithm are as follows:
conquer, and combine algorithmic paradigm.
• Divide means partitioning the n-element array to be sorted into • If the array is of length 0 or 1, then it is already sorted.
two sub-arrays of n/2 elements. If A is an array containing zero or
• Otherwise, divide the unsorted array into two sub-arrays of about half
one element, then it is already sorted. However, if there are more the size.
elements in the array, divide A into two sub-arrays, A1 and A2 ,
each containing about half of the elements of A. • Use merge sort algorithm recursively to sort each sub-array.
• Conquer means sorting the two sub-arrays recursively using merge • Merge the two sub-arrays to form a single sorted list.
sort.
• Combine means merging the two sorted sub-arrays of size n/2 to
produce the sorted array of n elements
Example & Algorithm (MERGE)
The elements to be sorted are: MERGE Algorithm:
Solution:
Compare ARR[I] and ARR[J], the smaller of the two is placed in TEMP at the
location specified by INDEX and subsequently the value I or J is incremented.
When I is greater than MID, copy the remaining elements of the right sub-
array in TEMP.
The running time of merge sort in the average case and the worst case can be given as O(n log n). Although merge sort has an
optimal time complexity, it needs an additional space of O(n) for the temporary array TEMP.
Example of Radix Sort/Bucket Sort
Radix sort is a linear sorting algorithm for integers and uses the concept
of sorting names in alphabetical order. When we have a list of sorted
names, the radix is 26 (or 26 buckets) because there are 26 letters in the
English alphabet. So radix sort is also known as bucket sort. Observe that
words are first sorted according to the first letter of the name. That is, 26
classes are used to arrange the names, where the first class stores the
names that begin with A, the second class contains the names with B,
and so on. During the second pass, names are grouped according to the
second letter. After the second pass, names are sorted on the first two
letters. This process is continued till the nth pass, where n is the length of
the name with maximum number of letters. After every pass, all the
names are collected in order of buckets. That is, first pick up the names in
the first bucket that contains the names beginning with A. In the second
pass, collect the names from the second bucket, and so on.
When radix sort is used on integers, sorting is done on each of the digits
in the number. The sorting procedure proceeds by sorting the least
significant to the most significant digit. While sorting the numbers, we
have ten buckets, each for one digit (0, 1, 2, …, 9) and the number of
passes will depend on the length of the number having maximum
number of digits.
Example of Radix Sort/Bucket Sort
Sort the numbers given below using radix sort. 3rd pass: the numbers are sorted according to the digit at the
hundreds place. The buckets are pictured upside down.
345, 654, 924, 123, 567, 472, 555, 808, 911
1st pass: the numbers are sorted according to the digit at ones place.
The buckets are pictured upside down as shown below.
The numbers are collected bucket by bucket. The new list thus
formed is the final sorted result. After the third pass, the list can be
After this pass, the numbers are collected bucket by bucket. The new list thus given as 123, 345, 472, 555, 567, 654, 808, 911, 924
formed is used as an input for the next pass.
2nd pass: the numbers are sorted according to the digit at the tens place. The
buckets are pictured upside down.
Complexity of Radix Sort
To calculate the complexity of radix sort algorithm, assume that
there are n numbers that have to be sorted and k is the number of
digits in the largest number. In this case, the radix sort algorithm is
called a total of k times. The inner loop is executed n times. Hence,
the entire radix sort algorithm takes O(kn) time to execute. When
radix sort is applied on a data set of finite size (very small set of
numbers), then the algorithm runs in O(n) asymptotic time.
Shell Sort
Shell sort is a sorting algorithm that is a generalization of insertion sort. While Technique:
discussing insertion sort, we have observed two things:
• First, insertion sort works well when the input data is ‘almost sorted’. To visualize the way in which shell sort works, perform the following
steps:
• Second, insertion sort is quite inefficient to use as it moves the values just
one position at a time. Step 1: Arrange the elements of the array in the form of a table
Shell sort is considered an improvement over insertion sort as it compares and sort the columns (using insertion sort).
elements separated by a gap of several positions. This enables the element to
take bigger steps towards its expected position. In Shell sort, elements are Step 2: Repeat Step 1, each time with smaller number of longer
sorted in multiple passes and in each pass, data are taken with smaller and columns in such a way that at the end, there is only one column
smaller gap sizes. However, the last step of shell sort is a plain insertion sort. of data to be sorted.
But by the time we reach the last step, the elements are already ‘almost sorted’,
and hence it provides good performance.
Note that we are only visualizing the elements being arranged in a
table, the algorithm does its sorting in-place.
Algorithm & Example of Shell Sort Sort the elements given below using shell sort.
63, 19, 7, 90, 81, 36, 54, 45, 72, 27, 22, 9, 41, 59, 33