Sorting Algorithms
Sorting Algorithms
Algorithm
Muhammad Umer Mehmood
Sorting Algorithms
Insertion Sort Algorithm
Insertion Sort
Insertion Sort
1 2 3 4 5 6
5 2 4 6 1 3
Insertion Sort
1 2 3 4 5 6
5 2 4 6 1 3
Insertion Sort
1 2 3 4 5 6
5 2 4 6 1 3
Insertion Sort
1 2 3 4 5 6
2 5 4 6 1 3
Insertion Sort
1 2 3 4 5 6
2 4 5 6 1 3
Insertion Sort
1 2 3 4 5 6
2 4 5 6 1 3
Insertion Sort
1 2 3 4 5 6
2 4 5 6 1 3
Insertion Sort
1 2 3 4 5 6
1 2 4 5 6 3
Insertion Sort
1 2 3 4 5 6
1 2 3 4 5 6
Insertion Sort
Insertion Sort Complexity
Time Complexity
Best 𝑂 𝑛
Worst 𝑂 𝑛2
Average 𝑂 𝑛2
Space Complexity 𝑂 1
Stability 𝑌𝑒𝑠
Loop Invariants and the Correctness of
Insertion Sort
• This algorithm works for A (5, 2, 4, 6, 1, 3).
• The index j indicates the “current card” being inserted into the hand.
• At the beginning of each iteration of the for loop, which is indexed by
j, the subarray consisting of elements A[1..j-1] constitutes the
currently sorted hand,
• the remaining subarray A[j+1..n] corresponds to the pile of cards still
on the table.
Loop Invariants and the Correctness of
Insertion Sort
• Initialization: It is true prior to the first iteration of the loop.
• Maintenance: If it is true before an iteration of the loop, it remains
true before the next iteration.
• Termination: When the loop terminates, the invariant gives us a
useful property that helps show that the algorithm is correct
Initialization
• The loop invariant holds before the first loop iteration when:
• J = 2. The subarray A[1 … j-1],
• Therefore, consist of just a single element A[1].
Maintenance
Showing that each iteration maintains the loop invariant. Informally,
the body of the for loop works by:
• A[j - 1], A[j - 2], A[j - 3]
so on by one position to the right until it finds the proper position for
A[j]
Termination
• Finally examine what happens when the loop terminates.
• The condition causing the for loop to terminate is that j > A.length =
n.
• Each iteration increases j by 1, which means j = n+1.
• Sub Array A[1….n] consist of the elements originally in A[1….n] in
sorted order.
Bubble Sort Algorithm
Bubble Sort
Just like the way bubbles rise from the bottom of a glass,
Bubble sort is a simple algorithm that sorts a list, allowing either lower
or higher values to bubble up to the top
Bubble Sort
The algorithm is called bubble sort because when sorting a list from
lowest to highest value, the highest values are moved up the list, which
some people imagine as bubbles in a fizzy drink rising to the top.
• Each time the algorithm goes through the list, it is called a pass
• Each time a pair of items are checked to see if they are in the correct
order, it is called a comparison
Bubble Sort
Example:
9 3 10 2 8 5 1
Bubble Sort
Example:
9 3 10 2 8 5 1
Bubble Sort
Example:
3 9 10 2 8 5 1
Bubble Sort
Example:
3 9 2 8 5 1 10
Bubble Sort
Example:
3 2 8 5 1 9 10
Bubble Sort
Example:
3 2 5 1 8 9 10
Bubble Sort
Example:
3 2 1 5 8 9 10
Bubble Sort
Example:
2 1 3 5 8 9 10
Bubble Sort
Example:
1 2 3 5 8 9 10
Bubble Sort-practice examples
• Step 1: Take a list of data to be sorted
• Step 2: Repeat step 3 (a pass) until no swaps are made:
• Step 3: Repeat steps 4–6 for all the consecutive pairs of items in the list,
starting from the first item:
• Step 4: Compare the item at the current position to the item after it (a comparison)
• Step 5: If the item at the current position is greater than the one next to it, swap the
items within the list
• Step 6: Go to the next item in the list
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 15 25 13 29 18
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 15 25 13 29 18
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 15 25 13 29 18
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 15 13 25 29 18
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 15 13 25 29 18
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 15 13 25 29 18
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 15 13 25 18 29
Sorting Number into Ascending Order
items
Index 0 1 2 3 4
value 13 15 18 25 29
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Sydney Salt Lake Athens Turin Beijing Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Sydney Salt Lake Athens Turin Beijing Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Sydney Athens Turin Beijing Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Sydney Athens Turin Beijing Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Athens Sydney Turin Beijing Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Athens Sydney Turin Beijing Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Athens Sydney Turin Beijing Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Athens Sydney Beijing Turin Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Athens Sydney Beijing Turin Vancouver London
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Athens Sydney Beijing Turin London Vancouver
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Salt Lake Athens Sydney Beijing Turin London Vancouver
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Athens Salt Lake Sydney Beijing London Turin Vancouver
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Athens Beijing Salt Lake London Sydney Turin Vancouver
City
Sorting words into ascending order
Items
Index 0 1 2 3 4 5 6
values Athens Beijing London Salt Lake Sydney Turin Vancouver
City
Pseudocode
bubbleSort(array)
swapped <- false
for i <- 1 to indexOfLastUnsortedElement - 1
if leftElement > rightElement
swap leftElement and right Element
swapped <- true
end bubbleSort
Bubble Sort Complexity
Time Complexity
Best 𝑂 𝑛
Worst 𝑂 𝑛2
Average 𝑂 𝑛2
Space Complexity 𝑂 1
Stability 𝑌𝑒𝑠
Complexity in detail
Items
Index 0 1 2 3 4 5 6
values 9 3 10 2 8 5 1
Items
Index 0 1 2 3 4 5 6
values 9 3 10 2 8 5 1
0 9 3 9 10 2 8 5 1
Items
Index 0 1 2 3 4 5 6
values 9 3 10 2 8 5 1
0 9 3 9 10 2 8 5 1
1 3 9 10 2 8 5 1
Items
Index 0 1 2 3 4 5 6
values 9 3 10 2 8 5 1
0 9 3 9 10 2 8 5 1
1 3 9 10 2 8 5 1
2 10 3 9 2 10 8 5 1
Items
Index 0 1 2 3 4 5 6
values 9 3 10 2 8 5 1
0 9 3 9 10 2 8 5 1
1 3 9 10 2 8 5 1
2 10 3 9 2 10 8 5 1
3 10 3 9 2 8 10 5 1
Items
Index 0 1 2 3 4 5 6
values 9 3 10 2 8 5 1
0 9 3 9 10 2 8 5 1
1 3 9 10 2 8 5 1
2 10 3 9 2 10 8 5 1
3 10 3 9 2 8 10 5 1
4 10 3 9 2 8 5 10 1
Items
Index 0 1 2 3 4 5 6
values 9 3 10 2 8 5 1
0 9 3 9 10 2 8 5 1
1 3 9 10 2 8 5 1
2 10 3 9 2 10 8 5 1
3 10 3 9 2 8 10 5 1
4 10 3 9 2 8 5 10 1
5 10 3 9 2 8 5 1 10
State of the Array at the end of each pass
Items
Index 0 1 2 3 4 5 6
unsorted 9 3 10 2 8 5 1
Items
Index 0 1 2 3 4 5 6
unsorted 9 3 10 2 8 5 1
End of Phase 1 3 9 2 8 5 1 10
Items
Index 0 1 2 3 4 5 6
unsorted 9 3 10 2 8 5 1
End of Phase 1 3 9 2 8 5 1 10
End of Phase 2 3 2 8 5 1 9 10
Items
Index 0 1 2 3 4 5 6
unsorted 9 3 10 2 8 5 1
End of Phase 1 3 9 2 8 5 1 10
End of Phase 2 3 2 8 5 1 9 10
End of Phase 3 2 3 5 1 8 9 10
Items
Index 0 1 2 3 4 5 6
unsorted 9 3 10 2 8 5 1
End of Phase 1 3 9 2 8 5 1 10
End of Phase 2 3 2 8 5 1 9 10
End of Phase 3 2 3 5 1 8 9 10
End of Phase 4 2 3 1 5 8 9 10
Items
Index 0 1 2 3 4 5 6
unsorted 9 3 10 2 8 5 1
End of Phase 1 3 9 2 8 5 1 10
End of Phase 2 3 2 8 5 1 9 10
End of Phase 3 2 3 5 1 8 9 10
End of Phase 4 2 3 1 5 8 9 10
End of Phase 5 2 1 3 5 8 9 10
Items
Index 0 1 2 3 4 5 6
unsorted 9 3 10 2 8 5 1
End of Phase 1 3 9 2 8 5 1 10
End of Phase 2 3 2 8 5 1 9 10
End of Phase 3 2 3 5 1 8 9 10
End of Phase 4 2 3 1 5 8 9 10
End of Phase 5 2 1 3 5 8 9 10
End of Phase 6 1 2 3 5 8 9 10
Bubble Sort Algorithm while and for loops
Bubble Sort Algorithm while and for loops
Flag:
• At the start of each iteration of the while loop (i.e. a pass), the flag is
set to False.
First Pass
True
False
Items
Index 0 1 2 3 4 5
value 2 9 8 6 10 15
First Pass
True
False
0 2 9 8 6 10 15
Items
Index 0 1 2 3 4 5
value 2 9 8 6 10 15
First Pass
True
False
0 2 9 8 6 10 15
True 1 9 2 8 9 6 10 15
Items
Index 0 1 2 3 4 5
value 2 9 8 6 10 15
First Pass
True
False
0 2 9 8 6 10 15
True 1 9 2 8 9 6 10 15
2 9 2 8 6 9 10 15
Items
Index 0 1 2 3 4 5
value 2 9 8 6 10 15
First Pass
True
False
0 2 9 8 6 10 15
True 1 9 2 8 9 6 10 15
2 9 2 8 6 9 10 15
3 2 8 6 9 10 15
Items
Index 0 1 2 3 4 5
value 2 9 8 6 10 15
First Pass
True
False
0 2 9 8 6 10 15
True 1 9 2 8 9 6 10 15
2 9 2 8 6 9 10 15
3 2 8 6 9 10 15
4 2 8 6 9 10 15
Items
Index 0 1 2 3 4 5
value 2 9 8 6 10 15
First Pass
True
False
0 2 9 8 6 10 15
True 1 9 2 8 9 6 10 15
2 9 2 8 6 9 10 15
3 2 8 6 9 10 15
4 2 8 6 9 10 15
5 2 8 6 9 10 15
Items
Index 0 1 2 3 4 5
value 2 8 6 9 10 15
Second Pass
True
False
Items
Index 0 1 2 3 4 5
value 2 8 6 9 10 15
Second Pass
True
False
0 2 8 6 9 10 15
Items
Index 0 1 2 3 4 5
value 2 8 6 9 10 15
Second Pass
True
False
0 2 8 6 9 10 15
True 1 2 6 8 9 10 15
Items
Index 0 1 2 3 4 5
value 2 8 6 9 10 15
Second Pass
True
False
0 2 8 6 9 10 15
True 1 2 6 8 9 10 15
2 2 6 8 9 10 15
Items
Index 0 1 2 3 4 5
value 2 8 6 9 10 15
Second Pass
True
False
0 2 8 6 9 10 15
True 1 2 6 8 9 10 15
2 2 6 8 9 10 15
3 2 6 8 9 10 15
Items
Index 0 1 2 3 4 5
value 2 8 6 9 10 15
Second Pass
True
False
0 2 8 6 9 10 15
True 1 2 6 8 9 10 15
2 2 6 8 9 10 15
3 2 6 8 9 10 15
4 2 6 8 9 10 15
Items
Index 0 1 2 3 4 5
value 2 6 8 9 10 15
Third Pass
True
False
0 2 6 8 9 10 15
True 1 2 6 8 9 10 15
2 2 6 8 9 10 15
3 2 6 8 9 10 15
4 2 6 8 9 10 15
Bubble Sort Algorithm while and for loops
improved
Bubble Sort Algorithm while and for loops
improved
Outer While Loop:
• This is unchanged and it works exactly like the previous version