0% found this document useful (0 votes)
89 views

Sorting Algorithms: Welcome To CS221: Programming & Data Structures

Here are the solutions to the questions: 1) Shell Sort: GAP_SIZE = 2 {30, 60, 20, 50, 40, 10} GAP_SIZE = 1 {10, 20, 30, 40, 50, 60} Quick Sort: Pivot = 35, Partition gives {20, 30, 10, 40, 50, 60} Pivot = 40, Partition gives {20, 30, 10, 40, 50, 60} 2) Pseudo code for Quicksort with Insertion Sort for subarrays: quickSort(arr, left, right) if right - left <= LIMIT insertionSort(arr, left, right) else

Uploaded by

PRITAM RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Sorting Algorithms: Welcome To CS221: Programming & Data Structures

Here are the solutions to the questions: 1) Shell Sort: GAP_SIZE = 2 {30, 60, 20, 50, 40, 10} GAP_SIZE = 1 {10, 20, 30, 40, 50, 60} Quick Sort: Pivot = 35, Partition gives {20, 30, 10, 40, 50, 60} Pivot = 40, Partition gives {20, 30, 10, 40, 50, 60} 2) Pseudo code for Quicksort with Insertion Sort for subarrays: quickSort(arr, left, right) if right - left <= LIMIT insertionSort(arr, left, right) else

Uploaded by

PRITAM RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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:

The merge sort algorithm uses a function MERGE which


combines the sub-arrays to form a sorted array. While the merge
sort algorithm recursively divides the list into smaller lists, the
merge algorithm conquers the list to sort the elements in
individual lists. Finally, the smaller lists are merged to form one
list
To understand the merge algorithm, consider the figure below which shows how we merge two lists to form one list. For
ease of understanding, we have taken two sub-lists each containing four elements. The same concept can be utilized to
merge four sub-lists containing two elements, or eight sub-lists having one element each.

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

The algorithm to sort an array of elements using shell


sort is shown above. In the algorithm, we sort the
elements of the array Arr in multiple passes. In each
pass, we reduce the GAP_SIZE (visualize it as the number
of columns) by a factor of half as done in Step 4. In each
iteration of the for loop in Step 5, we compare the values
of the array and interchange them if we have a larger
value preceding the smaller one.
Solve:
1. Given the following array: {30, 60, 20, 50, 40, 10}.
• Using the Shell Sort, show the array after each iteration of the outer loop corresponding to a new GAP_SIZE value.
• Using the Quick Sort, show the array after each call of Partition(). Assume the pivot index is calculated as (Left+Right)/2
2. For very short arrays, Insertion Sort is actually faster than Quick Sort. Write a version of Quick Sort that uses Insertion Sort to sort a
sub-array whose length is less than some specified limit.
3. A list of unsorted elements are: 39 9 81 45 90 27 72 18. Apply Merge sort algorithm to sort the elements in ascending order.
4. Solve Q. No. 4 using Radix sort algorithm.
5. Solve Q. Nos. 10, 11 and 12 of Chapter 14 page no 462 (from “Data Structures Using C”)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy