Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Sorts.java
- */
- package SortingFiles;
- /**
- *
- * @author Mathew Harmon
- */
- public class Sorts
- {
- /**
- *
- * @param array
- * Description: which merely calls quickSort(array, 0, array.length - 1)
- */
- public static void quickSort(Comparable[] array)
- throws java.lang.ArrayIndexOutOfBoundsException{
- quickSort(array, 0, array.length - 1);
- }
- /**
- *
- * @param array
- * @param from
- * @param to
- * Description: if indices not in array which implements the Optimized
- quick sort algorithm as presented in class, utilizing the following
- methods: insertionSort,partition,swap, and sortFirstMiddleLast.
- * @see insertionSort
- * @see partition
- * @see swap
- * @see sortFirstMiddleLast
- */
- public static void quickSort(Comparable[] array, int from, int to)
- throws java.lang.ArrayIndexOutOfBoundsException {
- if( array.length <= THREE) {
- insertionSort(array, from, to);
- }
- else{
- int mid = partition(array, from, to);
- quickSort(array, from, mid -1);
- quickSort(array, mid +1, to);
- }
- }
- /**
- *
- * @param array
- * @param from
- * @param to
- */
- public static void insertionSort(Comparable[] array, int from, int to)
- throws java.lang.ArrayIndexOutOfBoundsException
- //i think this is right?....
- {
- Comparable temp;
- for(int i = from; from < array.length; i++){
- temp = array[i];
- int j = to;
- for(j = i; j > 0; j--)
- if(temp.compareTo(array[j - 1]) < 0)
- array[j] = array[j - 1];
- else
- break;
- array[j] = temp;
- }
- }
- /**
- *
- * @param array
- * @param from
- * @param to
- * Description: sets a midpoint, calls sortFirstMiddleLast,
- moves data around the pivot value, and returns the pivot index
- * @return
- */
- private static int partition(Comparable[] array, int from, int to)
- throws java.lang.ArrayIndexOutOfBoundsException {
- //not the right return just putting that for now
- //this is the main bulk of the code!
- sortFirstMiddleLast(array,from,to,mid);
- return 0;
- }
- /**
- *
- * @param array
- * @param from
- * @param to
- */
- private static void swap(Comparable[] array, int from, int to)
- throws java.lang.ArrayIndexOutOfBoundsException{
- Comparable swapped = array[from];
- array[from] = array[to];
- array[to] = swapped;
- }
- /**
- *
- * @param array
- * @param from
- * @param mid
- * @param to
- */
- private static void sortFirstMiddleLast(Comparable[] array, int from,
- int mid, int to)throws java.lang.ArrayIndexOutOfBoundsException{
- int a = from;
- int b = mid;
- int c = to;
- swap(array,from,to);
- if(a > b){
- swap(array,a,b);
- }
- if(b > c){
- swap(array,b,c);
- }
- if(a < b){
- swap(array,a,b);
- }
- }
- static final int THREE = 3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement