Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Created by Ariel on 6/16/2016.
*/
public class SelectionSort {
public static void sortLoop ( int arr [] ) {
int lowest = 0, current = 0, index = 0;
int i, j; // loops indexes
for ( i = 0; i < arr.length; i++) {
current = arr[i];
lowest = arr[i];
for ( j = i; j < arr.length; j++) {
if (arr[j] < lowest) {
lowest = arr[j];
index = j;
}
}
arr[i] = lowest;
arr[index] = current;
}
}
/*
* Recursive Implementation of Selection Sort
*/
public static void sortRecursively ( int arr [], int start, int end) {
int current = arr[start];
int lowest = arr[start];
int index = start;
if (end-1 > start) {
for ( int i = start; i < end; i++) {
if ( arr[i] < lowest) {
index = i;
lowest = arr[i];
}
}
arr[start] = lowest;
arr[index] = current;
sortRecursively(arr, start+1, end);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment