You are on page 1of 6

G.H.

RAISONI COLLEGE OF ENGINEERING


(An Autonomous Institute Under UGC act 1956 & affiliated to R.T.M. Nagpur University)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Session: 2015 2016

Subject:- Data Structure Using C


3 Semester A
TAE-1
Technical Report
on
SELECTION SORT

Submitted By:
1. Aishwarya Shirod[A-01]
2. Ashu Pawar[A-04]
3. Manaswi Mate[A-16]
4. Minakshi Jaiswal[A-18]

INTRODUCTION:
One of the basic problems of computer science is sorting a list of items. It refers to the
arranging of numerical or alphabetical or character data in statistical order (either in
increasing order or decreasing order) or in lexicographical order (alphabetical value like
addressee key) [1-3]. There are a number of solutions to this problem, known as sorting
algorithms. There are several elementary and advanced sorting algorithms. Some sorting
algorithms are simple and spontaneous, such as the bubble sort. Others, such as the quick sort
are enormously complex, but produce super-fast results. Some sorting algorithm work on less
number of elements, some are suitable for floating point numbers, some are good for specific
range, some sorting algorithms are used for huge number of data, and some are used if the list
has repeated values. Other factors to be considered in choosing a sorting algorithm include
the programming effort, the number of words of main memory available, the size of disk or
tape units and the extent to which the list is already ordered . That means all sorting
Algorithms are problem specific, meaning they work well on some specific problem and do
not work well for all the problems. However, there is a direct correlation between the
complexity of an algorithm and its relative effectiveness . Many different sorting algorithms
have been developed and improved to make sorting fast.
The classical bubble sort, selection sort, and insertion sort are very simple
algorithms, often included in the text books to introduce algorithms and
sorting, having runtime complexity of (n2) making them impractical to
use. The selection sort has a slight better running time than the simplest
bubble sort algorithm and worse than the insertion sort.

THEORY:
Here is an example of this sort algorithm sorting 6 elements:

Selection sort can also be used on list structures that make add and remove efficient, such as a
linked list. In this case it is more common to remove the minimum element from the
remainder of the list, and then insert it at the end of the values sorted so far.

ALGORITHM:
Selection Sort (a[], length)
Here a is the unsorted input list and length is the length of array. After completion of the
algorithm array will become sorted. Variable max keeps the location of the maximum value.
1. Repeat steps 2 to 5 until length=1
2. Set max=0
3. Repeat for count=1 to length
If (a[count]>a[max])
Set max=count
End if
4. Interchange data at location length-1 and max
5. Set length=length-1

PROGRAM:
var swap = function(array, firstIndex, secondIndex) {
var temp = array [firstIndex];
array [firstIndex] = array[secondIndex];
array [secondIndex] = temp;
};
var indexOfMinimum = function(array, startIndex) {

var minValue = array[startIndex];


var minIndex = startIndex;

for(var i = minIndex + 1; i < array.length; i++) {


if(array[i] < minValue) {
minIndex = i;
minValue = array[i];
}
}
return minIndex;
};
var selection Sort = function(array) {

};

var array = [22, 11, 99, 88, 9, 7, 42];


selectionSort(array);
println("Array after sorting: " + array);

//Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);

ANALYSIS:
Selection sort loops over indices in the array; for each index, selection sort calls
indexOfMinimum and swap. If the length of the array is n, there are n indices in the
array.
Since each execution of the body of the loop runs two lines of code, you might think that 2n2
n2n lines of code are executed by selection sort. But it's not true! Remember that
indexOfMinimum and swap are functions: when either is called, some lines of code are
executed.
How many lines of code are executed by a single call to swap? In the usual implementation,
it's three lines, so that each call to swap takes constant time.
How many lines of code are executed by a single call to indexOfMinimum? We have to
account for the loop inside indexOfMinimum. How many times does this loop execute in a
given call to indexOfMinimum? It depends on the size of the subarray that it's iterating
over. If the subarray is the whole array (as it is on the first step), the loop body runs nnn
times. If the subarray is of size 6, then the loop body runs 6 times.
For example, let's say the whole array is of size 8 and think about how selection sort works.
1. In the first call of indexOfMinimum, it has to look at every value in the array, and
so the loop body in indexOfMinimum runs 8 times.
2. In the second call of indexOfMinimum, it has to look at every value in the subarray
from indices 1 to 7, and so the loop body in indexOfMinimum runs 7 times.
3. In the third call, it looks at the subarray from indices 2 to 7; the loop body runs 6
times.
4. In the fourth call, the subarray from indices 3 to 7; the loop body runs 5 times.
5.
6. In the eighth and final call of indexOfMimimum, the loop body runs just 1 time.
If we total up the number of times the loop body of indexOfMinimum runs, we get 8 + 7 +
6 + 5 + 4 + 3 + 2 + 1 = 36 times.
Best case: in every pass statement 5 will be executed just once and hence the comparison in
statement 6 will be accomplished also once and hence, T(n) = n-1 = O(n). The best-case
complexity of the classical selection sort is O(n2).

Worst case: in the iteration no k, when k-1 items are already sorted and n-k+1 items are still
unsorted, n-k comparisons are required to find the largest element. Thus in this case T(n) =
(n-1) + (n-2) +(n-3) + . + 3 + 2 + 1 = n(n-1)/2 = (n2-n)/2 = O(n2).
Average case: probabilistically an item is greater than half of the items in the list and smaller
than the other half in the list. So, in iteration k, when k-1 items are already sorted and n-k+1
items are still unsorted, (n-k)/2 comparisons are required to find the current maximum. This
leads to T(n) = ((n-1) + (n-2) + (n-3) + . + 3 + 2 + 1)/2 = n(n-1)/4 = (n2-n)/4 = O(n2).

CONCLUSION:
Sorting a list of items into ascending or descending order can help either a human or a
computer find items on that list quickly, perhaps using an algorithm like binary search.
JavaScript has a built-in sorting method. It works on arrays of numbers, or even on arrays of
strings.
Even though JavaScript has a built-in sorting method, sorting is a great example of how there
may be many ways to think about the same problem, some perhaps better than others.
Understanding sorting is a traditional first step towards mastery of algorithms and computer
science.

REFERENCES:
1] Khan Academy
[https://www.khanacademy.org/computing/computer-science/algorithms/sortingalgorithms/a/analysis-of-selection-sort]
2] PDF attached.