Sorting Part 1 Without Code
Sorting Part 1 Without Code
Lecture Flow
1) Pre-requisites
2) Problem Definitions and Applications
3) Different approaches
4) Bubble sort
5) Selection sort
6) Insertion sort
7) Counting sort
8) Practice questions
9) Resources
10) Quote of the day
Pre-requisites
● Basic python
● Asymptotic Analysis
● Arrays
Problem Definition
Real Life Example
Playing Cards
What is Sorting?
?
Sorting by Distribution
Sorting by Distribution
Maintains the original order of similar Does not preserve the initial arrangement
items after sorting. of exact items after sorting.
Stable Sorting Vs Unstable Sorting
In-Place vs Out-of-Place
In-Place Out-of-Place
Uses constant space by modifying the Uses extra space to modify order of
order of the elements within the list. elements.
Efficiency of Sorting Algorithm
● The complexity of a sorting algorithm measures the running time of a function
in which n number of items are to be sorted.
● Various sorting algorithms are analyzed in the cases like - Best case, Worst
case or Average case.
Comparison Sorting
1. Bubble Sort
Bubble Sort
Swap
Compare Elements
First Pass Done
?
Compare The Elements and Swap
Compare elements
Compare The Elements
Compare elements
Done!
Compare elements
Compare The Elements
Compare elements
Done!
Compare elements
Done!
?
Q: What happens when we put the smallest at the end?
?
Selection Sort Simulation
The same process is applied to the rest of the items in the array.
After two iterations, the two least value is positioned at the beginning in a sorted
manner.
The same process is applied to the rest of the items in the array.
After three iterations, the three least value is positioned at the beginning in a sorted
manner.
The same process is applied to the rest of the items in the array.
After four iterations, the four least value is positioned at the beginning in a sorted
manner.
The same process is applied to the rest of the items in the array.
Done!
Algorithm
Move the blue record to the left until it reaches the correct position.
Insertion Sort Simulation
Move the blue record to the left until it reaches the correct position.
Swap
Insertion Sort Simulation
Move the blue record to the left until it reaches the correct position.
Insertion Sort Simulation
?
Insertion Sort
Algorithm
● Compare the current element with all elements in the sorted array
● Shift all the the elements in sorted sub-list that is greater than the
value to be sorted.
Phase 1: Counting
First, a storage array is created whose length corresponds to the number range.
Then you iterate once over the elements to be sorted, and, for each element,
you increment the value in the array at the position corresponding to the
element.
Consider the following array as an example.
Phase 2: Rearranging
We iterate once over the histogram array. We write the respective array
index into the array to be sorted as often as the histogram indicates at the
corresponding position.
In the example, we start at position 0 in the auxiliary array. That field contains
a 1, so we write the 0 exactly once into the array to be sorted.
At position 1 in the histogram, there is a 0, meaning we skip this field – no 1 is
written to the array to be sorted.
Position 2 of the histogram again contains a 1, so we write 2 once into the
array to be sorted
We come to position 3, which contains a 3; so we write three times a 3 into
the array
And so it goes on. We write 4 once, 6 five times, 7 once, 8 twice and finally 9
once into the array to be sorted
Solve by Using Counting Sort
Count Sorting with Negative Numbers
The problem with the previous counting sort was that we could not sort the
elements if we have negative numbers in them. Because there are no
negative array indices.
Time complexity
Note: Counting sort is most efficient if the range of input values is not greater
than the number of values to be sorted.
● Any improvement in sorting time significantly affect
computer time.
Time Complexity
Average
Best Case Case Worst Case Space Stable In Place
Link
Using built-in functions to sort
Python libraries: sorted() and .sort()
array = [1,2,5,4,3,6]
array.sort(reverse=True)
print(array)
# 6 5 4 3 2 1
Writing custom comparator
Default sorting sorts based on the values
studentToCost = {}
for idx in range(len(students)):
studentToCost[students[idx]] = costs[idx]
def customComparator(item):
return studentToCost[item]
students.sort(key = customComparator)
print(students)
# [1, 5, 3, 2, 6, 4]
Selecting a sorting algorithm
Language libraries often have sorting algorithms so we won’t be coding our own
sorting algorithms every time.
● Insertion Sort
● Selection Sort
● Counting Sort
● Visualizations
Practice Questions
Bubble Sort
Insertion Sort
Counting Sort
Selection Sort
Sort Colors
Pancake Sorting
Find Target Indices After Sorting Array
Maximum Number Of Coins You Can Get
How Many Numbers Are Smaller Than The Current Number