Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Subject: Data Structure

Using C++ (CSET202)

Week 4 Lecture

Visualization of Sorting Algorithms


What, Why and How?

by

Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Last Week’s Learnings

• Visualized the Linear & Binary Search Algorithm

• Understood the Recursion

• Analyzed the Operations of Bubble and Selection Sorting


Algorithms

• Optimized the performance of Bubble Sorting Algorithm


General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Space Complexity of this
algorithm?
a) f(n)= O(n)
Algorithm print(n)
{ b) f(n)= O(100)
for ( i=0; i<n; i++) c) f(n)= O(1)
{
Which one is the d) I am confused
printf(“This is number: %d”, i);
correct answer? }
}

Correct Answer is c
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Time Complexity of this
algorithm?
a) f(n)= O(n)
Algorithm print(n)
{ b) f(n)= O(n2)
for ( i=n; i>0; i=i/2)
c) f(n)= O(logn)
{
Which one is the printf(“This is number: %d”, i); d) I am confused
correct answer? }
}

Correct Answer is c
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Time Complexity of deleting
an element from beginning of the array?

a) f(n)= O(1)

b) f(n)= O(n2)

Which one is the c) f(n)= O(n)

d) I am confused
correct answer?

Correct Answer is c
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Time Complexity of inserting
an element at the end of the array?

a) f(n)= O(1)

b) f(n)= O(n2)

Which one is the c) f(n)= O(n)

d) I am confused
correct answer?

Correct Answer is A
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Worst Case Time Complexity
of Bubble Sort Algorithm?

a) f(n)= O(1)

b) f(n)= O(n2)

Which one is the c) f(n)= O(n)

d) I am confused
correct answer?

Correct Answer is B
General Assessment Time
10
9
8
7
6
5
4
3
2
1
Number of Swap Operations is performed in
the Optimized Bubble Sort Algorithm?

a) n

b) n2

Which one is the c) 1

d) 0
correct answer?

Correct Answer is D
Sorting Algorithms

• Bubble Sort
• Selection Sort
• Insertion Sort
• Shell Sort
• Merge Sort
• Counting Sort
Understanding Bubble Sort

Compares two adjacent elements and


swaps them until they are in the intended
order

*Movement of array elements is just like the movement of air bubbles in the water
Analyzing Time Complexity of Bubble Sort

Worst Case Analysis


Can TC be further
Average Case Analysis TC = O(n2) improved ?

Best Case Analysis

Total Time= Total Comparisons + Total Swapping


Optimizing the Time Complexity of Bubble Sort

1 2 3 4 5 6 void bubbleSort (int array[], int size) If there is no swap in the first
{ pass, and if I come out from the
second loop without going
for (int pass = 1; pass < size; ++pass) further then I can improve the
Pass #Comp #Swap { time
for (int i = 0; i <= size - pass; ++i)
1 5 0 {
if (array[i] > array[i + 1])
2 4 0 {
int temp = array[i];
3 3 0 array[i] = array[i + 1];
array[i + 1] = temp;
4 2 0 }
}
5 1 0 }
}

Total Time= Total Comparisons + Total Swapping

Best Case Analysis TC = O(n2)


Optimizing the Time Complexity of Bubble Sort
N=6
void bubbleSort (int array[], int size)
1 2 3 4 5 6 {
flag=1;
Pass #Comp #Swap for (int pass = 1; pass < size; ++pass)
{
5 0 for (int i = 0; i <= size - pass; ++i)
1
{
if (array[i] > array[i + 1])
{
int temp = array[i];
For n values array[i] = array[i + 1];
array[i + 1] = temp;
Pass #Comp #Swap flag=0;
}
1 n-1 0 }
if (flag==1) break;
}
Total Time= Total Comparisons + Total Swapping
}
= n-1 + 0 = n-1
Best Case Analysis TC = O(n)
Analyzing Time Complexity of Bubble Sort

Worst Case Analysis

Average Case Analysis TC = O(n2)

Best Case Analysis


Optimized Best Case Analysis TC = O(n)

Can TC be further improved ?


Can TC be further improved ?

Instead of doing so many swapping, we can do


only one swapping in each pass.
Selection Sort

Selects the smallest element from an


unsorted list in each iteration and places
that element at the beginning of the
unsorted list.
Analyzing Time Complexity of Selection Sort

Worst Case Analysis

Average Case Analysis TC = O(n2)

Best Case Analysis


Can TC be further improved ?

Total Time= Total Comparisons + Total Swapping


Time Complexity of Selection Sort

Pass #Comp #Swap


1 2 3 4 5 6 5 0
1
2 4 0

3 3 0
List is already sorted 4 2 0

Best Case Analysis 5 1 0

TC = O(n2) For Visualization

