943,648 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2090
  • Java RSS
Apr 27th, 2009
0

Quicksort median of three pivot help

Expand Post »
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)
  1. public class QuickSort{
  2. public static void main(String a[]){
  3. int i;
  4. int array[] = {12,9,4,99,120,1,3,10,13};
  5.  
  6.  
  7. System.out.println("Values Before the sort:\n");
  8. for(i = 0; i < array.length; i++)
  9. System.out.print( array[i]+" ");
  10. System.out.println();
  11. quick_srt(array,0,array.length-1);
  12. System.out.print("Values after the sort:\n");
  13. for(i = 0; i <array.length; i++)
  14. System.out.print(array[i]+" ");
  15. System.out.println();
  16. System.out.println("PAUSE");
  17. }
  18.  
  19. public static void quick_srt(int array[],int low, int n){
  20. int swap;
  21.  
  22. int lo = low;
  23. int hi = n;
  24. if (lo >= n) {
  25. return;
  26. }
  27. //Pivot median of three
  28. int mid = array[(lo + hi) / 2];
  29. if(array[mid] < array[lo]){
  30. swap = array[lo];
  31. array[lo]=array[mid];
  32. array[mid]=swap;
  33. }
  34. if(array[hi] < array[lo]){
  35. swap = array[lo];
  36. array[lo]= array[hi];
  37. array[hi]= swap;
  38. }
  39. if(array[mid] < array[lo]){
  40. swap = array[mid];
  41. array[mid]= array[hi];
  42. array[hi] = swap;
  43. }
  44. swap = array[mid];
  45. array[mid]= array[hi-1];
  46. array[hi-1]= swap;
  47.  
  48. while (lo < hi) {
  49. while (lo<hi && array[lo] < mid) {
  50. lo++;
  51. }
  52. while (lo<hi && array[hi] > mid) {
  53. hi--;
  54. }
  55. if (lo < hi) {
  56. int T = array[lo];
  57. array[lo] = array[hi];
  58. array[hi] = T;
  59. }
  60. }
  61. if (hi < lo) {
  62. int T = hi;
  63. hi = lo;
  64. lo = T;
  65. }
  66. quick_srt(array, low, lo);
  67. quick_srt(array, lo == low ? lo+1 : lo, n);
  68. }
  69. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Apr 28th, 2009
0

Re: Quicksort median of three pivot help

Dear,

There are some missing statement in your program.

Please compare following code with your source code.

Java Syntax (Toggle Plain Text)
  1. public class QuickSort{
  2. public static void main(String a[]){
  3. int i;
  4. int array[] = {12,9,4,99,120,1,3,10,13};
  5.  
  6.  
  7. System.out.println("Values Before the sort:\n");
  8. for(i = 0; i < array.length; i++)
  9. System.out.print( array[i]+" ");
  10. System.out.println();
  11. quick_srt(array,0,array.length-1);
  12. System.out.print("Values after the sort:\n");
  13. for(i = 0; i <array.length; i++)
  14. System.out.print(array[i]+" ");
  15. System.out.println();
  16. System.out.println("PAUSE");
  17. }
  18.  
  19. public static void quick_srt(int []k,int lb,int ub) {
  20. boolean flag=true;
  21. int i,j,key,temp;
  22.  
  23. if(lb<ub)
  24. {
  25. i=lb;
  26. j=ub+1;
  27. key=k[lb];
  28. while(flag) {
  29. i++;
  30. while(k[i]<key && i<ub)
  31. i++;
  32. j--;
  33. while(k[j]>key && j>lb)
  34. j--;
  35. if(i<j) {
  36. temp=k[i]; k[i]=k[j]; k[j]=temp;
  37. }
  38. else
  39. flag=false;
  40. }
  41. temp=k[lb];
  42. k[lb]=k[j];
  43. k[j]=temp;
  44. quick_srt(k,lb,j-1);
  45. quick_srt(k,j+1,ub);
  46. }
  47. }
  48. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Apr 29th, 2009
0

Re: Quicksort median of three pivot help

Personally I wouldn't listen to anyone who pastes code at you and doesn't explain it at all, but that's just me. I don't understand what you mean by "median of three", care to explain?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Sharing Entity Class Between Computers
Next Thread in Java Forum Timeline: Reading data to a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC