Skip to content

Commit 592fca0

Browse files
committed
Selection sort code added
1 parent 47e5a47 commit 592fca0

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

Sorting/Selection Sort/Main.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Main
2+
{
3+
// Prints the array
4+
private static void printList(int arr[])
5+
{
6+
int n = arr.length;
7+
for (int i=0; i<n; ++i)
8+
System.out.print(arr[i] + " ");
9+
System.out.println();
10+
}
11+
12+
public static void main(String[] args)
13+
{
14+
Bubble selection = new Selection();
15+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
16+
selection.sort(arr);
17+
System.out.println("Sorted array");
18+
printList(arr);
19+
}
20+
21+
}

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/

Sorting/Selection Sort/Selection.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Selection
2+
{
3+
//Following function will sort the array in Increasing (ascending) order
4+
void sort(int arr[])
5+
{
6+
int i, j, tmp, current_Min;
7+
8+
/* advance the position through the entire array */
9+
/* (could do i < n-1 because single element is also min element) */
10+
for (i = 0; i < n-1; i++)
11+
{
12+
// find the min element in the unsorted a[i .. n-1]
13+
14+
current_Min = i; // assume the min is the first element
15+
// test against elements after i to find the smallest
16+
for ( j = i+1; j < n; j++)
17+
{
18+
// if this element is less, then it is the new minimum
19+
if (arr[j] < arr[current_Min]) //To sort in decreasing order just change the comparison operator to '>'
20+
{
21+
current_Min = j; // found new minimum; remember its index
22+
}
23+
}
24+
25+
if(current_Min != i) //if the current_Min is equal to i, then it is in right position already
26+
{
27+
//Swap the values
28+
tmp = arr[i];
29+
arr[i] = arr[current_Min];
30+
arr[current_Min] = tmp;
31+
}
32+
}
33+
}
34+
}
17.6 KB
Loading

0 commit comments

Comments
 (0)