0% found this document useful (0 votes)
5 views

Unit2 Selection Sort

Uploaded by

4ghj9zrgy2
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)
5 views

Unit2 Selection Sort

Uploaded by

4ghj9zrgy2
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/ 23

Unit 2

Program for selection sort


Aim:
• To implement selection sort
Theory:
• The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from the unsorted
part and putting it at the beginning.
• The algorithm maintains two subarrays in a given array.

• The subarray which already sorted.


• The remaining subarray was unsorted.
• In every iteration of the selection sort, the minimum element
(considering ascending order) from the unsorted subarray is picked
and moved to the sorted subarray
How selection sort works?
Another example
• As shown in the above illustration,
• for N number of elements we take N-1 passes to completely sort the
array.
• At the end of every pass, the smallest element in the array is placed at
its proper position in the sorted array.
Procedure:
Algorithm
• Initialize minimum value to element at location 0.(min_element)
• Traverse the array to find the minimum element in the array.
• While traversing if any element smaller than min_element is found
then swap both the values.
• Then, increment index to point to the next element.
• Repeat until the array is sorted.
Selection Sort Algorithm

• selectionSort(array, size)
• repeat (size - 1) times
• set the first unsorted element as the minimum
• for each of the unsorted elements
• if element < currentMinimum
• set element as new minimum
• swap minimum with first unsorted position
• end selectionSort
Source code:
output

You might also like