944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2322
  • C++ RSS
Jun 10th, 2007
0

Quicksort problems

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 19
Solved Threads: 0
Newbie Poster
Lutzee is offline Offline
12 posts
since Sep 2006
Jun 10th, 2007
0

Re: Quicksort problems

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]?
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Jun 10th, 2007
0

Re: Quicksort problems

  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
C++ Syntax (Toggle Plain Text)
  1. int a=0;
  2. a=b-c;
is the same thing as saying
C++ Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007

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 C++ Forum Timeline: Return Array from C++
Next Thread in C++ Forum Timeline: Using a switch statement with nested if/else statements





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


Follow us on Twitter


© 2011 DaniWeb® LLC