Selection Sort Algorithm in Data Structure
• In this algorithm, elements of an Array list[N] are sorted into an ascending order.
• Two variables SMALL and POS have been used to locate the smallest element in the unsorted part of the array LIST.
• Temp is variable used to interchange the selected element with the first element of the unsorted part of the array LIST.
Steps of Selection Sort Algorithm
1• for i=1 to N-1 repeat steps 2 to 7 2• SMALL= LIST[i]; POS=i 3• for j=i+1 to N repeat step 4 4• if(LIST[J]>SMALL) then { SMALL =LIST[j] POS=j { end of loop j } 5• TEMP= LIST[i]; 6• LSIT[i]=LIST[Pos] 7• LIST[POS]=TEMP { End of loop i } 8• End
• In this we took two loops, the outer loop i and inner loop j.
• The number of comparisons needed for N element is N-1, so outer loop i terminates at N-1 and inner j loop always start from i+1th location record comparison has to start from second element of uncertain part of the array LIST.
• In this interchange will take place outside the body of loop j i.e from step 5 to 7.
17- Array- Selection Sort Part-2- DataStructure Tutorials
CLICK HERE TO Download This PDF NOTES