-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
36 lines (31 loc) · 920 Bytes
/
SelectionSort.java
File metadata and controls
36 lines (31 loc) · 920 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
32
33
34
35
36
public class SelectionSort {
public static int[] Ssort(int arr[]) {
// Idea: pick the smallest( from unsorted) ,put it at the beginning of the
// array.
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[min] > arr[j]) {
min = j;
}
}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
return arr;
// Time Complexity=O(n2) Space Comlexity=O()
}
public static void main(String[] args) {
int ar[] = { 5, 4, 1, 3, 2 };
for (int num : ar) {
System.out.print(num + " ");
}
System.out.println();
Ssort(ar);
for (int num : ar) {
System.out.print(num + " ");
}
}
}