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

CH 5 Sorting

The document discusses sorting algorithms. It provides an overview of sorting and describes insertion sort in detail. Insertion sort works by building a sorted list from left to right by inserting each element into the correct position. The time complexity of insertion sort is O(n^2) in the worst case and O(n) in the best case.

Uploaded by

lmxcosmos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CH 5 Sorting

The document discusses sorting algorithms. It provides an overview of sorting and describes insertion sort in detail. Insertion sort works by building a sorted list from left to right by inserting each element into the correct position. The time complexity of insertion sort is O(n^2) in the worst case and O(n) in the best case.

Uploaded by

lmxcosmos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

5.

Sorting
Data Structure and Algorithms

Dr. Udaya Raj Dhungana


Assist. Professor
Pokhara University, Nepal
Guest Faculty
Hochschule Darmstadt University of Applied Sciences, Germany
E-mail: [email protected] and [email protected]

1
Overview
Unit 5: Sorting Algorithms (5 hrs)
1.Internal/external Sort, Stable/Unstable Sort
2.Insertion and selection Sort
3.Bubble and Exchange Sort
4.Quick Sort and Merge Sort
5.Radix Sort
6.Shell Sort
7.Heap Sort as priority queue

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 2


Sorting
• Sorting refers to arranging a list of data in a particular order-
ascending or descending.

Unsorted List

Sorted lists

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 3


Sorting
• Most common orders are in lexicographical or numerical order.

Unsorted lists Sorted lists

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 4


Why Sorting?
• There are so many things in our real life that we need to search for,
like a particular record in database, roll numbers in merit list, a
particular telephone number in telephone directory, a particular
page in a book etc.
• All this would have been a mess if the data was kept unordered and
unsorted,
• but fortunately the concept of sorting came into existence, making it
easier for everyone to arrange data in an order, hence making it
easier to search.
• Sorting arranges data in a sequence which makes searching easier.
• The importance of sorting lies in the fact that data searching can be
optimized to a very high level, if data is stored in a sorted form.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 5


Stable and Unstable Sorting
• If a sorting algorithm, after sorting the contents, does not change
the sequence (order) of similar content (elements with same value)
in which they appear, it is called stable sorting.
• If a sorting algorithm, after sorting the contents, changes the
sequence of similar content in which they appear, it is called
unstable sorting.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 6


Internal and External Sorting
• If the number of data is small enough to fits into the main memory,
sorting is called internal sorting.
• If the number of data is so large that some of them reside on
external storage during the sort, it is called external sorting.

(Main Memory)

Internal Sorting External Sorting

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 7


Internal and External Sorting

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 8


Internal Sorting
• An internal sort is any data sorting process that takes place entirely
within the main memory of a computer.
• This is possible whenever the data to be sorted is small enough to all
be held in the main memory.
• Some common internal sorting algorithms include:
• bubble sort,
• insertion sort,
• quick sort,
• heap sort,
• radix sort and
• selection sort.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 9


External Sorting
• External sorting is a term for a class of sorting algorithms that can handle massive
amounts of data.
• External sorting is required when the data being sorted do not fit into the main
memory of a computing device (usually RAM) and instead they must reside in the
slower external memory (usually a hard drive).
• External sorting typically uses a hybrid sort-merge strategy.
• In the sorting phase, chunks of data small enough to fit in main memory are read,
sorted, and written out to a temporary file.
• In the merge phase, the sorted sub-files are combined into a single larger file.
• One example of external sorting is the external merge sort algorithm, which sorts
chunks that each fit in RAM, then merges the sorted chunks together.
• We first divide the file into runs such that the size of a run is small enough to fit
into main memory.
• Then sort each run in main memory using merge sort sorting algorithm.
• Finally merge the resulting runs together into successively bigger runs, until the
file is sorted.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 10


Insertion Sort

Source: https://www.happycoders.eu/algorithms/insertion-sort/#Example_Sorting_Playing_Cards

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 11


Insertion Sort
• Insertion sort is a simple sorting algorithm that works the way we sort
playing cards in our hands.
• Consider you have 10 cards out of a deck of cards in your hand. And they
are sorted, or arranged in the ascending order of their numbers.
• If your friend gives you another card, and ask you to insert the card in
just the right position, so that the cards in your hand are still sorted.
• What will you do? Well, you will have to go through each card from the
starting or the back and find the right position for the new card,
comparing its value with each card.
• Once you find the right position, you will insert the card there.
• Similarly, if more new cards are provided to you, you can easily repeat
the same process and insert the new cards and keep the cards sorted
too.
• This is exactly how insertion sort works.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 12


Insertion Sort
• The first element in the array is already sorted. Therefore, insertion sort
starts from the second element (index 1).
• Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element
at index 1, the key.
2. We compare the current element with the element(s) before it, in this
case, element at index 0:
A. If the current element is less than the first element, we insert
the key element before the first element.
B. If the key element is greater than the first element, then we
insert it after the first element.
3. Then, we take the third element of the array as current element and will
compare it with all elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 13


Insertion Sort

A graphical example of insertion sort. The partial sorted list (black)


initially contains only the rst element in the list. With each iteration
one element (red) is removed from the "not yet checked for order"
input data and inserted in-place into the sorted list.

Sourse: https://en.wikipedia.org/wiki/Insertion_sort

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 14


fi
Insertion Sort
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 15


Insertion Sort
• Example 1: Illustrate the operation of INSERTION-SORT on the array A
= (31, 41, 59, 26, 41, 58).

Pass A[1] A[2] A[3] A[4] A[5] A[6] Remarks


K=0 31 41 59 26 41 58 No shift

K=1 31 41 59 26 41 58 No shift
Shift = 3
K=2 31 41 59 26 41 58
Move = 1
Shift = 1
K=3 26 31 41 59 41 58
Move = 1
Shift = 1
K=4 26 31 41 41 59 58
Move = 1
K=5 26 31 41 41 58 59 Which is sorted

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 16


Insertion Sort
• Example 2: Sort A = (4, 3, 2, 10, 12, 1, 5, 6).

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 17


Insertion Sort: Complexity
• if we provide an already sorted array to the insertion sort algorithm,
it will still execute the outer for loop, thereby requiring n steps to
sort an already sorted array of n elements, which makes its best case
time complexity a linear function of n.
• Wherein for an unsorted array, it takes for an element to compare
with all the other elements which mean every n element compared
with all other n elements. Thus, making it for n x n, i.e., n2
comparisons.
• Worst Case Time Complexity: O(n2)
• Best Case Time Complexity: O(n)
• Average Time Complexity: O(n2)
• Space Complexity: O(1) [only requires a constant amount O(1) of
additional memory space]

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 18


Insertion Sort: Characteristics
• It is efficient for smaller data sets, but very inefficient for larger
lists.
• Insertion Sort is adaptive, that means it reduces its total number of
steps if a partially sorted array is provided as input, making it
efficient.
• It is better than Selection Sort and Bubble Sort algorithms.
• Its space complexity is less. Like bubble Sort, insertion sort also
requires a single additional memory space.
• It is stable since it does not change the relative order of elements
with equal keys

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 19


Selection Sort
• Selection sort is conceptually the simplest sorting algorithm.
• This algorithm will first find the smallest element in the array and
swap it with the element in the first position,
• then it will find the second smallest element and swap it with the
element in the second position, and
• it will keep on doing this until the entire array is sorted.
• It is called selection sort because it repeatedly selects the next-
smallest element and swaps it into the right place.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 20


Selection Sort

Source: https://www.happycoders.eu/algorithms/selection-sort/

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 21


Selection Sort
• Following are the steps involved in selection sort(for sorting a given
array in ascending order):
1. Starting from the first element, we search the smallest element
in the array, and replace it with the element in the first
position.
2. We then move on to the second position, and look for smallest
element present in the subarray, starting from index 1, till the
last index.
3. We replace the element at the second position in the original
array, or we can say at the first position in the subarray, with
the second smallest element.
4. This is repeated, until the array is completely sorted.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 22


Selection Sort

• Algorithm:
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat steps 2, 3 and 4 until list is sorted

Selection sort animation.


Red is current min.
Yellow is sorted list.
Blue is current item.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 23


Selection Sort

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 24


Selection Sort
void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray


for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

//Swap the found minimum element with the first element


swap(&arr[min_idx], &arr[i]);
}
}

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 25


Selection Sort
• Example:

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 26


Selection Sort
• Example:

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 27


Selection Sort
• Selection Sort requires two nested loops to complete itself, one for
loop is in the function selectionSort, and inside the first loop we are
making a call to another function indexOfMinimum, which has the
second (inner) for loop.
• Hence for a given input size of n, following will be the time and
space complexity for selection sort algorithm:
• Worst Case Time Complexity: O(n2)
• Best Case Time Complexity: O(n2)
• Average Time Complexity: O(n2)
• Space Complexity: O(1)

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 28


Selection Sort: Characteristics
• Selection Sort is an easy-to-implement and in its typical
implementation unstable, sorting algorithm with an average, best-
case, and worst-case time complexity of O(n²).
• Selection Sort is slower than Insertion Sort, which is why it is rarely
used in practice.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 29


Bubble Sort
• Bubble sort, sometimes referred to as sinking sort, is a simple sorting
algorithm that repeatedly steps through the list, compares adjacent
elements and swaps them if they are in the wrong order.
• The pass through the list is repeated until the list is sorted.
• The algorithm works by comparing each item in the list with the
item next to it, and swapping them if required. In other words, the
largest element has bubbled to the top of the array.
• The algorithm repeats this process until it makes a pass all the way
through the list without swapping any items.
• Sorting takes place by stepping through all the elements one-by-one
and comparing it with the adjacent element and swapping them if
required.
• It is a stable sort.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 30


Bubble Sort

An example of bubble sort. Starting from the beginning of the list, compare every
adjacent pair, swap their position if they are not in the right order (the latter one is
smaller than the former one). After each iteration, one less element (the last one) is
needed to be compared until there are no more elements left to be compared.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 31


Bubble Sort
• If the given array has to be sorted in ascending order, then bubble
sort will start by comparing the first element of the array with the
second element, if the first element is greater than the second
element, it will swap both the elements, and then move on to
compare the second and the third element, and so on.
• If we have total n elements, then we need to repeat this process for
n-1 times.
• It is known as bubble sort, because with every complete iteration
the largest element in the given array, bubbles up towards the last
place or the highest index, just like a water bubble rises up to the
water surface.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 32


Bubble Sort
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 33


Bubble Sort
Example: Using bubble sort, Sort the following array =
{7, 5, 2, 4, 3, 9}

7, 5, 2, 4, 3, 9 5, 2, 4, 3, 7, 9 2, 4, 3, 5, 7, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9
5, 7, 2, 4, 3, 9 2, 5, 4, 3, 7, 9 2, 4, 3, 5, 7, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9
5, 2, 7, 4, 3, 9 2, 4, 5, 3, 7, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9 5th Pass
5, 2, 4, 7, 3, 9 2, 4, 3, 5, 7, 9 2, 3, 4, 5, 7, 9 4th Pass
5, 2, 4, 3, 7, 9 2, 4, 3, 5, 7, 9 3rd Pass
5, 2, 4, 3, 7, 9 2nd Pass
1st Pass

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 34


Bubble Sort
Example: Using bubble sort,
Sort the following array =
{5, 3, 1, 9, 8, 2, 4, 7}

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 35


Bubble Sort
• Worst Case Time Complexity: O(n2)
• Best Case Time Complexity: O(n)
• Average Time Complexity: O(n2)
• Space Complexity: O(1)

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 36


Exchange Sort
• The exchange sort is almost similar as the bubble sort.
• The exchange sort compares each element of an array and swap
those elements that are not in their proper position, just like a
bubble sort does.
• The only difference between the two sorting algorithms is the
manner in which they compare the elements.
• The exchange sort compares the first element with each element of
the array, making a swap where is necessary.
• In some situations the exchange sort is slightly more efficient than
its counterpart- bubble sort.
• The bubble sort needs a final pass to determine that it is finished,
thus is slightly less efficient than the exchange sort, because the
exchange sort doesn’t need a final pass.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 37


Exchange Sort
Example: Using exchange sort, Sort the following array =
{7, 5, 2, 4, 3, 9}

7, 5, 2, 4, 3, 9 2, 7, 5, 4, 3, 9 2, 3, 7, 5, 4, 9 2, 3, 4, 7, 5, 9 2, 3, 4, 5, 7, 9
5, 7, 2, 4, 3, 9 2, 5, 7, 4, 3, 9 2, 3, 5, 7, 4, 9 2, 3, 4, 5, 7, 9 2, 3, 4, 5, 7, 9
2, 7, 5, 4, 3, 9 2, 4, 7, 5, 3, 9 2, 3, 4, 7, 5, 9 2, 3, 4, 5, 7, 9 5th Pass
2, 7, 5, 4, 3, 9 2, 3, 7, 5, 4, 9 2, 3, 4, 7, 5, 9 4th Pass
2, 7, 5, 4, 3, 9 2, 3, 7, 5, 4, 9 3rd Pass
2, 7, 5, 4, 3, 9 2nd Pass
1st Pass

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 38


Exchange Sort

//test by replacing the > by < in the condition

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 39


Class Discussion
Bubble Sort Exchange Sort
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 40


Quick Sort
• Quick sort is a fast sorting algorithm- avg running time:
O(n log n)
• Quick sort algorithm is invented by C. A. R. (Tony)
Hoare.
• It is based on Divide-and-Conquer approach that uses
recursion.
• The quick sort algorithm follows three main steps:
• Pick an element from the array as a pivot
• Partition the problem set by moving smaller
elements to the left of the pivot and larger
elements to its right
• Repeat the above steps on each partition

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 41


Quick Sort
• In quick sort, the partition of the list is performed based on the
element called pivot.
• Here pivot element is one of the elements in the list.
• The list is divided into two partitions such that "all elements to the
left of pivot are smaller than the pivot and all elements to the right
of pivot are greater than or equal to the pivot”.
• There are various ways to choose the pivot element:
• Chose pivot as first element.
• Chose pivot as last element.
• Chose pivot as median element.
• Chose pivot as random element.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 42


Quick Sort
• Let’s take a look at the following illustrations to understand the
approach better:

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 43


Quick Sort

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 44


Quick Sort

https://gfycat.com/pleasantcloseeyelashpitviper

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 45


Quick Sort
• Example: taking the first element as a pivot element

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 46


Quick Sort
• Example: taking the first element as a pivot element

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 47


Quick Sort
• Example: taking the first element as a pivot element

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 48


Quick Sort
• Example: taking the first element as a pivot element
• At the point where rightmark becomes less than leftmark, we stop.
The position of rightmark is now the split point. The pivot value can be
exchanged with the contents of the split point and the pivot value is now
in place

• The list can now be divided at the split point and the quick sort can be
invoked recursively on the two halves.
Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 49
Quick Sort
Algorithm:
1. Consider the first element of the array as pivot.
2.Define two variables left and right. Set left and right
to first and last elements of the array respectively.
3.Increment left until array[left] > pivot then stop.
4.Decrement right until array[right] < pivot then stop.
5.If left < right then exchange array[left] and
array[right].
6.Repeat steps 3,4 & 5 until left > right.
7.Exchange the pivot element with array[right] element.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 50


Quick Sort

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 51


Quick Sort

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 52


Quick Sort
• Quick Sort uses divide and conquer technique like merge sort, but
does not require additional storage space.
• It is one of the most famous comparison based
sorting algorithm which is also called as partition exchange sort.
• Like merge sort, it also uses recursive call for sorting elements.
• Complexity:

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 53


Quick Sort: Complexity
• For an array, in which partitioning leads to unbalanced subarrays,
then the running time is the worst case, which is O(n2)
• Where as if partitioning leads to almost equal subarrays, then the
running time is the best, with time complexity as O(n*log n).
• Time Complexity:
• Worst Case: O(n2)
• Best Case: O(n log n)
• Average: O(n log n)
• Space Complexity:
• Average: O(n log n)
• Quick sort is unstable sort.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 54


