Skip to content

Commit 491ee2d

Browse files
Merge pull request #2 from Aishwary2004Gupta/Aishwary2004Gupta-patch-2
SelectionSort.java
2 parents 9ae4094 + c4d3820 commit 491ee2d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

SelectionSort.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class SelectionSort {
2+
public static void main(String[] args) {
3+
int[] arr = {5,6,2,1,0,-2,-5};
4+
selectionSort(arr, arr.length, 0,0);
5+
System.out.println(Arrays.toString(arr));
6+
}
7+
static void selectionSort(int[] arr, int row, int col, int max){ //max = index
8+
if (row == 0){
9+
return;
10+
}
11+
12+
if (col < row){
13+
if (arr[col] > arr[max]) { //element
14+
selectionSort(arr, row, col+1, col); //max will be equal to col
15+
}else {
16+
selectionSort(arr, row, col + 1, max);
17+
} //now there is a maximum
18+
}else { //col == row, go to next row
19+
int temp = arr[max];
20+
arr[max] = arr[row-1]; //last element in that row
21+
arr[row-1] = temp;
22+
23+
selectionSort(arr,row - 1, 0,0); //max = 0th index
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)