Professional Documents
Culture Documents
[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.
1
2/23/2023
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
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
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
10
5
2/23/2023
11
11
Question: 12
12
6
2/23/2023
Question: 13
13
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
17
18
9
2/23/2023
19
19
20
20
10
2/23/2023
21
22
11
2/23/2023
23
23
24
12
2/23/2023
25
26
26
13
2/23/2023
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