1. Name
Reg #
MANNUAL
SELECTION SORT
Selection sort is a sorting algorithm, specifically an in-place comparison sort. It
has O(n2) time complexity, making it inefficient on large lists, and generally performs
worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has
performance advantages over more complicated algorithms in certain situations,
particularly where auxiliary memory is limited
Aim:
Write a program to Sort values in Ascending or Descending Order using Selection
Sort Technique in Linear Array.
Theory:
The algorithm divides the input list into two parts: the sublist of items already
sorted, which is built up from left to right at the front (left) of the list, and the sublist of
items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is
empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding
the smallest (or largest, depending on sorting order) element in the unsorted sublist,
exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order),
and moving the sublist boundaries one element to the right
Selection sort carries out a sequence of passes over the table. At the first pass an entry
is selected on some criteria and placed in the correct position in the table. The possible
criteria for selecting an element are to pick the smallest or pick the largest. If the
smallest is chosen then, for sorting in ascending order, the correct position to put it is at
the beginning of the table. Now that the correct entry is in the first place in the table the
process is repeated on the remaining entries. Once this has been repeated n-1 times
the n-1 smallest entries are in the first n-1 places which leaves the largest element in
the last place. Thus onlyn-1 passes are required. The algorithm can be described as
follows
Suppose the input array has n elements. The outer loop runs n-1 rounds, roughly one
for each position. Inside each outer loop, the inner loop goes through the unsorted part
of the array. On average, each inner loop scans through n/2 elements. The total time is
2. roughly t(n) = (n – 1) * (n/2) *k = O(n^2), where k denotes the number of basic
operations inside each inner loop; the constants are absorbed in the big-Oh notion.
Note that in the big-Oh notion, we only care about the dominating factor (which is n^2 in
this case)
Selection sort is not difficult to analyze compared to other sorting algorithms since none
of the loops depend on the data in the array. Selecting the lowest element requires
scanning all n elements (this takes n − 1 comparisons) and then swapping it into the first
position. Finding the next lowest element requires scanning the remaining n − 1
elements and so on, for (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 ∈ Θ(n2) comparisons
(see arithmetic progression). Each of these scans requires one swap for n − 1 elements
(the final element is already in place)
Algorithm:
Here A is an Array with N elements stored in it. This algorithm sorts
the elements in A.
1. Repeat Steps 2 to 4 for Pass = 1 to N – 1
2. Set Swapped = 0 and K = 0
3. Repeat while K < (N – Pass)
(a) if A[ K ] > A[ K + 1] then
Interchange A[ K ] and A[ K + 1 ] and
Set Swapped = 1
[ End of if structure. ]
(b) Set K = K + 1
[ End of inner loop of step-3. ]
4. if Swapped = 0 then break the outer loop of step-1
[ End of outer loop of step-1. ]
for i from 1 to n-1
{
Find smallest element in ith to nth entries.
Exchange this element with ith entry.
}
3. Example:
Suppose the input array has n elements. The outer loop runs n-1 rounds, roughly one
for each position. Inside each outer loop, the inner loop goes through the unsorted part
of the array. On average, each inner loop scans through n/2 elements. The total time is
roughly t(n) = (n – 1) * (n/2) *k = O(n^2), where k denotes the number of basic
operations inside each inner loop; the constants are absorbed in the big-Oh notion.
Note that in the big-Oh notion, we only care about the dominating factor (which is n^2 in
this case)