-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.java
More file actions
37 lines (31 loc) · 868 Bytes
/
Copy pathselection_sort.java
File metadata and controls
37 lines (31 loc) · 868 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
37
class Solution {
static void selectionSort(int[] arr) {
// code here
int n = arr.length;
for(int i = 0 ; i<n-1;i++){
int mi=i;
for(int j = i+1;j<n;j++){
if (arr[j]<arr[mi]){
mi=j;
}
}
int temp = arr[i];
arr[i]=arr[mi];
arr[mi]=temp;
}
}
static void printArray(int[] arr){
for(int val: arr){
System.out.print(val+ " ");
}
System.out.println();
}
public static void main(String[] args){
int[] arr = {4,1,3,9,7};
System.out.println("Original array: ");
printArray(arr);
selectionSort(arr);
System.out.println("Sorted array: ");
printArray(arr);
}
}