Quicksort problems

Reply

Join Date: Sep 2006
Posts: 12
Reputation: Lutzee is an unknown quantity at this point 
Solved Threads: 0
Lutzee Lutzee is offline Offline
Newbie Poster

Quicksort problems

 
0
  #1
Jun 10th, 2007
Hi there everyone. I wonder if you could help me with my code. It just seems to go into an infinite loop. I would greatly appreciate some help with this. Thanks

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int qsrt(int array[], int first, int last)
  5.  
  6. {
  7.  
  8.  
  9. int over = 0;
  10. over = first-last;
  11. int g=0;
  12.  
  13. if(over==0)
  14. {
  15. return 0;
  16. }
  17.  
  18. int c=0;
  19.  
  20. int start = first;
  21. int end = last;
  22. int pivot = array[first];
  23. int temp =0;
  24.  
  25. while(first != last)
  26. {
  27.  
  28. while((array[last]>pivot)&&(first != last))
  29. {
  30. last--;
  31. }
  32.  
  33. while((array[first]<pivot)&&(first != last))
  34. {
  35. first++;
  36. }
  37.  
  38. temp = array[last];
  39. array[last] = array[first];
  40. array[first] = temp;
  41.  
  42.  
  43.  
  44. c = last-first;
  45.  
  46. if(c>1)
  47. {
  48. first++;
  49. last--;
  50. }
  51.  
  52. }
  53.  
  54. c= 0;
  55. c= first-1;
  56.  
  57. qsrt(array,first,end);
  58. qsrt(array,start,(c));
  59.  
  60. return 0;
  61. }
  62.  
  63. int main (void)
  64.  
  65. {
  66. int anarray[9] = {4,5,3,8,5,6,2,8,9};
  67.  
  68.  
  69.  
  70. qsrt(anarray,0,8);
  71. system("pause");
  72.  
  73.  
  74. return 0;
  75. }

Thanks once again
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 222
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Quicksort problems

 
0
  #2
Jun 10th, 2007
IF your intent is to sort a range of integers beginning at first and continuing to last, there are simpler ways to do it.
You don't need two loops or so many extra varaibles.

If you need to sort from both ends, shouldn't pivot be in the middle of the arrary rather than equal to array[first]?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,424
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Quicksort problems

 
0
  #3
Jun 10th, 2007
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int qsrt(int array[], int first, int last){
  5. int over = 0;
  6. over = first-last;
  7. int g=0;
  8. if(over==0){
  9. return 0;
  10. }int c=0;
  11. int start = first;
  12. int end = last;
  13. int pivot = array[first];
  14. int temp =0;
  15. while(first != last){
  16. while((array[last]>pivot)&&(first != last)){
  17. last--;
  18. }while((array[first]<pivot)&&(first != last)){
  19. first++;
  20. }temp = array[last];
  21. array[last] = array[first];
  22. array[first] = temp;
  23. c = last-first;
  24. if(c>1){
  25. first++;
  26. last--;
  27. }
  28. }c= 0;
  29. c= first-1;
  30. qsrt(array,first,end);
  31. qsrt(array,start,(c));
  32. return 0;
  33. }
  34.  
  35. int main (void){
  36. int anarray[9] = {4,5,3,8,5,6,2,8,9};
  37. qsrt(anarray,0,8);
  38. system("pause");
  39. return 0;
  40. }

first, you should learn how to format your code correctly...


second, saying this
  1. int a=0;
  2. a=b-c;
is the same thing as saying
  1. int a=b-c;
when you have b and c declared...

third, what are you doing there? it seems like you are ordering numbers from least to greatest... but... JRM is right... you could do this with less variables...
Last edited by happygeek; Jun 11th, 2007 at 5:40 am. Reason: swears removed, keep it clean
-->sometimes i wanna take my toaster in a bath<--
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC