G1-Sorting Algorithms
G1-Sorting Algorithms
Algorithms
Unit 7 Sorting Algorithms
1
What is sorting?
• Sorting is the process of rearranging the items in a collection (e.g. an array) so
that the items are in some kind of order (either ascending or descending).
• Examples
• Sorting numbers from smallest to largest
• Sorting names alphabetically
• Sorting movies based on release year
• Sorting movies based on revenue
2
Why Study Sorting Algorithms?
3
Types of Sorting Algorithms
Sorting
4
Space Used
• In place sorting: Sorting algorithms which does not require any extra space for
sorting (Example: Bubble Sort, Selection Sort)
70 10 80 30 20 40 60 50 90
10 20 30 40 50 60 70 80 90
5
Space Used
• Out place sorting: Sorting algorithms which requires an extra space for sorting
(Example: Merge Sort)
70 10 80 30 20 40 60 50 90
10 20 30 40 50 60 70 80 90
6
Stability
• Stable sorting: If a sorting algorithm after sorting the values does not change
the sequence of similar values in which they appear, then this sorting is called
stable sorting (Example: Insertion Sort)
70 10 80 40 20 40 60 50 90
10 20 40 40 50 60 70 80 90
7
Stability
• Unstable sorting: If a sorting algorithm after sorting the values changes the
sequence of similar values in which they appear, then this sorting is called
unstable sorting (Example: Quick Sort)
70 10 80 40 20 40 60 50 90
10 20 40 40 50 60 70 80 90
8
Bubble Sort - Introduction
• Bubble Sort is a simple algorithm which is used to sort a given set of n elements
provided in the form of an array with n number of elements.
• Bubble Sort compares all the element one by one and sort them based on their
values.
• 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.
• In every iteration, the largest element in the array bubbles up towards the last
place.
9
Bubble Sort - Algorithm
• Start iterating over the array.
• Compare the adjacent elements. For example, the first and second, second
and third, etc.
• Swap them if they are not in order.
• Repeat these steps except for the elements which are placed at their correct
positions.
10
Bubble Sort - Working
11
Bubble Sort - Function
13
Selection Sort – Working
14
Selection Sort – Function
• Insertion sort works similarly as we sort cards in our hand in a card game.
• We assume that the first card (index 0) is already sorted (start the index from
1) then, we select an unsorted card.
• If the unsorted card is greater than the card in hand, it is placed on the right
otherwise, to the left.
• Insertion sort is a sorting algorithm that places an unsorted element at its
suitable place in each iteration.
16
Insertion Sort - Working
17
Insertion Sort - Function
19
Merge Sort – Divide and Conquer Algorithm
• Divide/Break
• If q is the half-way point between p and q, then we can split the subarray A[p...r] into two
arrays A[p...q] and A[q+1...r]
• Conquer/Solve
• In the conquer step, we try to sort both the subarrays A[p..q] and A[q+1,....r]. If we haven’t
yet reached the base case, we again divide both these subarrays and try to sort them.
• Merge/Combine
• When the conquer step reaches the base step and we get two sorted subarrays A[p..q] and
A[q+1, r] for array A[p..r], we combine the results by creating a sorted array A[p..r] from two
sorted subarrays A[p..q] and A[q+1, r].
20
Merge Sort - Divide
21
Merge Sort - Conquer
22
Merge Sort – How to merge two sorted arrays?
23
Merge Sort – How to merge two sorted arrays?
24
Merge Sort - Function
26
Merge Sort - Function
27
Quick Sort - Introduction
• Quick sort algorithm depends upon the selection of pivot. Generally, there are
4 major ways of choosing a pivot:
• Picking the first element as the pivot
• Picking the last element of the array as the pivot
• Picking a random
• Picking median
28
Quick Sort - Introduction
• Divide: Partition (rearrange) the array A[p...r] into two (possibly empty)
subarray A[p..q-1] and A[q+1..r] such that each element of A[q..q-1] is less
than or equal to A[q], which is, in turn, less than or equal to each element of
A[q+1..r]. Compute the index q as part of this partitioning procedure.
• Conquer: Sort the two subarrays A[p..q-1] and A[q+1..r] by recursive calls to
quicksort.
• Combine: Because the subarrays are already sorted, no work is needed to
combine then: the entire array A[p..r] is now sorted.
29
Quick Sort – How Quick Sorting Works?
• After selecting the pivot element (last element in our case), we divide the array
for the first time.
• In QS, we call this partitioning. It is not simple breaking down of array into 2
subarrays, but in case of partitioning, the array elements less than pivot are
positioned on the left side and elements greater than pivot are positioned on
the right side of it.
• By doing so, the pivot element will be at its final sorted position. The elements
to the left and right, may not be sorted.
• Then we pick subarrays, elements on the left of pivot and elements on the
right of pivot, and we perform partitioning on them by choosing a pivot in the
subarrays.
30
Quick Sort - Working
31
Quick Sort - Function
33