Bubble Sort

I have my bubble sort working. I like to do an ascending followed by a descending sort. Any help would be well.. helpful
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>

using namespace std;

int i; 
int tmp;
int array[50];
int n = 50;
 
   void Bubblesort(int array[], int n)
      {
       int tmp, j, q;   
       for (q=0; q < n-1; q++)                                                      
      {
       for (j=0; j < n-1; j++)
      {
       if (array[j] < array[j+1])// changes acending to decending
      {
       tmp = array[j];
       array[j] = array[j+1];
       array[j+1] = tmp;
       }
       }
       }
       }                                                                            
int main()
{                                                                               
 for (int j = 0; j < n; j++)
{                                                                            
 i = rand() % 101;                                                         
 array[j] = i;
}                                                                            
 for (int k = 0; k < n; k++)
{                                                                            
 cout << array[k] << " ";                                                 
}                                                                            
 cout << endl << endl;
 Bubblesort(array, n);
 for (int i=0; i < n; i++)
{
 cout << array[i] << " " ;
}
 cout << endl;
 system("pause");
 return 0;
}
If you have already sorted the array then all you need to do is reverse it.
Yeah, but what I am trying to do is output ascending followed by descending.
Yes you sort ascending, then you output it, then you reverse it (found this [untested] http://www.cplusplus.com/reference/algorithm/reverse/) and then you output it again.
Topic archived. No new replies allowed.