| | |
Quicksort median of three pivot help
![]() |
Hello, BTW the program compiles. Im trying to change a quick sort program so that it picks a median of three for the pivot instead of the first low number. My code is not running right, can somebody help me out. thanks
java Syntax (Toggle Plain Text)
public class QuickSort{ public static void main(String a[]){ int i; int array[] = {12,9,4,99,120,1,3,10,13}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); quick_srt(array,0,array.length-1); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); } public static void quick_srt(int array[],int low, int n){ int swap; int lo = low; int hi = n; if (lo >= n) { return; } //Pivot median of three int mid = array[(lo + hi) / 2]; if(array[mid] < array[lo]){ swap = array[lo]; array[lo]=array[mid]; array[mid]=swap; } if(array[hi] < array[lo]){ swap = array[lo]; array[lo]= array[hi]; array[hi]= swap; } if(array[mid] < array[lo]){ swap = array[mid]; array[mid]= array[hi]; array[hi] = swap; } swap = array[mid]; array[mid]= array[hi-1]; array[hi-1]= swap; while (lo < hi) { while (lo<hi && array[lo] < mid) { lo++; } while (lo<hi && array[hi] > mid) { hi--; } if (lo < hi) { int T = array[lo]; array[lo] = array[hi]; array[hi] = T; } } if (hi < lo) { int T = hi; hi = lo; lo = T; } quick_srt(array, low, lo); quick_srt(array, lo == low ? lo+1 : lo, n); } }
Dear,
There are some missing statement in your program.
Please compare following code with your source code.
There are some missing statement in your program.
Please compare following code with your source code.
Java Syntax (Toggle Plain Text)
public class QuickSort{ public static void main(String a[]){ int i; int array[] = {12,9,4,99,120,1,3,10,13}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); quick_srt(array,0,array.length-1); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); } public static void quick_srt(int []k,int lb,int ub) { boolean flag=true; int i,j,key,temp; if(lb<ub) { i=lb; j=ub+1; key=k[lb]; while(flag) { i++; while(k[i]<key && i<ub) i++; j--; while(k[j]>key && j>lb) j--; if(i<j) { temp=k[i]; k[i]=k[j]; k[j]=temp; } else flag=false; } temp=k[lb]; k[lb]=k[j]; k[j]=temp; quick_srt(k,lb,j-1); quick_srt(k,j+1,ub); } } }
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- Improvements to Quicksort using the Median of 3 partition and Insertion sort (C++)
- Quicksort strings? (C++)
- Non-recursive quicksort help (C++)
- Quicksorting linked list - simple algorithm (C)
- Can you guys help me? about Quick Sort Algorithm (C)
Other Threads in the Java Forum
- Previous Thread: Sharing Entity Class Between Computers
- Next Thread: Reading data to a file
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






