You are on page 1of 15

2/23/2023

[2]
SORTING
SORTING ALGORITHMS ARE A METHOD OF ORGANIZING
DATA IN A CERTAIN ORDER AND CAN BE USED TO
ORGANIZE MESSY DATA TO BE EASIER TO USE.
THEREFORE, DEVELOPING A STRONG UNDERSTANDING
OF SORTING ALGORITHMS AND HOW THEY WORK IS A
CRUCIAL FUNDAMENTAL OF COMPUTER SCIENCE.

[I] Bubble Sort Algorithm 2

 Bubble Sort is the simplest sorting algorithm that


works by repeatedly swapping the adjacent
elements if they are in the wrong order.
 This algorithm is not suitable for large data sets as
its average and worst-case time complexity is
quite high.

1
2/23/2023

How does Bubble Sort Work? 3

 Input: arr[] = {5, 1, 4, 2, 8}


 First Pass:
 Bubble
sort starts with very first two elements,
comparing them to check which one is greater.
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first
two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are
already in order (8 > 5), algorithm does not swap them.

How does Bubble Sort Work? 4

 Second Pass:
 Now, during second iteration it should look like
this:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

2
2/23/2023

How does Bubble Sort Work? 5

 Third Pass:
 Now, the array is already sorted, but our algorithm
does not know if it is completed.
 The algorithm needs one whole pass without any
swap to know it is sorted.
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
 ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
 ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

3
2/23/2023

Time Complexity: O(N2)


Auxiliary Space: O(1)

Optimized Implementation of 8

Bubble Sort
 The above function always runs O(N2) time even if
the array is sorted. It can be optimized by stopping
the algorithm if the inner loop didn’t cause any
swap.
 Worst and Average Case Time Complexity: O(N2).
The worst case occurs when an array is reverse
sorted.
 Best Case Time Complexity: O(N). The best case
occurs when an array is already sorted.
 Auxiliary Space: O(1)

4
2/23/2023

Time Complexity: O(N2)


Auxiliary Space: O(1)

Recursive Bubble Sort 10

 Recursive Bubble Sort has no performance/implementation


advantages, but can be a good question to check one’s
understanding of Bubble Sort and recursion.
 If we take a closer look at Bubble Sort algorithm, we can
notice that in first pass, we move largest element to end
(Assuming sorting in increasing order). In second pass, we
move second largest element to second last position and so
on.
 Recursion Idea.
 Base Case: If array size is 1, return.
 Do One Pass of normal Bubble Sort. This pass fixes last element of
current subarray.
 Recur for all elements except last of current subarray.

10

5
2/23/2023

11

•Time Complexity: O(n*n)


•Auxiliary Space: O(n)

11

Question: 12

 1. Difference between iterative and recursive bubble


sort?
 Ans. Recursive bubble sort runs on O(n) auxiliary space
complexity whereas iterative bubble sort runs on O(1)
auxiliary space complexity.
 2. Which is faster iterative or recursive bubble sort?
 Ans. Based on the number of comparisons in each
method, the recursive bubble sort is better than the
iterative bubble sort, but the time complexity for both
the methods is same.

12

6
2/23/2023

Question: 13

 3. Which sorting method we should prefer more iterative


or recursive bubble sort?
 Ans. Both the methods complete the computation at
the same time(according to time complexity analysis)
but iterative code takes less memory than recursive one,
so we should prefer iterative bubble sort more than
recursive bubble sort.

13

Bubble Sort Algorithm 14

Summary:
 Advantages:
 Bubble sort is easy to understand and implement.
 It does not require any additional memory space.
 It’s adaptability to different types of data.
 Disadvantages
 Bubble sort has a time complexity of O(n^2) which makes it very
slow for large data sets.
 It is not efficient for large data sets, because it requires multiple
passes through the data.
 It is not a stable sorting algorithm, meaning that elements with the
same key value may not maintain their relative order in the sorted
output.

14

7
2/23/2023

15
[ii] Insertion Sort
 Insertion sort is a simple sorting algorithm that works
similar to the way you sort playing cards in your
hands. The array is virtually split into a sorted and
an unsorted part. Values from the unsorted part
are picked and placed at the correct position in
the sorted part.
 Characteristics of Insertion Sort:
 This algorithm is one of the simplest algorithm
with simple implementation
 Basically, Insertion sort is efficient for small data
values
 Insertion sort is adaptive in nature, i.e. it is
appropriate for data sets which are already
partially sorted.

2/23/2023

15

16

16

8
2/23/2023

17

Time Complexity: O(N^2)


Auxiliary Space: O(1)

17

What is Binary Insertion Sort? 18

 We can use binary search to reduce the number of


comparisons in normal insertion sort. Binary Insertion
Sort uses binary search to find the proper location to
insert the selected item at each iteration. In normal
insertion, sorting takes O(i) (at ith iteration) in worst
case.
 We can reduce it to O(logi) by using binary search.
The algorithm, as a whole, still has a running worst
case running time of O(n^2) because of the series of
swaps required for each insertion.

18

9
2/23/2023

19

19

20

20

10
2/23/2023

[iii] Selection Sort Algorithm 21

 Selection sort is a simple and efficient sorting


algorithm that works by repeatedly selecting the
smallest (or largest) element from the unsorted
portion of the list and moving it to the sorted portion
of the list.
 The algorithm repeatedly selects the smallest (or
largest) element from the unsorted portion of the list
and swaps it with the first element of the unsorted
portion. This process is repeated for the remaining
unsorted portion of the list until the entire list is sorted.

21

Selection Sort Algorithm 22

 One variation of selection sort is called


“Bidirectional selection sort” that goes through
the list of elements by alternating between the
smallest and largest element, this way the
algorithm can be faster in some cases.
 The algorithm maintains two subarrays in a given
array.
 The subarray which already sorted.
 The remaining subarray was unsorted.

22

11
2/23/2023

23

23

Selection Sort Algorithm 24

 In every iteration of the selection sort, the


minimum element (considering ascending
order) from the unsorted subarray is picked and
moved to the beginning of unsorted subarray.
 After every iteration sorted subarray size increase
by one and unsorted subarray size decrease by
one.

24

12
2/23/2023

Selection Sort Algorithm 25

 Follow the below steps to solve the problem:


 Initialize minimum value(min_idx) to location 0.
 Traverse the array to find the minimum element in the
array.
 Whiletraversing if any element smaller than min_idx is
found then swap both the values.
 Then, increment min_idx to point to the next element.
 Repeat until the array is sorted.

25

26

Time Complexity: The time complexity of Selection Sort is


O(N2) as there are two nested loops:
•One loop to select an element of Array one by one = O(N)
•Another loop to compare that element with every other Array
element = O(N)
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N2)
Auxiliary Space: O(1) as the only extra memory used is for
temporary variables while swapping two values in Array. The
selection sort never makes more than O(N) swaps and can be
useful when memory write is a costly operation.

26

13
2/23/2023

Selection Sort Algorithm 27

Summary:
 Selection sort is a simple and easy-to-understand sorting algorithm
that works by repeatedly selecting the smallest (or largest) element
from the unsorted portion of the list and moving it to the sorted
portion of the list.
 This process is repeated for the remaining unsorted portion of the list
until the entire list is sorted.
 It has a time complexity of O(n^2) in the worst and average case
which makes it less efficient for large data sets.
 Selection sort is a stable sorting algorithm.
 It can be used to sort different types of data.
 It has specific applications where it is useful such as small data sets
and memory-constrained systems.

27

28

28

14
2/23/2023

29

29

30

30

15

You might also like