More sorting problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 48
Reputation: ayk-retail is an unknown quantity at this point 
Solved Threads: 0
ayk-retail ayk-retail is offline Offline
Light Poster

More sorting problems

 
0
  #1
Aug 1st, 2007
I have this lab from a C++ class that I can't get to work...
Write a function containing a simple sort program that will sort an array of integers by using the stack class (as used in Ex_10). The array (pointer) and its length should be passed to the function as parameters so that the original will be sorted. The array created in main() should contain a maximum of 20 integers which should be entered at runtime (input to be ended by the value 0 or automatically when the 20th value is entered). Step through the array and place the largest number onto the stack (push). Each time you step through the array find the next smallest number and push it onto the stack. Assume no two numbers are duplicated in the array. When the stack is popped, all entries should appear in ascending order.

Here is the code I have so far...
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int STACK_SIZE = 100;
  5. class stack
  6. {
  7. private:
  8. int count; // number of items in the stack
  9. int data[STACK_SIZE];
  10. public:
  11. stack();
  12. ~stack();
  13. void push(const int item); // push an item on the stack
  14. int pop(void); // pop item off the stack
  15. };
  16. stack::stack()
  17. {
  18. count = 0; // zero the stack
  19. }
  20. stack::~stack() {}
  21. void stack::push(const int item)
  22. {
  23. if (count < STACK_SIZE)
  24. {
  25. data[count] = item;
  26. ++count;
  27. }
  28. else cout << "Overflow!\n";
  29. }
  30. int stack::pop(void)
  31. {
  32. if (count >0)
  33. {
  34. --count;
  35. return (data[count]);
  36. }
  37. else
  38. {
  39. cout << "Underflow!\n";
  40. return 0;
  41. }
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. void sort(int *x, int l)
  52. {
  53. int length = l, old = 0, big;
  54. stack s;
  55. for (int i = 0; i <l; i++)
  56. {
  57. big = x[0];
  58. for (int j = 0; j <l; j++)
  59. {
  60.  
  61. if (x[j] > big)
  62. {
  63. big = x[j];
  64. old = j;
  65. }
  66. }
  67. s.push(big);
  68. x[old] = x[l-1];
  69. l--;
  70. }
  71.  
  72. for (int i = 0; i <l; i++)
  73. x[i] = s.pop();
  74. }
  75.  
  76. int main()
  77. {
  78. int a[21];
  79. int count = 0;
  80. do
  81. {
  82. cout << "Enter number "<<count+1<<", 0 to quit: ";
  83. cin >> a[count];
  84. count++;
  85. }
  86. while (a[count-1]!=0 && count <20);
  87.  
  88.  
  89.  
  90. sort(a, count);
  91.  
  92. for (int i = 0; i < count; i++)
  93. cout << a[i] << endl;
  94.  
  95. return 0;
  96. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 48
Reputation: ayk-retail is an unknown quantity at this point 
Solved Threads: 0
ayk-retail ayk-retail is offline Offline
Light Poster

Re: More sorting problems

 
0
  #2
Aug 1st, 2007
Interestingly I didn't mention the problem in the main post. The problem is that it does not sort the code.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 488
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: More sorting problems

 
0
  #3
Aug 1st, 2007
The problem would appear to be that you're not sorting your entire list, only half of it.

look in the loop here
  1. for (int i = 0; i <l; i++)
  2. {
  3. big = x[0];
  4. for (int j = 0; j <l; j++)
  5. {
  6.  
  7. if (x[j] > big)
  8. {
  9. big = x[j];
  10. old = j;
  11. }
  12. }
  13. s.push(big);
  14. x[old] = x[l-1];
  15. l--;
  16. }
At the same time as incrementing i, you're decreasing 'l' - which means the for loop stops when you're halfway through the list (the two values converge in the middle).

there are all sorts of ways of doing sorts, but i suggest you leave your 'l' variable alone - also, your inner loop probably wants to start at j = i rather than j=0 (Similarly, your 'big' number probably wants to start at x[i] )
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 48
Reputation: ayk-retail is an unknown quantity at this point 
Solved Threads: 0
ayk-retail ayk-retail is offline Offline
Light Poster

Re: More sorting problems

 
0
  #4
Aug 1st, 2007
I made some changes but it is still not working...
  1. void sort(int *x, int l)
  2. {
  3. int length = l, old = 0, big;
  4. stack s;
  5. for (int i = 0; i <l; i++)
  6. {
  7. big = x[i];
  8. for (int j = i; j <l; j++)
  9. {
  10.  
  11. if (x[j] > big)
  12. {
  13. big = x[j];
  14. old = j;
  15. }
  16. }
  17. s.push(big);
  18. x[old] = x[l-1];
  19.  
  20. }
  21.  
  22. for (int i = 0; i <l; i++)
  23. x[i] = s.pop();
  24. }
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