Merge Sort
• Merge sort is one of the most efficient sorting algorithms.
• With worst-case, time complexity is Ο(n log n)
• It works on the principle of Divide and Conquer algorithm.
• Working principle:
• 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 the smaller sorted lists keeping the
new list sorted too.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 55


Merge Sort

An example of merge sort. First divide the list into the smallest
unit (1 element), then compare each element with the adjacent
list to sort and merge the two adjacent lists. Finally all the
elements are sorted and merged.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 56


Merge Sort: Example 1

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 57


Merge Sort: Example 2
Step 1: Divide the unsorted list into n sublists, each comprising 1 element

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 58


Merge Sort: Example 2
Step 2: Repeatedly merge sublists to produce newly sorted sublists until there is
only 1 sublist remaining.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 59


Merge Sort
Algorithm:
//arr = array, l = left index, m = middle index, r = right index
mergeSort(arr[], l, r)
{
If r > l
1. Find the middle point to divide the array into two halves:
middle m = l+ (r-l)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
}

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 60


Merge Sort

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 61


Merge Sort

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 62


Merge Sort: Complexity

Case Time Complexity

Best Case O(n*logn)

Average Case O(n*logn)

Worst Case O(n*logn)

Space Complexity: O (n)


Merge sort is stable sort.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 63


Radix Sort
• Radix sort is an integer sorting algorithm.
• Radix sorts data with integer values by grouping the values by
individual digits that share the same significant position and value.
• Radix sort uses counting sort as a subroutine to sort an array of
numbers on each significant position.
• Radix sort works on data types other than just integers.
• Radix sort takes in a list of n integers which are in base b (the radix)
and so each number has at most d digits where d = ⌊logb(k) + 1⌋
and k is the largest number in the list.
• For example, 3 digits are needed to represent 104 (in base 10).
• It is important that radix sort can work with any base

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 64


Radix Sort
• Radix sort works by sorting each digit from least significant digit to
most significant digit.
• So in base 10 system, radix sort would sort by the digits in the 1's
place, then the 10’s place, and so on.
• To do this, radix sort uses counting sort as a subroutine to sort the
digits in each place value.
• This means that for a three-digit number in base 10, counting
sort will be called to sort the 1's place, then it will be called to
sort the 10's place, and finally, it will be called to sort the 100's
place, resulting in a completely sorted list.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 65


Radix Sort
• Example: Sort the array = {329, 457, 657, 839, 436, 720, 355} using
radix sort.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 66


Radix Sort
• Example: Sort the array = {121, 432, 564, 23, 1, 45, 788} using radix
sort.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 67


Radix Sort
The Radix Sort Algorithm:

• Do following for each digit i where i varies from least significant


digit to the most significant digit.

• Sort input array using counting sort (or any stable sort)
according to the ith digit.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 68


Radix Sort: Complexity
• Radix sort will operate on n d-digit numbers where each digit can be one
of at most b different values (since b is the base being used).

• For example, in base 10, a digit can be 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.

• Radix sort uses counting sort on each digit.

• Each pass over n d-digit numbers will take O(n+b) time, and there are d
passes total.

• Therefore, the total running time of radix sort is O(d(n+b)).

• When d is a constant and b isn't much larger than n (in other words,
b=O(n)), then radix sort takes linear time.

• Radix sort is stable sort.

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 69


Shell Sort
• Assignment for you.
• (Hints:https://www.youtube.com/watch?v=408TQi6MWmI and https://
www.youtube.com/watch?v=1yDcmjLTWOg )

https://stackoverflow.com/questions/4833423/shell-sort-java-example

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 70


Heap Sort
• Already studied.

https://commons.wikimedia.org/wiki/File:Heap_sort_example.gif
Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 71
Efficiency of Sort Algorithms

Dr. Udaya R. Dhungana, Pokhara University, Nepal. [email protected] 72


THANK YOU

End of the Chapter

73

You might also like