Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.util.Random;
- /**
- * Implements various sorting algorithms.
- *
- * @author --zaux
- * @verison 1.0
- */
- public class BaseSorting {
- /**
- * Entry point for sample output.
- *
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Random rand = new Random();
- //Q1
- String[] a = {"S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"};
- show(a);
- quicksortmid(a);
- assert isSorted(a); //requires assertions enabled.
- show(a);
- //Q2
- String[] b = {"S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"};
- mergeSort(b);
- assert isSorted(b);
- show(b);
- // Produces erroneous sorting on incorrect algorithms;
- Integer[] c = {7, 3, 2, 4, 9, 10, 1,2, 22, 23, 0, 1, 10};
- show(c);
- quicksortmid(c);
- show(c);
- /*
- // Output is not formatted
- Integer[] d = new Integer[1000];
- int shrink = 1000;
- for(int x = 0; x < 1000; x++){
- //Uncomment to test with random numbers
- //d[x] = rand.nextInt(1000);
- d[x] = shrink;
- shrink--;
- }
- show(d);
- quicksortmid(d);
- show(d);
- */
- System.out.println("\n\n");
- Integer[] e = {7, 3, 2, 4, 9, 10, 1, 2, 22, 23, 0, 1, 10};
- show(e);
- mergeSort(e);
- show(e);
- Integer[] x = new Integer[100000];
- for(int xx = 0; xx < x.length; xx++)
- x[xx] = rand.nextInt(100);
- show(x);
- mergesort(x);
- show(x);
- }
- public static void quicksortmid(Comparable[] a) {
- quicksortmid(a, 0, a.length-1);
- }
- private static void quicksortmid(Comparable[] data, int min, int max){
- if(min < max){
- // create partitions
- int indexofpartition = partition(data, min, max);
- //sor the left partition (lower values)
- quicksortmid(data, min, indexofpartition-1);
- //sort the right partition (higher values)
- quicksortmid(data, indexofpartition+1, max);
- }
- }
- private static <T extends Comparable<T>>int partition(T[] data, int min, int max){
- T partitionelement, small, med, large;
- int left,right;
- int middle = (min+max)/2;
- small = data[min];
- large = data[max];
- med = data[middle];
- int partitionIndex = min;
- partitionelement = small;
- // compare the elements of the 3 indexes selected and select the partitionelement
- if (small.compareTo(med) > 0 && small.compareTo(large) < 0){
- partitionelement = small;
- partitionIndex = min;
- }
- else if(med.compareTo(small) > 0 && med.compareTo(large) < 0){
- partitionelement = med;
- partitionIndex = middle;
- }
- else if(large.compareTo(small) > 0 && small.compareTo(med) < 0){
- partitionelement = large;
- partitionIndex = max;
- }
- // JMO: fixed the swap, was swapping with middle, when i should have been swapping with
- // partition index.
- swap(data,partitionIndex, min);
- left = min;
- right = max;
- while (left < right){
- // search for an element that is > the partition element
- while (left < right && data[left].compareTo(partitionelement) <= 0)
- left++;
- // search for an element that is < the partition element
- while (data[right].compareTo(partitionelement) > 0)
- right--;
- // swap the elements
- if (left < right)
- swap(data, left, right);
- }
- // move the partition element into place
- swap(data, min, right);
- return right;
- }
- /**
- * Swaps two values at two given indexes, helps partition, which helps quicksort
- * @param data
- * @param index1
- * @param index2
- */
- public static void swap(Comparable[] data, int index1, int index2){
- Comparable tmp = data[index1];
- data[index1] = data[index2];
- data[index2] = tmp;
- }
- public static void mergesort(Comparable[] a) {
- mergeSort(a);
- }
- /**
- * Helper method for mergesort, returns sorted array through recursion;
- * @param a the array to be sorted
- * @return a sorted array
- */
- public static Comparable[] mergeSort(Comparable[] a){
- // Since this method will be recursive we set up some logic for the
- // base case since arrays of size 1 are already sorted by default
- // we only act on arrays of length > 1
- if(a.length > 1){
- int mid = (a.length)/2;
- int rest = a.length-mid;
- //initially set to size [max-mid]
- Comparable[] split1 = new Comparable[mid];
- Comparable[] split2 = new Comparable[rest];
- // Copy the items from a into the 2 sub arrays
- for(int x = 0; x < mid; x++){
- split1[x] = a[x];
- }
- for(int x = 0 ; x < rest ; x++){
- split2[x] = a[x+mid];
- }
- //Recursive call on the two subarrays
- mergeSort(split1);
- mergeSort(split2);
- // merge the two subarrays and store those values in a
- for(int x = 0; x < a.length; x++){
- a[x] = merge(split1,split2)[x];
- }
- }
- return a;
- }
- /**
- * Takes two sorted arrays and merges them by with the lowest value first;
- * @param a the sub array to merge into the merged array
- * @param b the sub array to merge into the merged array
- * @return a merged, sorted array.
- */
- public static Comparable[] merge(Comparable[] a, Comparable[] b){
- //Initial runs threw AIOB exception, the issue was that below we subtracted 1
- // from a.length and b.length which is incorrect as it should just be the length of both
- Comparable[] merged = new Comparable[(a.length)+(b.length)];
- int alast = a.length-1;
- int blast = b.length-1;
- int afirst = 0, bfirst = 0, mindex = 0;
- while((afirst <= alast) && (bfirst <= blast)){
- //
- if(a[afirst].compareTo(b[bfirst]) > 0){
- merged[mindex] = b[bfirst];
- mindex++;
- bfirst++;
- } else if (a[afirst].compareTo(b[bfirst]) < 0){
- merged[mindex] = a[afirst];
- mindex++;
- afirst++;
- } else{
- //Else the two compared values are equal
- merged[mindex] = b[bfirst];
- merged[mindex+1] = a[afirst];
- //both a/b indexes get incremented
- afirst++;
- bfirst++;
- // merge index gets incremented by 2 because we stored two values
- // on the pass
- mindex += 2;
- }
- }
- //copy remaining elements
- while(afirst <= alast){
- merged[mindex] = a[afirst];
- afirst++;
- mindex++;
- }
- while(bfirst <= blast){
- merged[mindex] = b[bfirst];
- bfirst++;
- mindex++;
- }
- return merged;
- }
- /**
- * Displays contents of array, space separated.
- * @author Sedgewick
- * @param a Array to display.
- */
- private static void show(Comparable[] a) {
- for (Comparable a1 : a)
- System.out.print(a1 + " ");
- System.out.println();
- }
- /**
- * Checks if array is in sorted order.
- * @author Sedgewick
- * @param a Array to be checked.
- * @return Returns true if array is sorted.
- */
- public static boolean isSorted(Comparable[] a) {
- for (int i = 1; i < a.length; i++)
- if (less(a[i], a[i-1]))
- return false;
- return true;
- }
- //See previous method.
- private static boolean less(Comparable v, Comparable w) {
- return v.compareTo(w) < 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement