i am collecting data from a database and sorting them into an array, but need to sort that array into descending order, below is what the code is
1 2 3 4 5 6 7 8 9 10 11 12 13
int array_numbers[300][300][300];
for(int i = 0 ; i < 300 ; i++)
{
array_numbers[DBRow->col1][DBRow->col2][DBRow->col3] // Collects all db info
}
//we have collected the info, now sort
for(int i = 0 ; i < 300 ; i++)
{
//sort DBRow->col1 by descending order here
}
also, just to let you know that DBRow->col1 is a value of something mostly between 0 - 20000, i only want to arrange DBRow->col1 by descending order, nevermind DBRow->col2 and DBRow->col3, how in c++ can this be done?
#include<iostream>
usingnamespace std;
int main() {
int array[5] = {3, 4, 6, 7, 1}; //original 5 element array that needs sorting
int newarray[5]; //new sorted array that will be constructed based on old array
// \/ Finds the maximum number of the array and sets the first element of the new array = to the max
int firstmax = array[0];
int secondmax = 0;
for (int i = 0; i < 5; i++) {
secondmax = array[i];
if (secondmax > firstmax) {
firstmax = secondmax;}}
newarray[0] = firstmax;
// /\ Finds the maximum number of the array and sets the first element of the new array = to the max
// \/ Sorts remaining elements of the array in descending order
int positron = 1;
int difference = 1;
int counter = 0;
while (true) {
for (int i = 0; i < 5; i++) {
if (array[i] == firstmax - difference) {
newarray[positron] = firstmax - difference;
firstmax -= difference;
difference = 0;
positron++;
counter++;}}
difference++;
if (counter == 4) {break;}}
// /\ Sorts remaining elements of the array in descending order
// \/ Prints the array
for (int i = 0; i < 5; i++) {
cout << newarray[i];}
// /\ Prints the array
while (true) {}
return 0;}
thanks for your example!
however the DBRow->col1 data are random numbers between 0-20000, in the part of your example where it sorts the rest of the elements to descending order wont work as the data is not just a difference of only -1, its random
Oh don't worry it works if the difference of one is not found it loops and looks for a larger one. The only thing that needs to be taken care of is if the amount of elements in the array varies.
opps i missed that! anyway since i will be most likely be searching for over 200 values to be sorted for descending, wouldn't the continuous looping subtracting 1, 2, 3 searching until it finds the value be very slow since it will random letters with most likely have a difference of 300-700 from each other?? is there no faster way doing this?