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

Selection Sort Presentation

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

Selection Sort Presentation

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

Selection Sort

Christian De la Torre
Nhiel Carlo Montellano
Jhon Matinong
Selection Sort Algorithm
Selection sort is a simple and efficient sorting algorithm that
works by repeatedly selecting the smallest (or largest) element
from the unsorted portion of the list and moving it to the sorted
portion of the list.

The algorithm repeatedly selects the smallest (or largest) element


from the unsorted portion of the list and swaps it with the first
element of the unsorted part. This process is repeated for the
remaining unsorted portion until the entire list is sorted.
Selection Sort Algorithm
1. Set the first element as minimum

currentIndex = 0
i = 0
min = 20

20 12 10 15 2
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


2. Compare minimum with the second element. If the second
element is smaller than minimum, assign the second
element as minimum.
Compare minimum with the third element. Again, if the third
element is smaller, then assign minimum to the third element
otherwise do nothing. The process goes on until the last element.

currentIndex = 0
i = 1
min = 12

20 12 10 15 2
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


2. Compare minimum with the second element. If the second
element is smaller than minimum, assign the second
element as minimum.
Compare minimum with the third element. Again, if the third
element is smaller, then assign minimum to the third element
otherwise do nothing. The process goes on until the last element.

currentIndex = 0
i = 2
min = 10

20 12 10 15 2
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


2. Compare minimum with the second element. If the second
element is smaller than minimum, assign the second
element as minimum.
Compare minimum with the third element. Again, if the third
element is smaller, then assign minimum to the third element
otherwise do nothing. The process goes on until the last element.

currentIndex = 0
i = 3
min = 10

20 12 10 15 2
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


2. Compare minimum with the second element. If the second
element is smaller than minimum, assign the second
element as minimum.
Compare minimum with the third element. Again, if the third
element is smaller, then assign minimum to the third element
otherwise do nothing. The process goes on until the last element.

currentIndex = 0
i = 4
min = 2

20 12 10 15 2
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


3. After each iteration, minimum is placed in each
currentIndex of index in the unsorted list.

currentIndex = 0
i = 4
min = 20

2 12 10 15 20

Swapping
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


4. For each iteration, indexing starts from the first unsorted
element. Step 1 to 3 are repeated until all the elements are
placed at their correct positions.

4 Steps to perform a Selection Sort

Step 1: Find the minimum (or maximum) element in the


unsorted portion of the array.

Step 2: Swap this minimum (or maximum) element with


the element at the beginning (or end) of the unsorted
portion of the array.

Step 3: Increment the boundary of the sorted portion of the array


to include the newly placed element.

Step 4: Repeat steps 1-3 until the entire array is sorted.


Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


4. For each iteration, indexing starts from the first unsorted
element. Step 1 to 3 are repeated until all the elements are
placed at their correct positions.

currentIndex = 1

2 12 10 15 20
i = 2
min = 12

2 12 10 15 20
i = 3
min = 10

2 12 10 15 20
i = 4
min = 10

i = 4
2 10 12 15 20
min = 12

Swapping
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


4. For each iteration, indexing starts from the first unsorted
element. Step 1 to 3 are repeated until all the elements are
placed at their correct positions.

currentIndex = 2

2 10 12 15 20
i = 2
min = 12

2 10 12 15 20
i = 3
min = 12

2 10 12 15 20
i = 4
min = 12

Already
Sorted
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

Selection Sort Algorithm


4. For each iteration, indexing starts from the first unsorted
element. Step 1 to 3 are repeated until all the elements are
placed at their correct positions.

currentIndex = 3

2 10 12 15 20
i = 3
min = 15

2 10 12 15 20
i = 4
min = 15

Already
Sorted
procedure selectionSort(array: array of integers)
n := length(array)
for i from 0 to n - 1 do
min := i
for j from i to n - 1 do
if array[min] > array[j] then
min := j
end if
end for

temp := array[i]
array[i] := array[min]
array[min] := temp
end for
end procedure
Time Complexities

Best Case Average Case Worst Case


0(n2) 0(n2) 0(n2)
Thank you for listening!
1+1=10

You might also like