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

lecture-5 (2)

The document provides an overview of sorting algorithms, specifically Selection Sort and Bubble Sort, detailing their algorithms, code snippets, and analysis of time and space complexities. Selection Sort has a time complexity of O(n^2) and is an in-place algorithm, while Bubble Sort also has a time complexity of O(n^2) but can be optimized by detecting when no swaps are made. Additionally, the document touches on the average case for Insertion Sort and introduces the Tower of Hanoi problem with its recurrence relation.

Uploaded by

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

lecture-5 (2)

The document provides an overview of sorting algorithms, specifically Selection Sort and Bubble Sort, detailing their algorithms, code snippets, and analysis of time and space complexities. Selection Sort has a time complexity of O(n^2) and is an in-place algorithm, while Bubble Sort also has a time complexity of O(n^2) but can be optimized by detecting when no swaps are made. Additionally, the document touches on the average case for Insertion Sort and introduces the Tower of Hanoi problem with its recurrence relation.

Uploaded by

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

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Foundations of Data Structures and Algorithms


(BITS F232 )

Lecture No. 5

1
Selection Sort
• Big Idea
Orders a list of values by repeatedly putting the
smallest unplaced value into its final position.
Algorithm:
– Look through the list to find the smallest value.
– Swap it so that it is at index 0.
– Look through the list to find the second-
smallest value.
– Swap it so that it is at index 1.
...
– Repeat until all values are in their proper
places.
Selection Sort – Code Snippet

Selection Sort – Code Snippet


for (i = 0 ; i < n-1 ; i++)
{
Here
index = i; i = variable to traverse the array A
for(j = i+1 ; j < n ; j++) index = variable to store the index of
minimum element
{ j = variable to traverse the unsorted
if(A[j] < A[index]) sub-array
temp = temporary variable used for
index = j; swapping
}
temp = A[i];
A[i] = A[index];
A[index] = temp;
}
• Invariant?
Selection Sort - Analysis
• Analysis
Each step of Selection Sort algorithm involves finding minimum
element of the unsorted list.
Initially size of unsorted list is n
Finding minimum = (n-1) comparisons
After 1st iteration size of unsorted list is n-1
Finding minimum = (n-2) comparisons
After 2nd iteration size of unsorted list is n-2
Finding minimum = (n-3) comparisons
And so on
Finally – Total number of comparisons
= (n-1) + (n-1) + … +1 = n(n-1)/2
Therefore Time complexity = O(n2)
Selection Sort - Analysis
Analysis (Notes)
• Analysis does not depend on particular instance
• Therefore Best Case = Worst case = O(n2)
• Running time does not depend depends on the
amount of order in the sequence
• Number of swap operations = O(n)
(Check this for Insertion sort – Exercise)
• Selection sort is an in-place algorithm.
It performs all computation in the original array
and no other array is used.
Hence, the space complexity is O(1).
Bubble Sort
• Idea
Large item is like “bubble” that floats to the end of the array
Algorithm:
Given an array of n elements
Step 1. Compare pair of adjacent items
Step 2. Swap if the items are out of order
Step 3. Repeat until the end of array
The largest item will be at the last position
Step 4. Reduce n by 1 and go to Step 1
Terminology – One Pass consists of Step1 – Step 3
Note:
• After 1st Pass largest element is in the last position.
• Algorithm terminates after n-1 passes
Trace of Bubble Sort Algorithm
Initial List
19 12 30 14 18

Pass1
19 12 30 14 18 Exchange

12 19 30 14 18 No Exchange
12 19 30 14 18 Exchange
12 19 14 30 18 Exchange

12 19 14 18 30 End of Pass1
Note: Largest element is at the end of the list
Trace of Bubble Sort Algorithm
Pass 2
12 19 14 18 30 No Exchange

12 19 14 18 30 Exchange

12 14 19 18 30
Exchange

12 14 18 19 30
No Exchange
12 14 18 19 30
End of Pass2
Note:
• 2nd Largest element is in its correct position
• Bubble Sort will terminate after two more passes
Trace of Bubble Sort Algorithm
Observe:
• Bubble Sort will continue with passes till termination
though list may be sorted after an intermediate Pass.
• Like in above example, list is sorted after 2nd Pass but
Bubble Sort will terminate only after 4 Passes
Bubble Sort – Pseudo Code

Pseudo Code
begin
for i = 1 to n−1
for j = 1 to n−i
if (a[j]> a[j+ 1]) then
Swap (a[j],a[j+ 1])
end
• Invariant?
Bubble Sort - Analysis
Analysis:
• Irrespective of the nature of input, the number of passes to
be made is n−1
• The number of comparisons during ith pass is n−i.
The total number of comparisons is =
(n−1) + (n−2) +...+ 2 + 1 = n(n-1)/2 = O(n2)
Bubble Sort - Analysis
Analysis (Notes)
• Analysis does not depend on particular instance
• Therefore Best Case = Worst case = O(n2)
• Number of swap operations
Best Case – O(1)
Worst Case - O(n2)
Exercise: Identify the instances of best case and worst case
• Bubble sort is an in-place algorithm.
It performs all computation in the original array
and no other array is used.
Hence, the space complexity is O(1).
Bubble Sort - Variation
• As observed in the example the list is sorted well
before (n-1) passes.
• Question
How to take advantage of this?
• Observe
If no exchanges are made in a pass then the list is
sorted.
• Exercise
Design an algorithm that incorporates the above
idea & find the number of comparisons it makes.
Sorting - Summary
Summary – Time Complexity
Best Case Worst Case
Insertion Sort O(n) O(n2)
Selection Sort O(n2) O(n2)
Bubble Sort O(n2) O(n2)

Note: Insertion, Selection & bubble sort are “comparison based”


sorting algorithms.
i.e. they are based on comparing elements of the sequence.
Question:
Better Sorting Algorithms?
Theorem: The lower bound for any comparison based sorting
algorithm is O(nlogn). We will Prove later
Average Case
Average Case for Insertion sort
An inversion is a situation where there exist i > j with
A[i] < A[j].
Example:
Consider the sequence 4, 3, 1, 8, 3
In this list there are 5 inversions:
(4, 3), (4, 1), (4, 3), (3, 1), (8, 3)
Insertion sort – Average Case
Average Case for Insertion sort
When the for loop encounters A[i] it will require a number of
shifts/swaps equal to the inversion count involving A[i].
This is because each shift corrects an inversion. Thus the total
number of shifts will equal the total number of inversions.
Inversions: (5,2),(5,4),(5,1),(5,3),(2,1)(4,1),
(4,3),(6,1),(6,3)
Total = 9
No. of Shifts
1+1+0+4+3 = 9

So running time can be written in terms of the number of


inversions which exist in the original list.
Average Case

Average Case
Given a sequence what is the Expected Number
of inversions?
Let
Average Case
Therefore, by linearity of expectation

And

So

Therefore, Average case is of O(n2)


Observe: Average case behaves like worst case
Recurrence Relations
The Tower of Hanoi It Consists of three pegs mounted
on a board together with disks of
different sizes. Initially these disks
are placed on the first peg in order
of size, with the largest on the
bottom
The rules of the puzzle allow disks
The Initial Position in Tower of Hanoi
to be moved one at a time from one
peg to another as long as a disk is
never placed on top of a smaller
disk.
The goal of the puzzle is to have
all the disks on the 2nd peg in
order of size, with the largest on
An Intermediate Position in the Tower of Hanoi the bottom
Recurrence Relations
Let Hn denote the number of moves needed to solve the
Tower of Hanoi problem with n disks.
We set up a recurrence relation for the sequence {H n}.
A recurrence relation is an equation according to which the nth term of a sequence of numbers
is defined in terms of its previous terms.

Begin with n disks on peg 1.


We can transfer the top n − 1 disks, following the rules of
the puzzle, to peg 3 using Hn-1 moves
We keep the largest disk fixed during these moves.
Then, we use one move to transfer the largest disk to the
second peg.
We can transfer the n − 1 disks on peg 3 to peg 2 using Hn-1
additional moves, placing them on top of the largest disk,
which always stays fixed on the bottom of peg 2.
Recurrence Relations - Solution
This shows that Hn = 2 Hn-1 + 1
The initial condition is H1 = 1
Because one disk can be transferred from peg 1 to peg 2,
according to the rules of the puzzle, in one move.
How to solve the recurrence relation?
Iterative Approach We have used the recurrence
relation repeatedly to express Hn in
terms of previous terms of the
sequence

You might also like