0% found this document useful (0 votes)
16 views8 pages

Selection Sort

Selection sort is an in-place comparison sorting algorithm that divides an array into two parts - a sorted part on the left and an unsorted part on the right. It works by selecting the smallest element from the unsorted part and swapping it with the leftmost element, making it part of the sorted part. This process continues moving the unsorted part boundary to the right by one element each iteration. It has a worst-case and average time complexity of O(n^2) where n is the number of elements, making it unsuitable for large data sets.

Uploaded by

Grace Busa
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)
16 views8 pages

Selection Sort

Selection sort is an in-place comparison sorting algorithm that divides an array into two parts - a sorted part on the left and an unsorted part on the right. It works by selecting the smallest element from the unsorted part and swapping it with the leftmost element, making it part of the sorted part. This process continues moving the unsorted part boundary to the right by one element each iteration. It has a worst-case and average time complexity of O(n^2) where n is the number of elements, making it unsuitable for large data sets.

Uploaded by

Grace Busa
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/ 8

Selection Sort

Group 1:

Agillon, Marjorie
Agillon, Arvin Jay
Agon, James Warren
Aguho, Marc Lawrence
Albania, Sairon Lecs
Bueno, Jamaica Sorrel
What is Algorithm?

An algorithm is a set of instructions designed


to perform a specific task. In computer
programming, algorithms are often created as
functions. These functions serve as small
programs that can be referenced by a larger
program.
Selection Sort:

Selection sort is a simple sorting algorithm. 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.
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.
This algorithm is not suitable for large data
sets as its average and worst case complexities
are of Ο(n2), where n is the number of items.
Example:
Array = [23, 34, 12, 56, 27, 8]

• [23, 34, 12, 56, 27, 8].


• First we need to swap the left and right element of the array.
• [8, 34, 12, 56, 27, 23]

• [8, 34, 12, 56, 27, 23]


• Second, we need to swap 32 and 12, because 12 is less than 34.
• [8, 12, 34, 56, 27, 23]

• [8, 12, 34, 56, 27, 23]


• Third, we will swap 34 and 23 because 23 is less than 34 .
• [8, 12, 23, 56, 27, 34]
• [8, 12, 23, 56, 27, 34]
• Fourth, we will swap 56 and 27 because 27 is less than 56 .
• [8, 12, 23, 27, 56, 34]

• [8, 12, 23, 27, 56, 34]


• And the last step is swap 56 and 34 because 56 is higher than 34 and needs to be at the last of the array.
• [8, 12, 23, 27, 34, 56]

Sorted array: [8, 12, 23, 27, 34, 56]


Pseudo Code of Selection Sort
list : array of items
n : size of list

for i = 1 to n – 1

/* set current element as minimum*/


min = i 

/* check the element to be minimum */

for j = i+1 to n 
if list[j] < list[min] then
min = j;
end if
end for

/* swap the minimum element with the current element*/


if indexMin != i then
swap list[min] and list[i]
end if
end for

end procedure

You might also like