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

G1-Sorting Algorithms

The document discusses different sorting algorithms including bubble sort, selection sort, insertion sort, merge sort, and quick sort. It provides descriptions of how each algorithm works and its time and space complexity.

Uploaded by

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

G1-Sorting Algorithms

The document discusses different sorting algorithms including bubble sort, selection sort, insertion sort, merge sort, and quick sort. It provides descriptions of how each algorithm works and its time and space complexity.

Uploaded by

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

CSA201 – Applied Data Structures and

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?

• Sorting is an incredibly common task, so it's good to know how it works


• There are many different ways to sort things, and different techniques have
their own advantages and disadvantages
• Sorting sometimes has quirks, so it's good to understand how to navigate them

3
Types of Sorting Algorithms

Sorting

Space Used Stability

In place Out of place Stable Unstable

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

Time complexity: O(N2)


Space complexity: O(1)
12
Selection Sort - Introduction
• Selection sort is one of the simplest sorting algorithms.
• It is similar to the hand picking where we take the smallest element and put it
in the first position and the second smallest at the second position and so on.
• We first check for smallest element in the list and swap it with the first
element of the list.
• Again, we check for the smallest number in a sublist, excluding the first
element of the list as it is where it should be (at the first position) and put it in
the second position of the list. We continue repeating this process until the
list gets sorted.

13
Selection Sort – Working

14
Selection Sort – Function

Time complexity: O(N2)


Space complexity: O(1)
15
Insertion Sort - Introduction

• 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

Time complexity: O(N2)


Space complexity: O(1)
18
Merge Sort - Introduction

• Merge sort is a divide and conquer algorithm


• Divide the input array in two halves and we keep halving recursively until they
become too small that cannot be broken further
• Merge halves by sorting them

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

Time complexity: O(NlogN)


Space complexity: O(N)
25
Merge Sort - Function

26
Merge Sort - Function

27
Quick Sort - Introduction

• Quick sort is a divide and conquer algorithm


• Find pivot number and make sure smaller numbers located at the left of pivot
and bigger numbers are located at the right of the pivot
• Unlike merge sort extra space is not required

• 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

Time complexity: O(NlogN)


Space complexity: O(N)
32
Quick Sort - Function

33

You might also like