Princeton1/module5/src/main/java/com/hithomelabs/princeton1/module5/Selection.java
hitanshu310 7ba2d22e43
Some checks failed
sample gradle build and test / build (pull_request) Failing after 1m18s
Insertion sort and selection sort with test cases
2025-02-01 16:34:39 +05:30

24 lines
689 B
Java

package com.hithomelabs.princeton1.module5;
public class Selection<E> extends AbstractCustomSorts<E>{
/*
* * Selection sort "selects" the smallest element and swaps it with arr[0] of the array
* * Then proceeds to do the same swapping arr[i] with arr[i:arr.length-1]
*/
public void sort(E[] arr){
if (arr == null) return;
Comparable<E>[] arr1 = (Comparable<E>[]) arr;
for(int i = 0; i < arr1.length - 1; i++){
int minIndex = i;
for(int j = i+1; j < arr.length; j ++){
if (less((Comparable<E>) arr[j], arr[minIndex])) minIndex = j;
}
exch(arr, i, minIndex);
}
}
}