Note: While looking at No Swap Execution (Swap=0) in the first pass, we can not say
that list is completely sorted. Hence, by introducing flag concept (as similar to bubble
sort) in the selection sorting algorithm, we can not improve the time complexity.
(Readers can make the correction in the previous week (Week 3) lecture slide)
Can TC further improve ?

Instead of doing repetitive comparisons, we


can minimize the number of comparison in
each pass.
Concept of Insertion Sort

1. Insertion sort is a simple sorting algorithm that works


similar to the way you sort playing cards in your
hands.

2. The array is virtually split into a sorted and an


unsorted part.

3. Values from the unsorted part are picked and placed at


the correct position in the sorted part.
Understanding Insertion Sort
Step 1 - If the element is the first element, assume that it is

already sorted. Return 1.


To sort an array of size n in ascending order
Step2 - Pick the next element and store it separately in a key.

Step3 - Now, compare the key with all elements in the sorted

array.

Step 4 - If the element in the sorted array is smaller than the

key element, then do nothing and key will point to the next

element else, shift greater elements in the array towards

the right.

Step 5 - Insert the value.

Step 6 - Repeat until the array is sorted.


Understanding Insertion Sort
Sorted Un-Sorted
Step 1 - If the element is the first element, assume that it is already
Key
4 3 1 10 5
sorted. Return 1.
3 Key

Step2 - Pick the next element and store it separately in a key.


4 3 1 10 5
Step3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the key 3

element, then do nothing and key will point to the next element 4 1 10 5
Key
else, shift greater elements in the array towards the right. Key Sorted Un-Sorted

Step 5 - Insert the value. 1 3 4 1 10 5

Step 6 - Repeat Step 2-5 until the array is sorted. 1


1
3 4 10 5
Understanding Insertion Sort
Sorted Un-Sorted

Step 1 - If the element is the first element, assume that it is 1 3 4 10 5


Key
already sorted. Return 1. 10 Key

Step2 - Pick the next element and store it separately in a key.


1 3 4 10 5
Step3 - Now, compare the key with all elements in the sorted Key
Key

array. 5
1 3 4 10 5
Step 4 - If the element in the sorted array is smaller than

the key element, then key will point to the next element else 1 3 4 5 10
shift greater elements in the array towards the right.

Step 5 - Insert the value.

Step 6 - Repeat Step 2-5 until the array is sorted. 1


Understanding Insertion Sort
i

Sorted Key
Un-Sorted

4 3 1 10 5
Step 1 - If the element is the first element, assume that it is
j
already sorted. Return 1.
void insertionSort(int arr[], int n)
Step2 - Pick the next element and store it separately in a key. {
int i, key, j;
Step3 - Now, compare the key with all elements in the sorted for (i = 1; i < n; i++)
{
array. key = arr[i];
j = i - 1;
Step 4 - If the element in the sorted array is smaller than the
while (arr[j] > key && j >= 0 )
key element, then key will point to the next element else {
arr[j + 1] = arr[j]; //Shifting to Right
shift greater elements in the array towards the right.
j = j - 1;
}
Step 5 - Insert the value.
arr[j + 1] = key;
Step 6 - Repeat until the array is sorted. }
}
Analyzing Time Complexity of Insertion Sort
Sorted Key
Un-Sorted

Worst Case 5 4 3 2 1 #Comp #Shift

1 1

Pass #Comp #Shift 2 2


1 1 1
Total Number of Comparison 3 2
= 1 + 2 + 3 + 4 +… n-1 = n (n+1)/2 =~ n2 2 2 2
. .
3 3 3
Total Number of Shifting
= 1 + 2 + 3 + 3 + ..n-1 = (n-1)*n/2 = ~n2 4 4 4 . .

n-1 n-1

Total Time = Total Number of Comparison + Total Number of Shifting = n2+n2=2n2

Time Complexity= O(n2)


Analyzing Time Complexity of Insertion Sort
Key
Sorted
Un-Sorted

Best Case 1 2 3 4 5 #Comp #Shift

1 0

Total Number of Comparison Pass #Comp #Shift 1 0


= 1 + 1 + 1 + 1 +… n-1 = n-1 = ~ n-1
1 1 0 1 0

Total Number of Shifting = 0 2 1 0 . .


3 1 0
. .
4 1 0
n-1 0

Total Time = Total Number of Comparison + Total Number of Shifting = n + 0 = n

Time Complexity= O(n)


Analyzing Time Complexity of Insertion Sort

Worst Case Analysis


TC = O(n2)
Average Case Analysis

Best Case Analysis TC = O(n)

Can TC be further improved ?


Understanding the Scope of Improving Insertion Sort

5 4 3 2 1 2 3 4 5 1

If the smaller value is to the far right and must be


moved to the far left, so many shift operations…

Can comparison or shifting


operations further reduce?
Concept of Shell Sort Algorithm

• If the smaller value is to the far right and must be


moved to the far left, this algorithm avoids large shifts.

