0% found this document useful (0 votes)
87 views14 pages

Selection Sort

The document discusses selection sort, a simple brute force sorting algorithm. Selection sort works by iterating through an array, finding the minimum element, and swapping it into the sorted position. It repeats this process, each time designating a new sorted position, until the entire array is sorted from lowest to highest. Pseudocode is provided to demonstrate how selection sort iterates through the array, finds the minimum remaining element, and swaps it into place.

Uploaded by

Hina Tariq
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)
87 views14 pages

Selection Sort

The document discusses selection sort, a simple brute force sorting algorithm. Selection sort works by iterating through an array, finding the minimum element, and swapping it into the sorted position. It repeats this process, each time designating a new sorted position, until the entire array is sorted from lowest to highest. Pseudocode is provided to demonstrate how selection sort iterates through the array, finds the minimum remaining element, and swaps it into place.

Uploaded by

Hina Tariq
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/ 14

Introduction to

Algorithms

12-Nov-19
Agenda

Muzaffar Iqbal Farooqi


- What is Sorting
- Selection Sort (Brute force)

1
What is Brute Force? (Recap)
 An algorithm design technique
It is a straightforward approach to solve a

12-Nov-19

problem in a very easy, yet (usually)
inefficient way.

Muzaffar Iqbal Farooqi


 “Brute force” means “Just do it!” without
taking into account the efficiency of the
algorithm.
 Force” comes from using computer power not
intellectual power
2
Brute Force Algorithms…
• There are a number of brute force algorithms.
• One of the common brute force algorithms is ‘Selection Sort’.

12-Nov-19
Muzaffar Iqbal Farooqi
3
Selection Sort
• Selection sort is a simple sorting algorithm.
• This sorting algorithm is an in-place comparison-based

12-Nov-19
algorithm in which the list is divided into two parts…
• the sorted part at the left end and

Muzaffar Iqbal Farooqi


• the unsorted part at the right end.
• Initially, the sorted part is empty and the unsorted part is the
entire list.
• The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element
becomes a part of the sorted array.
• This process continues moving unsorted array boundary by
one element to the right. 4
How Selection Sort Works?
• Consider the following depicted array as an example.

12-Nov-19
• For the first position in the sorted list, the whole list is

Muzaffar Iqbal Farooqi


scanned sequentially. The first position where 14 is stored
presently, we search the whole list and find that 10 is the
lowest value.

• So we replace 14 with 10. After one iteration 10 appears in


the first position of the sorted list.
5
How Selection Sort Works?
• For the second position, where 33 is residing, we start
scanning the rest of the list in a linear manner.

12-Nov-19
Muzaffar Iqbal Farooqi
• We find that 14 is the second lowest value in the list and it
should appear at the second place. We swap these values.

• After two iterations, two least values are positioned at the


beginning in a sorted manner.

6
How Selection Sort Works?
• The same process is applied to the next items in the array as
shown below...

12-Nov-19
Muzaffar Iqbal Farooqi
7
How Selection Sort Works?
• The same process is applied to the next items in the array as
shown below...

12-Nov-19
Muzaffar Iqbal Farooqi
8
How Selection Sort Works?
• The same process is applied to the next items in the array as
shown below...

12-Nov-19
Muzaffar Iqbal Farooqi
9
Algorithm
Step 1 − Set MIN to loca on 0
Step 2 − Search the minimum element in the list

12-Nov-19
Step 3 − Swap with value at loca on MIN
Step 4 − Increment MIN to point to next element

Muzaffar Iqbal Farooqi


Step 5 − Repeat un l list is sorted

10
Algorithm (Revised)
• Find the smallest element. Swap it with the first element.
• Find the second-smallest element. Swap it with the second

12-Nov-19
element.
• Find the third-smallest element. Swap it with the third

Muzaffar Iqbal Farooqi


element.
• Repeat finding the next-smallest element, and swapping it
into the correct position until the array is sorted.

11
Pseudocode
SELECTION-SORT(array,SIZE)
1. for i ← 0 to SIZE-1
2. smallest ← i

12-Nov-19
3. for j ← i + 1 to SIZE
4. if A[ j ] < A[ smallest ]
5. smallest ← j

Muzaffar Iqbal Farooqi


6. swap A[ i ] ↔ A[ smallest ]

12
Program

Muzaffar Iqbal Farooqi 12-Nov-19


13
?
QUESTIONS

Muzaffar Iqbal Farooqi 12-Nov-19


14

You might also like