21 lines
615 B
Java
21 lines
615 B
Java
package com.hithomelabs.princeton1.module5;
|
|
|
|
/*
|
|
* * 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 class Selection<E> extends AbstractCustomSorts<E>{
|
|
@Override
|
|
public void sort(E[] arr) {
|
|
for (int i = 0; i < arr.length; 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);
|
|
}
|
|
}
|
|
}
|
|
|