Skip to content

Commit bed4a91

Browse files
committed
Selection sort code comment and doc updated
1 parent a5efc8f commit bed4a91

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

Sorting/Selection Sort/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Selection sort algorithm starts by compairing first two elements of an array and
99

1010
If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get required result.
1111

12-
#####A visualization on Selection Sort
12+
#### A visualization on Selection Sort
1313
![Selection sort demonstration](https://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif)
1414

1515
*Selection sort animation. Red is current min. Yellow is sorted list. Blue is current item.*
1616

17-
####Complexity Analysis
17+
#### Complexity Analysis
1818
- Worst Case - O(n<sup>2</sup>)
1919
- Average Case - O(n<sup>2</sup>)
2020
- Best Case - O(n<sup>2</sup>)

Sorting/Selection Sort/selection-sort.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
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;
1+
/* Selection Sort implementation in JavaScript */
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+
// if this element is less, then it is the new minimum
14+
if (arr[j] < arr[currentMin]) //To sort in decreasing order just change the comparison operator to '>'
15+
currentMin = j; // found new minimum; remember its index
16+
}
17+
18+
if (currentMin != i) //if the current_Min is equal to i, then it is in right position already
19+
{
20+
//Swap the values
21+
tmp = arr[i];
22+
arr[i] = arr[currentMin];
23+
arr[currentMin] = tmp;
24+
}
25+
}
26+
27+
return arr;
2928
}
3029

3130
/******************* Testing Selection sort algorithm *********************/
@@ -35,13 +34,13 @@ function selectionSort(arr){
3534
* Using Math.round() will give you a non-uniform distribution!
3635
*/
3736
function getRandomInt(min, max) {
38-
return Math.floor(Math.random() * (max - min + 1)) + min;
37+
return Math.floor(Math.random() * (max - min + 1)) + min;
3938
}
4039

4140
var arr = [];
4241

43-
for(var i=0;i<10;i++){ //initialize a random integer unsorted array
44-
arr.push(getRandomInt(1, 100));
42+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
43+
arr.push(getRandomInt(1, 100));
4544
}
4645

4746
console.log("Unsorted array: ");

0 commit comments

Comments
 (0)