-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSortArray.java
More file actions
33 lines (29 loc) · 868 Bytes
/
Copy pathSortArray.java
File metadata and controls
33 lines (29 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
package algorithms.study;
/**
* created by cenkc on 5/10/2021
*/
public class SortArray {
public static void main(String[] args) {
SortArray sa = new SortArray();
int[] arr = {3,8,11,4,7,22,16,18};
int[] sortedArr = sa.selectionSort(arr);
sa.printArray(sortedArr);
}
private int[] selectionSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length ; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
private static void printArray(int[] sortedArr) {
for (int i = 0; i < sortedArr.length; i++) {
System.out.println("sortedArr[i] = " + sortedArr[i]);
}
}
}