-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSelectionSort.java
More file actions
31 lines (27 loc) · 873 Bytes
/
SelectionSort.java
File metadata and controls
31 lines (27 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class SelectionSort implements SortingStrategy
{
public int[] sort( int[] inputArray )
{
// find the smallest element starting from position i
for (int i = 0; i < inputArray.length - 1; i++)
{
int min = i; // record the position of the smallest
for (int j = i + 1; j < inputArray.length; j++)
{
// update min when finding a smaller element
if (inputArray[j] < inputArray[min])
min = j;
}
// put the smallest element at position i
swap(inputArray, i, min);
}
System.out.println("Array is sorted using Selection Sort Algorithm");
return inputArray;
}
private void swap( int[] inputArray,int k, int l )
{
int temp = inputArray[k];
inputArray[k] = inputArray[l];
inputArray[l] = temp;
}
}