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

Sorting Algorithms

The document provides a detailed overview of sorting algorithms, specifically focusing on Insertion Sort and Bubble Sort. It includes explanations of the algorithms, their complexities, and examples of how they operate. Additionally, it discusses loop invariants and the correctness of Insertion Sort, as well as the mechanics of Bubble Sort.

Uploaded by

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

Sorting Algorithms

The document provides a detailed overview of sorting algorithms, specifically focusing on Insertion Sort and Bubble Sort. It includes explanations of the algorithms, their complexities, and examples of how they operate. Additionally, it discusses loop invariants and the correctness of Insertion Sort, as well as the mechanics of Bubble Sort.

Uploaded by

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

Design and Analysis of

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

Cycle Number of Comparisons


1 𝑠𝑡 𝑛−1
2𝑛𝑑 𝑛−2
3𝑟𝑑 𝑛−3
……… …
𝑙𝑎𝑠𝑡 1
Bubble Sort Algorithm for loop
Bubble Sort Algorithm for loop
• Outer for Loop
• The range of the outer for loop is from 1 to n-1, where n is the
number of items

• Inner for Loop


• The range of the outer for loop is from 0 to n-2, where n is the
number of items.
• This loop checks each pair of consecutive values, items[index] and
items[index+1].
How the Algorithm Performs a Swap
• The value of the first item in the pair (items[index]) is copied to temp.
This is important as the value in the first item is overwritten in the
next line of code and will be lost otherwise.
• The value of the second item in the pair (items[index] + 1) is then
copied to items[index] (and replaces its original value).
• The value of temp is then copied to items[index + 1] (and replaces its
original value).
How efficient is this version?
• This version of the algorithm is the easiest to write but is not very
efficient
• The number of passes is always n−1
Tracing the algorithm using a trace table

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.

Outer While Loop:


• The outer while loop will run while the value of the flag is True (i.e.
one or more swaps were made on the previous pass, or it is the first
pass)
Bubble Sort Algorithm while and for loops
Inner for Loop:
• The inner for loop checks each pair of consecutive values,
items[index] and items[index + 1], to see if they are out of order and
need to be swapped; if so, the items are swapped and the flag is set
to True.
How efficient is this version?
• If no swaps are made during a pass, the process finishes because the
list is sorted
• If one or more swaps are made during a pass, there must be at least
one more pass through the list
Tracing the algorithm using a trace table
Items
Index 0 1 2 3 4 5
value 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
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

Inner for Loop:


