Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Quick Sort Optimization

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Quick Sort Optimization

    I have three classes. Each creates an array of 1000 int values.
    class a: uses QuickSort
    class b: uses QuickSort until the size of each partition is <10 then executes InsertSort for sorting the smaller partitions.
    class c: (this is the one I'm having trouble with): same as class b except executes InsertSort on the whole almost sorted array.
    It would seem that class c is only a slight variation of the code in class b (which basically adds to class a). Am I doing this all wrong? Help! Thanks in advanced...

    Class a:

    import java.util.Arrays;
    import java.util.Random;
     
    public class QuickSort {
     
    	static long StartTime = System.nanoTime();
     
    	private static final Random random = new Random();
    	private static final int RANDOM_INT_RANGE = 9999;
     
    	private static int[] randomArray(int size) {
     
    		// Randomize data (array)
    		final int[] arr = new int[size];
    		for (int i = 0; i < arr.length; i++) {
    			arr[i] = random.nextInt(RANDOM_INT_RANGE);
    		}
    		return arr;
    	}
     
    	// Sort
    	private static void sort(int[] arr) {
    		if (arr.length > 0)
    			sortInPlace(arr, 0, arr.length - 1);
    	}
     
    	private static void sortInPlace(int[] arr, int left, int right) {
     
    		if (left >= right)
    			return; // sorted
     
    		final int range = right - left + 1;
    		int pivot = random.nextInt(range) + left;
     
    		int newPivot = partition(arr, left, right, pivot);
     
    		sortInPlace(arr, left, newPivot - 1);
    		sortInPlace(arr, newPivot + 1, right);
     
    	}
     
    	private static int partition(int[] arr, int left, int right, int pivot) {
     
    		int pivotVal = arr[pivot];
    		swapArrayVals(arr, pivot, right);
     
    		int storeIndex = left;
    		for (int i = left; i <= (right - 1); i++) {
    			if (arr[i] < pivotVal) {
    				swapArrayVals(arr, i, storeIndex);
    				storeIndex++;
    			}
    		}
     
    		swapArrayVals(arr, storeIndex, right);
     
    		return storeIndex;
    	}
     
    	static long FinishTime = System.nanoTime();
    	static long TotalTime = FinishTime - StartTime;
     
    	private static void swapArrayVals(int[] arr, int from, int to) {
    		int fromVal = arr[from];
    		int toVal = arr[to];
    		arr[from] = toVal;
    		arr[to] = fromVal;
    	}
     
    	public static void main(String[] args) {
     
    		// Array size
    		int[] arr = randomArray(1000);
    		int[] copy = Arrays.copyOf(arr, arr.length);
     
    		// Print original data (array)
    		System.out.println("The starting/unsorted array: \n"
    				+ Arrays.toString(arr));
     
    		sort(arr);
     
    		// check the result
    		Arrays.sort(copy);
    		if (Arrays.equals(arr, copy)) {
    			System.out.println("The ending/sorted array: \n"
    					+ Arrays.toString(arr));
    			System.out.println("Total elapsed time (milliseconds) " + "is: "
    					+ TotalTime);
     
    		}
     
    	}
    }


    Class b:

    import java.util.Arrays;
    import java.util.Random;
     
    public class OptQSort1 {
     
    	static long StartTime = System.nanoTime();
     
    	private static final Random random = new Random();
    	private static final int RANDOM_INT_RANGE = 9999;
     
    	private static int[] randomArray(int size) {
     
    		// Randomize data (array)
    		final int[] arr = new int[size];
    		for (int i = 0; i < arr.length; i++) {
    			arr[i] = random.nextInt(RANDOM_INT_RANGE);
    		}
    		return arr;
    	}
     
    	// Sort
    	private static void sort(int[] arr) {
    		if (arr.length > 0)
    			sortInPlace(arr, 0, arr.length - 1);
     
    		insertionSort(arr, 0, arr.length - 1);
    	}
     
    	private static void sortInPlace(int[] arr, int left, int right) {
     
    		// OptQSort1:
    		int size = right - left + 1;
    		if (size < 10)
    			insertionSort(arr, left, right);
     
    		if (left >= right)
    			return; // sorted
     
    		final int range = right - left + 1;
    		int pivot = random.nextInt(range) + left;
     
    		int newPivot = partition(arr, left, right, pivot);
     
    		sortInPlace(arr, left, newPivot - 1);
    		sortInPlace(arr, newPivot + 1, right);
     
    	}
     
    	private static int partition(int[] arr, int left, int right, int pivot) {
     
    		int pivotVal = arr[pivot];
    		swapArrayVals(arr, pivot, right);
     
    		int storeIndex = left;
    		for (int i = left; i <= (right - 1); i++) {
    			if (arr[i] < pivotVal) {
    				swapArrayVals(arr, i, storeIndex);
    				storeIndex++;
    			}
    		}
     
    		swapArrayVals(arr, storeIndex, right);
     
    		return storeIndex;
    	}
     
    	private static void swapArrayVals(int[] arr, int from, int to) {
    		int fromVal = arr[from];
    		int toVal = arr[to];
    		arr[from] = toVal;
    		arr[to] = fromVal;
    	}
     
    	public static void insertionSort(int[] arr, int left, int right) {
    		int in, out;
     
    		for (out = left + 1; out <= right; out++) {
    			int temp = arr[out];
    			in = out;
     
    			while (in > left && arr[in - 1] >= temp) {
    				arr[in] = arr[in - 1];
    				--in;
    			}
    			arr[in] = temp;
    		}
     
    	}
     
    	static long FinishTime = System.nanoTime();
    	static long TotalTime = FinishTime - StartTime;
     
    	public static void main(String[] args) {
     
    		// Array size
    		int[] arr = randomArray(1000);
    		int[] copy = Arrays.copyOf(arr, arr.length);
     
    		// Print original data (array)
    		System.out.println("The starting/unsorted array: \n"
    				+ Arrays.toString(arr));
     
    		sort(arr);
     
    		// check the result
    		Arrays.sort(copy);
    		if (Arrays.equals(arr, copy)) {
    			System.out.println("The ending/sorted array: \n"
    					+ Arrays.toString(arr));
    			System.out.println("Total elapsed time (milliseconds) " + "is: "
    					+ TotalTime);
     
    		}
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Quick Sort Optimization

    Please explain your problem? Does the code compile?
    Do you get errors when executing?
    Is the results incorrect?
    Show the output and explain what is wrong with it and what it should be.

Similar Threads

  1. Replies: 3
    Last Post: September 30th, 2011, 08:45 AM
  2. Quick Java Overview?
    By ShadowKing98 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 1st, 2011, 08:14 AM
  3. Swing app gui optimization
    By nik_meback in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 12:55 PM
  4. [SOLVED] Quick JTable Column Sort Question
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2010, 04:35 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM