Results 1 to 2 of 2
Thread: Trouble with quick sort code
- 12-08-2012, 03:32 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Trouble with quick sort code
Hey I seem to be having a problem trying to implement some Java quick sort code over an array of 10,000 random numbers. I have a text file containing the numbers which are placed into an array, which is then passed to the sorting algorithm to be sorted. My aim is to time how long it takes to time the sorting increasing the numbers sorted each time using the timing loop I have. Not sure if each time the sorting code is being ran on the sorted array. I just keep getting this graph for best case (best case meaning the textfile is already sorted) : http://i.imgur.com/jIXxj.jpg rather than a O(n log n) graph.
Java Code:import java.io.*; import java.util.*; public class Quicksort { public static void main(String args[]) throws IOException { //Import the random integer text file into an integer array File fil = new File("randomASC.txt"); FileReader inputFil = new FileReader(fil); int [] myarray = new int [10000]; Scanner in = new Scanner(inputFil); for(int q = 0; q < myarray.length; q++) { myarray[q] = in.nextInt(); } in.close(); for (int n = 100; n < 10000; n += 100) { long total = 0; for (int r = 0; r < 10; ++r) { long start = System.nanoTime (); quickSort(myarray,0,n-1); total += System.nanoTime() - start; } System.out.println (n + "," + (double)total / 10.0); } } public static void quickSort(int[] a, int p, int r) { if(p<r) { int q=partition(a,p,r); quickSort(a,p,q); quickSort(a,q+1,r); } } private static int partition(int[] a, int p, int r) { int x = a[p]; int i = p-1 ; int j = r+1 ; while (true) { i++; while ( i< r && a[i] < x) i++; j--; while (j>p && a[j] > x) j--; if (i < j) swap(a, i, j); else return j; } } private static void swap(int[] a, int i, int j) { // TODO Auto-generated method stub int temp = a[i]; a[i] = a[j]; a[j] = temp; } }
- 12-08-2012, 05:55 PM #2
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: Trouble with quick sort code
latest news I'm getting this now! http://i.imgur.com/rMr0Y.jpg but that's now for average/best AND worst.... ¬_¬
Similar Threads
-
Quick sort problem
By fishy8158 in forum New To JavaReplies: 0Last Post: 02-18-2012, 04:21 AM -
Need help with quick sort method
By Get_tanked in forum New To JavaReplies: 1Last Post: 03-14-2011, 09:44 PM -
Quick Sort explanation.
By hawaiifiver in forum New To JavaReplies: 4Last Post: 03-10-2009, 02:28 AM -
Quick sort with median-of-three partitioning
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:40 PM -
Simple version of quick sort
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks