sorting arrays from top-down

Hello everyone, I just wanted to thank you all for all the help I have gotten from here. When I know more about programming I will pass it forward.

Problem:
I have a program that is complete and everything works fine, except the sorting of my array. The idNum[] array stores the highest value twice in [0] and [1] but everything else is sorted. I'm not sure what happened. This function is being called twice to sort both arrays, one is a string array with (CC333333)and the other is a int array with (3). Any help would be appreciated. Thank you.

Text file:
CC333333 U 3
AA111111 G 36
BB222222 U 80
DD444444 U 52
CD334455 U 100
BC223344 U 10
AB112233 A 134
EE555555 G 18
DE445566 U 122

Here's my code
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
  //***************************************************************************
void selectSort(string array[], int& cnt)
{
	int curTop,          // Current top of unsorted list
		next,            // Position to compare value to
		max;       	     // Position of greatest value
	string temp;         // Temp value for swapping

	// for each item in the list (top to bottom)
	for (curTop = 0; curTop < cnt - 1; curTop++)
	{
		max = curTop;      // start with current top as greatest value

						   // find greatest value from curTop down
		for (next = curTop + 1; next < cnt; next++)
			if (array[next] > array[max])
			{
				max = next;
				//   	array[max] = array[next];

			}

		//if greatest not at curTop, swap with curTop
		if (max != curTop)
		{
			temp = array[curTop];
			array[curTop] = array[max];
			array[max] = temp;
		}   // end swap
	}
	return;
}
Are you trying to do a bubble sort?
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>

using namespace std;

int main()
{
    string myStrings[] = {"CC333333 U 3",
                          "AA111111 G 36",
                          "BB222222 U 80",
                          "DD444444 U 52",
                          "CD334455 U 100",
                          "BC223344 U 10",
                          "AB112233 A 134",
                          "EE555555 G 18",
                          "DE445566 U 122"};

    // print array
    for (int i=0;i<9;i++)
    {
        cout << myStrings[i] << endl;
    }
    cout << endl;

    int swapit = 0;  // flag for no more swaps
    string temp;   // temporary string
    do
    {
        swapit = 0;  // reset flag
        for (int i=1;i<9;i++)
        {
            if (myStrings[i-1]>myStrings[i])
            {
                temp = myStrings[i-1];
                myStrings[i-1] = myStrings[i];
                myStrings[i]=temp;
                swapit = 1;  // flag a swap took place
            }
        }

    }while (swapit==1);

    cout << "sorted arrays" << endl;
    for (int i=0;i<9;i++)
    {
        cout << myStrings[i] << endl;
    }
    return 0;
}

Last edited on
You can also do without the temp string and the swap code commented out below by using a string swap function.
1
2
3
4
                //temp = myStrings[i-1];
                //myStrings[i-1] = myStrings[i];
                //myStrings[i]=temp;
                myStrings[i-1].swap(myStrings[i]);
Last edited on
I managed to fix the problem, it wasn't in that function but in my other function. I was storing it twice. By the way, our instructor wants us to use this type of sorting. Thank you for your reply.

I do however have another issue. Within that selectSort function I am also calling my second array that has stored in it, the string value of the class level. For example:
Credits Completed Class Level
Under 32 Freshman
32 – 63 Sophomore
64 - 95 Junior
Over 95 Senior

The following text file has:
DD444444 U 52
CD334455 U 100

DD444444 has 52 completed credits so class level is Sophomore
Also the ordinal value of FRESHMAN will be 0, and the ordinal value of SENIOR will be 3. So in my classLevel[] array I have the string value stored in it; ie, Senior, etc.

Problem:
When I use this same function to sort classLevel[], it sorts it in this manner:
Sophomore, Senior, Junior, and Freshman. Not in the order of the text file. For example, if CD334455 was in array[1] than the corresponding 100 or Senior should aslo be in array[1].

Here is my code:
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
  void selectSort(string array[], int& cnt)
{
	int curTop,          // Current top of unsorted list
		next,            // Position to compare value to
		max;       	     // Position of greatest value
	string temp;         // Temp value for swapping

	// for each item in the list (top to bottom)
	for (curTop = 0; curTop < cnt - 1; curTop++)
	{
		max = curTop;      // start with current top as greatest value

		// find greatest value from curTop down
		for (next = curTop + 1; next < cnt; next++)
			if (array[next] > array[max])
			{
				max = next;
				array[max] = array[next];

			}

		//if greatest not at curTop, swap with curTop
		if (max != curTop)
		{
			temp = array[curTop];
			array[curTop] = array[max];
			array[max] = temp;
		}   // end swap
	}
	return;
}

Any help would be appreciated. thank you.
Sorry I don't really follow what the data actually is or what the task actually is.
In truth I find it hard to figure out what someone is trying to do by looking at their code.
I find it easier to translate down from a human language description of the task.
Last edited on
Thank you. I might try and do a new post so people don't get confused. I'll also try and word it a little better. Thank you.
Topic archived. No new replies allowed.