Skip to content

Instantly share code, notes, and snippets.

@ArielSaldana
Created June 16, 2016 18:34
Show Gist options
  • Save ArielSaldana/4fe4d229816016b5a2b3af5cc6bcbd09 to your computer and use it in GitHub Desktop.
Save ArielSaldana/4fe4d229816016b5a2b3af5cc6bcbd09 to your computer and use it in GitHub Desktop.
/**
* 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