public class SortingStuff { /**************************************** * Bubble Sort ****************************************/ /* * bubble(int[] arr) * * @param arr is the int array to bubble in place. * * @return true if there were any elements out of order and false otherwise * * Bubbling involves comparing sequential pairs of items and swapping them * if they are out of order. The method should modify the input array arr. */ public static boolean bubble(int[] arr) { boolean swappedAny = false; // TODO write bubble return swappedAny; } /* * bubbleSort(int[] arr) * * @param arr the array to sort (in place) * * @return the number of times we called bubble before there were no swaps * necessary. * * bubbleSort should not create a new array and should modify the array arr * that is passed as an argument to the method. */ public static int bubbleSort(int[] arr) { int bubbleCount = 0; // TODO write bubbleSort return bubbleCount; } /**************************************** * Insertion Sort ****************************************/ /* * insert(int[] arr, int index) * * @param arr is the array that contains a sorted and unsorted portion. * * @param index is the location of the first unsorted element (starting from * index 0). * * The method should modify the array arr passed as an argument by adding * the element at location: index into the sorted portion of the array. The * elements in arr before and after calling this method should be identical, * but after calling this method the elements from 0 to index are in sorted * order. */ public static void insert(int[] arr, int index) { int itemToInsert = arr[index]; // TODO write insert } /* * insertionSort(int[] arr) * * @param arr is the array to sort. * * This method will subdivide into a sorted section and unsorted section. * Initially the array starts out with zero elements sorted and elements 0 * to length-1 unsorted. It will iterate through the elements to add a new * element to the sorted section of the array. * * This method should modify arr and not create an additional array. */ public static void insertionSort(int[] arr) { // TODO write insertionSort } /**************************************** * Selection Sort ****************************************/ /* * minIndex(int[] arr, int startIndex) * * @param arr is the array with a sorted and unsorted portion * * @param startIndex indicates the first index in arr that is not sorted. * The array is sorted from elements 0 to startIndex-1. * * @return the index of the smallest value between startIndex and * arr.length-1 (inclusive) */ public static int minIndex(int[] arr, int startIndex) { // TODO write minIndex; return 0; } /* * selectionSort(int[] arr) * * @param arr is the array to be sorted. It will get progressively sorted by * finding the smallest element in the array and moving it into the correct * spot. * * The method will modify the argument arr and will not create any new * arrays. */ public static void selectionSort(int[] arr) { // TODO write selectionSort } /* * isSorted(int[] arr) * * @param arr is the array to check if it is sorted (from small to large) * * @return true if the array is sorted small to large, otherwise false * * The method is intended to be used by the JUnit test caess. */ public static boolean isSorted(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { return false; } } return true; } }