• The only change here is that the inner for loop will iterate one time
less on each successive iteration of the while loop.
Bubble Sort Algorithm while and for loops
improved
Inner for Loop:
• The only change here is that the inner for loop will iterate one
timeless on each successive iteration of the while loop.
• The algorithm uses the variable pass_num as a counter to track the
current pass number. At the start of the algorithm, pass_num is
initialised to 1 since the first pass is about to be performed. After
each iteration of the for loop, pass_num is incremented by 1.
How efficient is this version?
• This is the most efficient version of the algorithm. It improves on the
previous version with fewer iterations of the inner for loop.
Tracing the Algorithm
Items
Index 0 1 2 3 4 5 6
Values 5 1 8 3 10 2 9
First Pass
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
False
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
False
True 0 5 1 5 8 3 10 2 9
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
False
True 0 5 1 5 8 3 10 2 9
1 1 5 8 3 10 2 9
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
False
True 0 5 1 5 8 3 10 2 9
1 1 5 8 3 10 2 9
2 8 1 5 3 8 10 2 9
3 1 5 3 8 10 2 9
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
False
True 0 5 1 5 8 3 10 2 9
1 1 5 8 3 10 2 9
2 8 1 5 3 8 10 2 9
3 1 5 3 8 10 2 9
4 10 1 5 3 8 2 10 9
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
False
True 0 5 1 5 8 3 10 2 9
1 1 5 8 3 10 2 9
2 8 1 5 3 8 10 2 9
3 1 5 3 8 10 2 9
4 10 1 5 3 8 2 10 9
5 10 1 5 3 8 2 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
5 1 8 3 10 2 9
True 1
False
True 0 5 1 5 8 3 10 2 9
1 1 5 8 3 10 2 9
2 8 1 5 3 8 10 2 9
3 1 5 3 8 10 2 9
4 10 1 5 3 8 2 10 9
5 10 1 5 3 8 2 9 10
Second Pass
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 5 3 8 2 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 5 3 8 2 9 10
True 2
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 5 3 8 2 9 10
True 2
0 1 5 3 8 2 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 5 3 8 2 9 10
True 2
0 1 5 3 8 2 9 10
1 5 1 3 5 8 2 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 5 3 8 2 9 10
True 2
0 1 5 3 8 2 9 10
1 5 1 3 5 8 2 9 10
2 1 3 5 8 2 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 5 3 8 2 9 10
True 2
0 1 5 3 8 2 9 10
1 5 1 3 5 8 2 9 10
2 1 3 5 8 2 9 10
3 8 1 5 5 2 8 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 5 3 8 2 9 10
True 2
0 1 5 3 8 2 9 10
1 5 1 3 5 8 2 9 10
2 1 3 5 8 2 9 10
3 8 1 3 5 2 8 9 10
4 1 3 5 2 8 9 10
Third Pass
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 5 2 8 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 5 2 8 9 10
True 3
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 5 2 8 9 10
True 3
False
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 5 2 8 9 10
True 3
False
0 1 3 5 2 8 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 5 2 8 9 10
True 3
False
0 1 3 5 2 8 9 10
1 1 3 5 2 8 9 10
3items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 5 2 8 9 10
True 3
False
0 1 3 5 2 8 9 10
1 1 3 5 2 8 9 10
2 5 1 3 2 5 8 9 10
3items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 5 2 8 9 10
True 3
False
0 1 3 5 2 8 9 10
1 1 3 5 2 8 9 10
2 5 1 3 2 5 8 9 10
3 1 3 2 5 8 9 10
Forth Pass
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 2 5 8 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 2 5 8 9 10
True 4
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 2 5 8 9 10
True 4
False
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 2 5 8 9 10
True 4
False
0 1 3 2 5 8 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 2 5 8 9 10
True 4
False
0 1 3 2 5 8 9 10
1 3 1 2 3 5 8 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 3 2 5 8 9 10
True 4
False
0 1 3 2 5 8 9 10
1 3 1 2 3 5 8 9 10
2 1 2 3 5 8 9 10
Fifth Pass
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 2 3 5 8 9 10
items
Swapped Pass_num index temp 0 1 2 3 4 5 6
1 2 3 5 8 9 10
True 4
False
0 1 2 3 5 8 9 10
1 1 2 3 5 8 9 10
Selection Sort Algorithm
Selection Sort Algorithm
• Selection sort is one of the basic sorting algorithms which is easy to
understand and implement. The idea is simple: given an array of
elements to be sorted,
• Divide the array into two subarrays: sorted and unsorted subarrays
Selection Sort Algorithm
• In each iteration, find the maximum/minimum element in the
unsorted subarray and swap it with the first element of the unsorted
subarray.
• Swapping the minimum value will sort the elements in ascending
order while swapping the maximum value will sort the elements in
descending order.
12 29 25 8 32 17 40
12 29 25 8 32 17 40
12 29 25 8 32 17 40
12 29 25 8 32 17 40
8 29 25 12 32 17 40
8 29 25 12 32 17 40
8 29 25 12 32 17 40
8 12 25 29 32 17 40
8 12 25 29 32 17 40
8 12 25 29 32 17 40
8 12 25 29 32 17 40
8 12 17 29 32 25 40
8 12 17 29 32 25 40
8 12 17 29 32 25 40
8 12 17 25 32 29 40
8 12 17 25 32 29 40
8 12 17 25 32 29 40
8 12 17 25 29 32 40
8 12 17 25 29 32 40
8 12 17 25 29 32 40
Selection Sort Algorithm
int i, j, min_index, temp;
for(i=0; i<n-1; i++) {
min_index = i;
for(j=i+1; j<n; j++)
if(A[j] < A[min_index])
min_index = j;
temp = A[i];
A[i] = A[min_index];
A[min_index] = temp;
}
Loop invariant of inner loop
At the start of each iteration of for loop, A[min_index] is the smallest
element in A[i, j).
• Initialization: before the first iteration i = min_index and j = i + 1.
A[min_index] is the only element in A[i, i+1) which is trivially the
smallest element.
Loop invariant of inner loop
Maintenance: Next, we tackle the second part: showing that each
iteration maintains the loop invariant. suppose for j loop invariant
holds, that is, A[min_index] is the smallest in A[i, j). for the next
iteration of loop where j = j + 1, if A[j + 1] < A[min_index] we update
min_index to j + 1. Now A[min_index] is the new min in A[i, j+1). Else
we do nothing. in this case A[min_index] remains the smallest in A[i,
j+1). in both cases loop invariant hold true.
Loop invariant of inner loop
Termination: condition which cause to terminate loop is j = n. now by
termination condition and loop invariant we can conclude
A[min_index] is the smalles in A[i, j = n) which means we found
minimum element A[min_index] in the subarray A[i, n-1].
Loop invariant of outer loop
• At the start of each iteration of loop, A[0, i) contains i smallest
elements of A[0...n-1] but in sorted order.

• Initialization: prior to the first iteration of loop i=0. A[0,i) is empty


subarray and the predicate is vacuously true.
Loop invariant of outer loop
• Maintenance: We need to show that each iteration maintains the
invariant property. Assume for i invariant holds. A[0, i) contains i
smallest elements of original array in sorted order. at the next
iteration i = i + 1. we can definitely conclude by first loop invariant
that A[min_index] will be the smallest element in A[i, n). Since we
know predicate is true for i, we can say that A[0, i) is sorted, and we
swap A[i] with A[min_index]. we're done. A[0, i+1) contains i+1
smallest elements of original array in sorted order.
Loop invariant of outer loop
• Termination: Termination condition which is i=n-1 along with
invariant implies that A[0, n-1) contains n-1 smallest elements of
array in sorted order. Since A[0, n-1) contains the n-1 smallest
elements of the array, automatically the last element which is A[n-1]
is greater than or equal to all elements in A[0, n-1) and therefore is in
the correct place in the sorted array.
Selection Sort Complexity
Time Complexity
Best 𝑂 𝑛2
Worst 𝑂 𝑛2
Average 𝑂 𝑛2
Space Complexity 𝑂 1
Stability 𝑌𝑒𝑠

You might also like