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

Selection Sort

Selection Sort is a sorting technique that repeatedly finds the smallest element from an unsorted portion of an array and swaps it with the first unsorted element, ultimately arranging all elements in ascending order. The algorithm involves multiple passes through the array, with each pass reducing the number of comparisons needed. The provided document includes an example of sorting an array using selection sort and outlines the algorithm step-by-step.

Uploaded by

digicampus
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)
2 views8 pages

Selection Sort

Selection Sort is a sorting technique that repeatedly finds the smallest element from an unsorted portion of an array and swaps it with the first unsorted element, ultimately arranging all elements in ascending order. The algorithm involves multiple passes through the array, with each pass reducing the number of comparisons needed. The provided document includes an example of sorting an array using selection sort and outlines the algorithm step-by-step.

Uploaded by

digicampus
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

Data Structure

Selection Sort
SELECTION SORT

 This sorting technique begins by comparing first element in the given array
of elements with the rest of the elements to find smallest element and then
that is exchanged with the first element.
 Then the process is repeated with the second element to find second smallest
element and is exchanged with the second element and so on.
 Finally , all elements will be arranged in ascending order.
 Since , the next least element is selected and exchanged appropriately so that
elements are finally sorted, this technique is called Selection sort.
Eg: Consider following array A as below and sort it using selection sort.
A[0] A[1] A[2] A[3] A[4]
A 45 20 40 5 15

Begin by assuming that the first smallest element is in The position 0 i.e, small=A[0] and
pos=0. Then as smaller element is found assign that to small and its position to pos.
45 20 40 5 15
A[1]<small , small=20, pos=1.

45 20 40 5 15

A[2]>small, small=20,pos=1
45 20 40 5 15
Pass I
45 20 40 5 15
A[3]<small, small=5, pos=3
Begin with small=20 and pos=1
5 20 40 45 15
A[2]>small, small=20 and pos=1

5 20 40 45 15
Pass II

5 20
A[3]>small, small=2040 45
and pos=1 15

A[4]<small, small=15 and pos=4


5 15 40 45 20

Initially
5 small=40
15 and40
pos=2 45 20

A[3]>small, small=40 and pos=2


5 15 20 45 40
Pass III
5 15 20 40 45
A[4]<small, small=20 and pos=4
Initially small=45 and pos=3
 In the selection sort, for a given array of n elements there are n-1 comparisons
in the first pass.
 At the end of first pass first smallest will be placed at the 0 th location.
 In the second pass number of comparisons one less than that of first pass i.e.n-2
comparisons and at the end second smallest goes to 1st location.
 This process continues until n-1 passes i.e. there are only 2 elements left to
arrange.
Algorithm:
SELECTION_SORT(A, n) This algorithm sorts given array A[n] using selection sort technique. Two
variables I and J are used to index the array elements.
Step1: start
Step2: Input the array A[n]
Step3: [Compute sorting]
Repeat For I  0 to n-1
Step4: [Assume first element as smallest]
Step5: smallA[I]
Step6: posI
Step7: Repeat For JI+1 to n-1
Step8: [Compare adjacent elements]
If (A[J]<small) Then
Step9: smallA[J]
Step10: posJ
[EndIf]
[End of step7 for loop]
Step11: [Exchange Ith with smallest element]
A[pos]A[I]
Step12: A[I]small
[End of step3 for loop]
for(int i = 0; i < n-1; i++){ A[0] A[1]
A[2] A[3] A[4] 45 20 40 5 15
int small = A[i]; A
int pos = i; 5 20 40 45 15
for(int j = i + 1; j<n; j++){
if(A[j] < small){
small = A[j];
pos = j;
}
}
A[pos] = A[i];
A[i] = small;
}
THANK YOU

You might also like