### Sorting

I'd love a little help. I can sort in ascending order or descending order but not both in my program. Would someone be able to give me a hand?
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include 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; }``````
So you want to make it swap between descending and ascending?

When I ran it I got this:

 ```41 85 72 38 80 69 65 68 96 22 49 67 51 61 63 87 66 24 80 83 71 60 64 52 90 60 49 31 23 99 94 11 25 24 51 15 13 39 67 97 19 76 12 33 99 18 92 35 74 0 99 99 97 96 94 92 90 87 85 83 80 80 76 74 72 71 69 68 67 67 66 65 64 63 61 60 60 52 51 51 49 49 41 39 38 35 33 31 25 24 24 23 22 19 18 15 13 12 11 0 Press any key to continue . . .```

One was descending and one was random.
Last edited on
Yeah... I'd like to print random followed by descending then ascending. I don't get it. And frustrated.
I think I have it but I changed the random to ascending. I'll post the code in a minute.
Okay, here hope this helps.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include #include #include using namespace std; int i; int tmp; int array[50]; int n = 50; void sort(int array[], int n){//added this <------------- int tmp, j, q; for (q=0; q < n-1; q++){ for (j=0; j < n-1; j++){ if (array[j] > array[j+1]){//changed this <------------- tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; } } } } 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() { srand(time(0));//added this <------------- while(1){//added this <------------- system("cls");//added this <------------- for (int j = 0; j < n; j++) { i = rand()% 101; array[j] = i; sort(array, n);//added this <------------- } 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; Sleep(1500);//added this <------------- }//added this <------------- return 0; }``````

You could use this to put things in alphabetical order by using `case` and each letter equals a number.
Last edited on
Whoa. Thanks but it keeps repeating or running.?
yea, I did that so you could see that is changes and that is always working.
You could use `if(GetAsyncKeyState(VK_ESCAPE))` to exit when you want to. Just add this in the `while` loop and press and hold the escape key and make it so when you do so it exits.

BTW is this for collage? When I'm out of middle school and high school I'm going to collage to learn C++, C#, and Java.
Last edited on
Btw to make you program shorter and more efficient just use `bool` or `int` and `if` to decide weather or not it is ascending or descending.
Yeah, I took a course just for fun. Dude if you are for real about your age, you are well on your way.

So if I may... I really want to print the results. 50 rand, sorted descending then ascending.
This makes it work the way you originally wanted. (one random, one descending or ascending changing every 4.5 seconds.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include #include #include using namespace std; int i; int tmp; int array[50]; int n = 50; int which = 0; 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 (which == 0){ if (array[j] < array[j+1]) { tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; } } else { if (array[j] > array[j+1]) { tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; } } } } } int main() { srand(time(0)); while(1){ system("cls"); 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; if (which == 0){which = 1;} else {which = 0;} Bubblesort(array, n); for (int i=0; i < n; i++) { cout << array[i] << " " ; } cout << endl; Sleep(4500); } return 0; }``````
Ahh miscommunication... run this code and you may get a better understanding. I think I need to resort the array. Sorted in descending and ascending
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````/*Write a program that generates 50 random numbers between 1 and 100. Sort the numbers into ascending order. Then, sort the numbers into descending order. Your output should consist of first, the list of 50 random numbers, next, the sorted list in ascending order, and finally, the sorted list in descending order. Use a bubble sort or insertion sort.*/ #include #include 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; }``````
short version for this:

 ``123456789101112131415161718192021222324`` ``````#include #include int main() { const int n = 50; int array[n]; srand((int)time(NULL)); //generate random numbers for (int j = 0; j < n; j++) { array[j] = rand() % 101; } //sort std::sort(array, &array[50]); //reverse order for(int i=0; i<=24; ++i) { std::swap(array[i], array[49-i]); } }``````
Last edited on
Okay, here is darkmaster's version, but with cout.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include #include using namespace std; int pause(){char a; cout << "*PRESS ANY KEY TO CONTINUE*";a=getch(); return 0;} int main() { const int n = 50; int array[n]; srand((int)time(NULL)); //generate random numbers cout << "Random Numbers:\n"; for (int j = 0; j < n; j++) { array[j] = rand() % 101; cout << array[j] << " "; } cout << endl << endl; //sort std::sort(array, &array[50]); for (int j = 0; j < n; j++) { cout << array[j] << " "; } cout << endl << endl; //reverse order for(int i=0; i<=24; ++i) { std::swap(array[i], array[49-i]); } for (int j = 0; j < n; j++) { cout << array[j] << " "; } cout << endl << endl; pause(); }``````
Topic archived. No new replies allowed.