Quicksort median of three pivot help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

Quicksort median of three pivot help

 
0
  #1
Apr 27th, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 498
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Quicksort median of three pivot help

 
0
  #2
Apr 28th, 2009
Dear,

There are some missing statement in your program.

Please compare following code with your source code.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,629
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Quicksort median of three pivot help

 
0
  #3
Apr 29th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC