0% found this document useful (0 votes)
9 views15 pages

Week 9

Selection and insertion sort
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

Week 9

Selection and insertion sort
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structures and Algorithms

CS221 – Week 9

Instructor: Dr. Salman Ahmed


Associate Professor FCSE
Office Location: G-04 (FCSE)
[email protected]
1
Recall from Week before Mid Term Exam
 Last week, we studied Bubble Sort Algorithm

 To recall: there are 2 loops in bubble sort algorithm

 The outer loop runs n times from i=0 to n-1

 The inner loop runs from j=1 to n-1

2
3
Analysis of Bubble Sort
Advantages:
1. Simple to understand and implement.
2. Very straight forward.

Disadvantages:
1. It runs slowly and hence it is not efficient, because more
efficient sorting techniques are available.
2. Even if array is sorted, n-1 comparisons are required.

4
Analysis of Bubble Sort
• Average-case: ➔ O(n2)
– We have to look at all possible initial data organizations.

• Worst-case: ➔ O(n2)
– Array is in reverse order:

• In fact, any sorting algorithm which sorts elements by swapping


adjacent elements can be proved to have an O(n2) average case
complexity

5
Selection Sort
• Partition the input list into a sorted and unsorted part (initially
sorted part is empty)

• Select the smallest element and put it to the end of the sorted part

• Increase the size of the sorted part by one

• Repeat this n-1 times to sort a list of n elements

6
Sorted Unsorted

23 78 45 8 32 56 Original List

After pass 1
8 78 45 23 32 56

8 23 45 78 32 56 After pass 2

After pass 3
8 23 32 78 45 56

After pass 4
8 23 32 45 78 56

After pass 5
8 23 32 45 56 78
Selection Sort – Execute wk9s8.cpp
 Select the position of minimum entry to 0

 Start with 1 till n-1 and check: is there any number lesser than the
number at position 0.

 If yes, move position_of_min to that position

 Finally, swap position 0 and position_of_min

 Repeat by selecting position_of_min to 1

8
Insertion Sort
• Start with second element of the array as first element in the array is
assumed to be sorted.

• Compare second element with the first element and check if the second
element is smaller then swap them.

• Move to the third element and compare it with the second element, then
the first element and swap as necessary to put it in the correct position
among the first three elements.

• Continue this process, comparing each element with the ones before it
and swapping as needed to place it in the correct position among the
sorted elements.

• Repeat until the entire array is sorted.


9
Sorted Unsorted

23 78 45 8 32 56 Original List

After pass 1
23 78 45 8 32 56

23 45 78 8 32 56 After pass 2

After pass 3
8 23 45 78 32 56

After pass 4
8 23 32 45 78 56

After pass 5
8 23 32 45 56 78
Insertion Sort
Few comments about insertion sort:

1. Outer loop starts with i=1 (instead of i=0)

2. Inner loop starts with j= i-1 and then decrement by one step
until you reach the zeroth element

3. Perform comparison, and if the condition is true then move all


elements to make space/place for the element.

11
Insertion Sort
Execute wk9s12.cpp and understand it

12
Insertion Sort - Analysis
• Running time depends on not only the size of the array but also the
contents of the array.

• Best-case: ➔ O(n)
– Array is already sorted in ascending order.

• Worst-case: ➔ O(n2)
– Array is in reverse order:

• Average-case: ➔ O(n2)
– We have to look at all possible initial data organizations.
13
Summary
 Now considering the three algorithms, we see that these algorithms are easy to
understand.
 Coding for these algorithms is also easy.
 There is no need of extra storage for sorting an array by these algorithms.
 With respect to the time complexity, these algorithms are proportional to N^2.
 So, we can see that as the value of N increases, the performance time of these
algorithms increases considerably as it is proportional to N^2.
 Thus, these algorithms are expensive with respect to time performance.

14
Summary
 There are algorithms that have the time complexity proportional to N log2 (N).

 the algorithms that are N log2 (N) algorithms are:


 Merge Sort
 Quick Sort
 Heap Sort

15

You might also like