Skip to content

Commit 394154b

Browse files
committed
Selection sort code added
1 parent aa71354 commit 394154b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Sorting/Selection Sort/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Selection Sort
2+
3+
**Selection sort** is one of the simplest sorting algorithm.
4+
It works by selecting the smallest (or largest, if you want to sort from big to small) element of the array and placing it at the head of the array. Then the process is repeated for the remainder of the array; the next largest element is selected and put into the next slot, and so on down the line.
5+
6+
![Selection Sort](selection_sort.jpg)
7+
8+
Selection sort algorithm starts by compairing first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is compared. This completes the first step of selection sort.
9+
10+
If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get required result.
11+
12+
#####A visualization on Selection Sort
13+
![Selection sort demonstration](https://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif)
14+
15+
*Selection sort animation. Red is current min. Yellow is sorted list. Blue is current item.*
16+
17+
####Complexity Analysis
18+
- Worst Case - O(n<sup>2</sup>)
19+
- Average Case - O(n<sup>2</sup>)
20+
- Best Case - O(n<sup>2</sup>)
21+
22+
### More on this topic
23+
- https://en.wikipedia.org/wiki/Selection_sort
24+
- http://quiz.geeksforgeeks.org/selection-sort/
25+
- https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Simple Selection Sort implementation
2+
function selectionSort(arr){
3+
var len = arr.length;
4+
5+
/* advance the position through the entire array */
6+
/* (could do i < n-1 because single element is also min element) */
7+
for(var i=0; i<len-1; i++){
8+
// find the min element in the unsorted a[i .. n-1]
9+
10+
var currentMin = i; // assume the min is the first element
11+
// test against elements after i to find the smallest
12+
for (var j=i+1; j<len; j++)
13+
{
14+
// if this element is less, then it is the new minimum
15+
if (arr[j] < arr[currentMin]) //To sort in decreasing order just change the comparison operator to '>'
16+
currentMin = j; // found new minimum; remember its index
17+
}
18+
19+
if(currentMin != i) //if the current_Min is equal to i, then it is in right position already
20+
{
21+
//Swap the values
22+
tmp = arr[i];
23+
arr[i] = arr[currentMin];
24+
arr[currentMin] = tmp;
25+
}
26+
}
27+
28+
return arr;
29+
}
30+
31+
/******************* Testing Selection sort algorithm *********************/
32+
33+
/**
34+
* Returns a random integer between min (inclusive) and max (inclusive)
35+
* Using Math.round() will give you a non-uniform distribution!
36+
*/
37+
function getRandomInt(min, max) {
38+
return Math.floor(Math.random() * (max - min + 1)) + min;
39+
}
40+
41+
var arr = [];
42+
43+
for(var i=0;i<10;i++){ //initialize a random integer unsorted array
44+
arr.push(getRandomInt(1, 100));
45+
}
46+
47+
console.log("Unsorted array: ");
48+
console.log(arr); //printing unsorted array
49+
50+
arr = selectionSort(arr);
51+
console.log("Sorted array: ");
52+
console.log(arr); //printing sorted array
17.6 KB
Loading

0 commit comments

Comments
 (0)