Various functions for sorting using selection sort.
/**
* Function for sorting array in ascending order
*
* @param: array | an array of integer values to sort
**/
void selectionSort(int[] numbers) {
int i = 0; // Outer loop index tracking
int j = 0; // Inner loop index tracking
int indexSmallest = 0; // Store index of smallest value in loops
int temp; // Temporary variable for swapping values in array
// Loop through entire array list
for (i = 0; i < numbers.length - 1; ++i) {
// Set indexSmallest to current value
indexSmallest = i;
// Loop through array list at position one above current
for (j = i + 1; j < numbers.length; ++j) {
// Check if value in inner loop is smaller than current smallest value
if ( numbers[j] < numbers[indexSmallest] ) {
// Update the index of smallest value to index of current smaller value
indexSmallest = j;
}
}
// Swap numbers[i] and numbers[indexSmallest]
temp = numbers[i]; // Assign the starting value in outer loop to temp
numbers[i] = numbers[indexSmallest]; // Assign starting value to smallest value
numbers[indexSmallest] = temp; // Assign larger starting value to vacated location
}
}
/**
* Function for sorting array in descending order
*
* @param: array | an array of integer values
**/
void selectionSortDesc(int[] numbers) {
int i = 0; // Outer loop index tracking
int j = 0; // Inner loop index tracking
int indexLargest = 0; // Store index of largest value in loops
int temp; // Temporary variable for swapping values in array
// Loop through entire array list
for (i = 0; i < numbers.length - 1; ++i) {
// Set indexLargest to current value
indexLargest = i;
// Loop through array list at position one above current
for (j = i + 1; j < numbers.length; ++j) {
// check if value in inner loop is larger than current largest value
if ( numbers[j] > numbers[indexLargest] ) {
// Update the index of largest value to index of current largest value
indexLargest = j;
}
}
// Swap numbers[i] and numbers[indexLargest]
temp = numbers[i]; // assign the starting value in outer loop to temp
numbers[i] = numbers[indexLargest]; // assign starting value to largest value
numbers[indexLargest] = temp; // assign larger starting value to vacated location
}
}
/**
* Function for sorting array in descending order for three values (PVector)
*
* @param: array | an array of integer values to sort
**/
void selectionSort(PVector[] numbers) {
int i = 0; // Outer loop index tracking
int j = 0; // Inner loop index tracking
int indexSmallest = 0; // Store index of smallest value in loops
int temp; // Temporary variable for swapping values in array
// Loop through entire array list
for (i = 0; i < numbers.length - 1; ++i) {
// Set indexSmallest to current value
indexSmallest = i;
// Loop through array list at position one above current
for (j = i + 1; j < numbers.length; ++j) {
// Check if x value of inner and outer loop is equal
if (numbers[j].x == numbers[indexSmallest].x) {
// Check if y value of inner and outer loop is equal
if (numbers[j].y == numbers[indexSmallest].y) {
// Check if z value of inner loop is less than current smallest value
if (numbers[j].z < numbers[indexSmallest].z) {
// update the index of smallest value to index of current smallest value
indexSmallest = j;
}
}
// If y values are not the equal check if inner y value is less than current smallest value
if (numbers[j].y < numbers[indexSmallest].y) {
indexSmallest = j;
}
}
// If x values are not equal check if inner x valu is less than current smallest value
if ( numbers[j].x < numbers[indexSmallest].x) {
indexSmallest = j;
}
}
// Swap numbers[i] and numbers[indexSmallest]
temp = numbers[i];
numbers[i] = numbers[indexSmallest];
numbers[indexSmallest] = temp;
}
}
/**
* Function for sorting array in descending order for three values (PVector)
* while using seperate PVector for tracking index changes
*
* @param: array | an array of integer values to sort
* @param: array | an array of integer values to track index changes
**/
void SelectionSort(PVector[] numbers, PVector[] changes) {
int i = 0; // Outer loop index tracking
int j = 0; // Inner loop index tracking
int indexSmallest = 0; // Store index of smallest value in loops
int temp; // Temporary variable for swapping values in array
// Loop through entire array list
for (i = 0; i < numbers.length - 1; ++i) {
// Set indexSmallest to current value
indexSmallest = i;
// Loop through array list at position one above current
for (j = i + 1; j < numbers.length; ++j) {
// Check if x value of inner and outer loop is equal
if (numbers[j].x == numbers[indexSmallest].x) {
// Check if y value of inner and outer loop is equal
if (numbers[j].y == numbers[indexSmallest].y) {
// Check if z value of inner loop is less than current smallest value
if (numbers[j].z < numbers[indexSmallest].z) {
// update the index of smallest value to index of current smallest value
indexSmallest = j;
}
}
// If y values are not the equal check if inner y value is less than current smallest value
if (numbers[j].y < numbers[indexSmallest].y) {
indexSmallest = j;
}
}
// If x values are not equal check if inner x valu is less than current smallest value
if ( numbers[j].x < numbers[indexSmallest].x) {
indexSmallest = j;
}
}
// Swap numbers[i] and numbers[indexSmallest]
temp = numbers[i];
numbers[i] = numbers[indexSmallest];
numbers[indexSmallest] = temp;
// swap changes list to track index swaps
temp = changes[i];
changes[i] = changes[indexSmallest];
changes[indexSmallest] = temp;
}
}