Sorting

Hi all, I'm having difficult to use the sorting method to get the subscripts from descending order. for example: {2,6,4,9,10}, since 10 is largest, ill store the subscript 4 to an array at position 0, and then the next largest, which is 3 to position 1, and so on. My error is number are totally off, and it altered my original array which is not intended.

/////////////My algorithm is totally wrong, Any help will be great. If you do not understand what I said above, please ask. Thanks in Advance.
void repQty()
{//start repQty function

//troubles with sorting
int startScan;
int maxIndex;

int index;
int numBook[20];
int size=20;
for (int i=0; i<row; i++)//row=20
{
numBook[i]=qtyOnHand[i];// pass number stored inqtyOnHand to numBook
}

for (startScan=0; startScan<(size-1); startScan++)
{
maxIndex=startScan;
for (index=startScan+1; index<size; index++)
{
if (numBook[index]>numBook[maxIndex])
{
maxIndex=index;

}
}
numBook[startScan]=maxIndex;
}
for (int i=0; i<row; i++)
{
index=numBook[i];//use subscript stored in numBook as subscripts
if (bookTitle[index][0]!='\0')
{
cout<<"Title is: "<<bookTitle[index]<<" isbn is: "<<isbn[index]<<" qty is: "<<qtyOnHand[index]<<endl;//These are just variable that has value stored.
}
}



}//end repQty function
http://www.algolist.net/Algorithms/Sorting/Bubble_sort
look here ... it might help u ...
also if you can afford it or if you can get it any other way try to read this book
Introduction to Algorithms
written by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein

it covers way more than sorting ... but you don't have to read it all at once ... just read from time to time when you think when you will have time ...
Last edited on
if you are new to sorting do bubble sort because it is simple and a decent introduction. if you want one of the better sorting algorithms use the sort template in the algorithms library.
Hi, TwoOfDiamons, Shinigami, and ui uiho, thanks for replying.I have a small question. "I want to get the subscripts from the sorting algorithms, not to actually sort them."
Example:
int array[20]={5,20,4,10};//original values
//using a sort method that print out the array
int subscriptArray[20];//store subscripts from array's largest value to smallest; NOT the actual values
so at subscriptArray[0]=1; subscriptArray[1]=3; subscriptArray[2]=0; subscriptArray[3]=2;
copy the array into another array and then sort them. then set value based on the number it occured in the 1st array.
copy(array, subscript)
sort(subscript.begin(), subscript.end())
for(int i = 0; i < array.length(); i++){
for(int j = 0; j < array.length(); j++){
if(array[j] == subscript[j]){
subscript[j] = i;
break;
}
}
}
Thanks, it worked! However I got a small problem, will post in another post:
Topic archived. No new replies allowed.