# selectionSort

Requirements: Compare the efficiency of Selection Sort, Insertion Sort and Quicksort.

Approach: Starting from your second homework assignment, evaluate the efficiency of Selection Sort, Insertion Sort and Quicksort. For doing this, you should evaluate their corresponding implementations in each of the 3 cases (best, worst, and average) and count the number of operations performed (assignments, comparisons, and overall, separately).
Selection Sort code:
// Method: selectionSort
// Author: instructorX
// Date: dd/mm/yyyy
// Purpose: A method that sorts an array using a selection sort

public static void selectionSort(Comparable[] array)
{
int i;
for (i = 0; i < array.length; i++) { int minIndex = i; for (int j = i + 1; j < array.length; j++) if (array[j].compareTo(array[minIndex]) < 0) minIndex = j; swap(array, minIndex, i); } } Insertion Sort code: // Method: insertionSort // Author: instructorX // Date: dd/mm/yyyy // Purpose: A method that sorts an array using an insertion sort public static void insertionSort(Comparable[] array) { for (int i = 1; i < array.length; i++) { Comparable temp = array[i]; int j = i - 1; while (j >= 0 && temp.compareTo(array[j]) < 0) { array[j + 1] = array[j]; j--; } array[j +1 ] = temp; } } QuickSort Code: public static void quickSort(int [] items, int leftIndex, int rightIndex) { int i, j, temp, pivotValue, partitionSize; partitionSize = rightIndex - leftIndex + 1; if(partitionSize <= 1) // base case, one item to be sorted return; pivotValue = items[(leftIndex + rightIndex) / 2]; i = leftIndex; // initialize the two partition indices j = rightIndex; // look for items in wrong partitions and switch them do { while (items[i] < pivotValue) // left item is in correct partition i++; while (items[j] > pivotValue) // right item is in correct partition
j–;