• Shell sort is a generalized version of the insertion sort


algorithm
Shell Sort Algorithms Sort the Elements

Gap of 4
• It first sorts elements that are far apart from each other
and successively reduces the interval between the
elements to be sorted.
9 5

8 6
• There is a concept of Gap or Interval, that can be
4
3
determined by following ways:
7 1

• Shell's original sequence: N/2 , N/4 , …, 1


(For Imagination)
• Knuth's increments, Sedgewick's increments,
• Hibbard's increments, Papernov & Stasevich increment, Pratt
List 1: (9,5)
List 2: (8,6)
List 3: (3,4)
List 4: (7,1)
Understanding Shell Sort Algorithms Sort the Elements

Gap of 4
We will use very simple technique of N/2, N/4, ...1
to calculate the gaps in our algorithm.

5
9
In Pass-1,
if the array size is N = 8 then,

Step-1 the elements lying at the gap


of N/2 = 4 are compared and swapped
if they are not in order.
Sort the Elements
Understanding Shell Sort Algorithms
Gap of 4
0 1 2 3 4 5 6 7

Step-2 This process goes on for all the remaining


elements.

8 6
• Then 1st element is compared with the 5th
4
3
element. 1
7

• Then 2nd and 3rd element is compared with


6th, 7th element
Understanding Shell Sort Algorithms

In Pass-2, Gap of N/4 = 8/4 = 2 is taken and again the elements lying at these gaps are sorted.
Sort the Elements
Imagination
Gap of 2

List 1- 5, 3, 9, 4

List 2- 6, 1, 8, 7
5 3 9 4
Apply Insertion Sort
6 1 8 7
Understanding Shell Sort Algorithms

In Pass-2, Gap of N/4 = 8/4 = 2 is taken and again the elements lying at these gaps are sorted.
0 1 2 3 4 5 6 7

Then 2nd element is compared with


the 0th element.

5 3

Then 3rd element is compared with 1st


element

6 1
Understanding Shell Sort Algorithms

In Pass-2, Gap of N/4 = 8/4 = 2 is taken and again the elements lying at these gaps are sorted.
0 1 2 3 4 5 6 7

Then 4th element is compared with 2nd


and 0th element
3 5 9

Then 5th element is compared with 3rd 0 1 2 3 4 5 6 7

and 1st element and so on…

5 3 9 4

6 1 8 7
Understanding Shell Sort Algorithms

In Pass 3, Finally, when the gap is N/8 = 8/8 =1 then the array
elements lying at the gap of 1 are sorted.
3 1 4 6 5 7 9 8

Then 1st element is compared with the 0th element.

Then 2nd element is compared with 1st and 0th element


and so on. It will be working as a normal insertion sort.

The array is now completely sorted.


Understanding Shell Sort Algorithms
Credit: https://www.udiprod.com/shell-sort/
i
Understanding Shell Sort Code Gap =n/2 = 8/2= 4 Key

0 1 2 3 4 5 6 7

void shellSort( int array[], int n)


{ j
for (int gap=n/2 ; gap > 0; gap =gap/ 2) 9 5
{
for (int i = gap; i < n; i ++) //Performing Gapped Insertion Sort
{
int key = array[i]; void insertionSort(int arr[], int n)
{
int j=i-gap; Mapping int i, key, j;
while(array [j ] > key && j >= 0) for (i = 1; i < n; i++) {
key = arr[i];
{ j = i - 1;
array[j+gap] = array[j ];
while (arr[j] > key && j >= 0)
j =j- gap; {
} arr[j + 1] = arr[j];
j = j - 1;
array[j+gap] = key; }
} arr[j + 1] = key;
}
} }
}
Understanding Shell Sort Algorithms

void shellSort( int array[], int n)


{
for (int gap = n / 2; gap > 0; gap =gap/ 2) // Rearrange elements at each n/2, n/4, n/8, ... gap
{
for (int i = gap; i < n; i ++) //Performing Gapped Insertion Sort
Best Case Analysis TC = O(nlogn)
{
int temp = array[i];
int j=i-gap;
Worst Case Analysis TC = O(n(logn)2)
while(array [j ] > temp && j >= 0)
{ Average Case Analysis TC = O(n(logn)2)
array[j+gap] = array[j ];
j =j- gap;
}
array[j+gap] = temp;
}
}
}
*Time complexity will vary for different gaps
Analyzing Time Complexity of Shell Sort

Worst Case Analysis


TC = O(n (logn)2)
Average Case Analysis

Best Case Analysis TC = O(nlogn)

Can TC further improve ?


Next Week Readings

• Merge Sort

• Count Sort

• Quick Assessments

Counting sort is a stable algorithm because the relative order of similar elements are not
Any Queries?

Office MCub311
Discussion Time: 3-5 PM
Mob: +91-8586968801

You might also like