Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

K. K.

Wagh Polytechnic,
NashikDepartment of Computer
Technology
Course: Data Structure Using C
Selection Sort
Presented By:
33-Kandekar Tanisha Rajendra
60-Umbare Gayatri Chandrakant
61-Vibhandik Harshada Jagdish
68-Geete Yukta Niranjan
73-Patil Roshan Dnyaneshwar
What will we learn?
Click icon to add picture

 What is actually a selection sort?


 How the selection sort works?
 Algorithm for selection sort.
 An example on selection sort.
 Advantages and disadvantages of selection sort.
Introduction
In selection sort, the smallest value among the
unsorted elements of the array is selected in every
pass and inserted to its appropriate position into the
array.
First, find the smallest element of the array and
place it on the first position. Then, find the second
smallest element of the array and place it on the
second position. The process continues until we get
the sorted array.
What is actually the selection sort?
• This sorting algorithm is an in-place comparison based
algorithm.
• In which the list is divided into two parts, the sorted part at
the left end and the unsorted part at the right end.
• Initially, the sorted part is empty and the unsorted part is
the entire list.
Algorithm:-
Step 1 − Repeat step 2
For i=0 to N-1
Step 2 − Repeat the same for j=j+1 to N-1
Step 3 – If A[i]>A[j]
swap A[i] and A[j]
Step 4 – end of loop
Step 5 − End
How it works?
Click icon to add picture

Consider the following Array.

A[6]={ 30,17 ,06 ,97 ,35,12 }


Click icon to add picture
N=6

0 1 2 3 4 5

30 17 06 97 35 12
Pass-I

0 1 2 3 4 5
30 17 06 97 35 12

17 30 06 97 35 12

06 30 17 97 35 12

06 30 17 97 35 12

06 30 17 97 35 12

06 30 17 97 35 12
Comp:-05
Exchanges:-02
Pass-II
0 1 2 3 4 5
06 30 17 97 35 12

06 17 30 97 35 12

06 17 30 97 35 12

06 17 30 97 35 12

06 12 30 97 35 17

Comp:-04
Exchanges:-02
Pass-III

0 1 2 3 4 5
06 12 30 97 35 17

06 12 30 97 35 17

06 12 30 97 35 17

06 12 17 97 35 30

Comp:-03
Exchanges:-01
Pass-IV

0 1 2 3 4 5

06 12 17 97 35 30

06 12 17 35 97 30

06 12 17 30 97 35

Comp:-02
Exchanges:-02
Pass-V

0 1 2 3 4 5

06 12 17 30 97 35

06 12 17 30 35 97

Sorted Array:-

06 12 17 30 35 97
Total Comparisons:-15
Comp:-01 Total Exchanges:-08
Exchanges:-01 Total No. of Passes:05
Time complexity:-

Selection sort algorithm consists of


two nested loops.

Owing to the two nested loops, it has


O(n2) time complexity.
 
Advantages of selection
sort:-

The main advantage of the selection sort is that it


performs well on a small list.

No additional temporary storage is required.

Selection sort uses minimum number of swap


operations O(n) among all the sorting algorithms.
Disadvantages of selection
sort:-

 The primary disadvantage of the selection sort is


its poor efficiency when dealing with a huge list
of items.

 The selection sort requires n-squared number of


steps for sorting n elements
Conclusion
In this presentation we have explained one of the process of sorting an array
i.e. Selection sort. We have described the algorithm for the same and have
Explained one example. We also went through the advantages and
disadvantages of selection sort.
A Quick Quiz….
Question answer session:-
How many comparisons are required to sort an
array of length 5 if the selection sort is used and the
array is already sorted in opposite order?
A. 1
B. 20
C. 10
D. 5
An array of 5 numbers has the following entries in order:
7 4 5 10 8. If we’ll use selection sort to sort this array in
descending order. What will the array contain after two
iterations of selection sort?
A. 10 8 7 5 4
B. 10 8 5 7 4
C. 8 10 5 7 4
D. None of these

In a selection sort structure, there is/are?


A. Two separate for loops
B. Three for loops, all separate
C. Two for loops, one nested in the other
D. A for loop nested inside a while loop
Which one of the following is the first step in a selection
sort algorithm?
A. The minimum value in the list is found
B. The maximum value in the list is found
C. Adjacent elements are swapped

How many passes (or "scans") will there be through a list


being sorted using a selection sort?
A. Array_size*2
B. Array_size+1
C. Array_size-1
D. None of the above
Answers:-
1. C
2. B
3. C
4. A
5. C

You might also like