forked from yshshrm/Algorithms-And-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathselection_sort.java
More file actions
29 lines (28 loc) · 821 Bytes
/
Copy pathselection_sort.java
File metadata and controls
29 lines (28 loc) · 821 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
/**
* Created by renu.yadav on 20/10/17.
*/
public class selection_sort {
/**
* selection sort in iteration , picks up the ith min element and keep it in place
* so after every ith iteration ith minimum element is in place
* takes O(n^2)
* @param arr
* @return
*/
public static int[] selectionSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int minIndex = i;
int minElement = arr[minIndex];
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < minElement) {
minIndex = j;
}
}
// swap , arr[i] with minElement
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
}