# Help! Sorting Problem

• 06-18-2004
zz3
Help! Sorting Problem
I'm working on a homework assignment, I have to make a program that balances Chemical Equations. I've got most of the work done but my program requires that both sides of the equation be in the same order.
To do this I figured I would sort them into alphabetical order. I wrote up some code to do this(listed below).

I really need some help because its due in a couple of days, can someone tell me what I am doing wrong.

Its an Insertion sort, the code should be fairly explanatory.

Code:

```short CStore::AlphaSort() {         int i = 0; //Loop Varibles         int k = 0;         char key = 0;                // current value working on         for(i = 1; i <= EleNum; i++)         {                 key = CStore::Element[i].Capital;                 k = i-1;         cout << "A" << key << "|" << i << "|" << k << endl;                        //////////////////////                 while( CStore::Element[k].Capital > key)        //go through                 {                         //move a value up one in the list                         CStore::Element[k+1].ChemNum = CStore::Element[k].ChemNum;                         CStore::Element[k+1].Capital = CStore::Element[k].Capital;                         CStore::Element[k+1].LCase  = CStore::Element[k].LCase;                         CStore::Element[k+1].Amount  = CStore::Element[k].Amount;                         cout << "B" << key << "|" << i << "|" << k << endl;                                                 if(k == 0)                        //when loop reaches bottom                                 {                                         cout << "C" << key << "|" << i << "|" << k << endl;                                         CStore::Element[k].ChemNum = CStore::Element[i].ChemNum;                                         CStore::Element[k].Capital = CStore::Element[i].Capital;                                         CStore::Element[k].LCase  = CStore::Element[i].LCase;                                         CStore::Element[k].Amount  = CStore::Element[i].Amount;                                 }                         k--;                                                                                }         //////////////////////         CStore::Element[k+1].ChemNum = CStore::Element[i].ChemNum;         CStore::Element[k+1].Capital = CStore::Element[i].Capital;         CStore::Element[k+1].LCase  = CStore::Element[i].LCase;         CStore::Element[k+1].Amount  = CStore::Element[i].Amount;         cout << "D" << key << "|" << i << "|" << k << endl;         }         ////////////////////////////////////////////////         PrintArray("After Sort:");         /////////////////////////////////////////////////////////         return 1; }```
• 06-19-2004
Salem
> for(i = 1; i <= EleNum; i++)
Well if it's stored in a regular array, then you're off by 1 here
for(i = 0; i < EleNum; i++)

> key = CStore::Element[i].Capital;
If the element is part of your class, there's no need to prefix everything with scope
key = Element[i].Capital;

> CStore::Element[k+1].ChemNum = CStore::Element[k].ChemNum;
You can do in 1 line what you do in 4, by doing
Element[k+1] = Element[k];

Go study the algorithm again - I'm pretty sure you need to store at least one element in a temporary variable while you move elements around in the array.
All I see at the moment is lots of clashes as one element overwrites another in the process of moving it.
• 06-19-2004
zz3
Thanks for the tips, I've cleaned up the code, but i'm still having problems. The cleaned up code is below

Code:

```short CStore::AlphaSort() {         int i = 0; //Loop Varibles         int k = 0;         CElement Temp;         char key = 0;                // current value working on         for(i = 1; i <= EleNum; i++)         {                 Temp = Element[i];                 k = i-1;                //////////////////////           //go through the loop backwards moving data up                 while(CStore::Element[k].Capital > key)                        {                         //move a value up one in the list                         Element[k+1] = Element[k];                                         if(k == 0)                        //when loop reaches bottom                                 {                                         Element[k] = Temp;                                 }                         k--;        //the while loop varible decrements                 }         //////////////////////         Element[k+1] = Temp;         }         ////////////////////////////////////////////////         PrintArray("After Sort:");         /////////////////////////////////////////////////////////         return 1; }```
• 06-19-2004
Salem
Not all of it though
for(i = 1; i <= EleNum; i++)
• 06-19-2004
zz3
Its meant to start at 1
• 06-19-2004
zz3
I fixed it!
One of the varibles was the wrong one. Here is the code if anybody cares.

Code:

```short CStore::AlphaSort() {         int i = 0; //Loop Varibles         int k = 0;         CElement Temp;         for(i = 1; i <= EleNum; i++)         {                 Temp = Element[i];                 k = i-1;                //////////////////////                 while((k >= 0) && Element[k].Capital > Temp.Capital)        //go through the loop backwards moving data up                 {                         //move a value up one in the list                         Element[k+1] = Element[k];                                         if(k == 0)                        //when loop reaches bottom                                 {                                         Element[k] = Temp;                                 }                         k--;                //the while loop varible decrements                 }         //////////////////////         Element[k+1] = Temp;         }         ////////////////////////////////////////////////         PrintArray("After Sort:");         /////////////////////////////////////////////////////////         return 1; }```