Please explain how this array of structures selection sort works

After a few hours of trial and error, I was able to figure out how to write the program, but I still don't understand how I sorted the structures within the array. I created a temporary structure and... yeah, I'm lost. Maybe it's the double for loop and then creating the temporary structure to store something in, I'm not sure what my hang up is.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
using namespace std;

struct RainFallData
{
	string month;
	double rainFall;

	RainFallData (string m = "")
	{
		month = m;
	} 
};

const int NUM_MONTHS = 12;

void inputRainfall (RainFallData[], int);
void sortArray (RainFallData[], int);
void displayArray (RainFallData[], int);

int main()
{
	
RainFallData array[NUM_MONTHS] = { "January", "Februrary", "March", "April", 
		                   "May", "June", "July", "August", "September", 
				   "October", "November", "December" };

inputRainfall(array, NUM_MONTHS);

sortArray(array, NUM_MONTHS);

displayArray(array, NUM_MONTHS);

return 0;

}

void inputRainfall (RainFallData passedarray[], int size)
{
   for (int i = 0; i < size; i++)
   {
      cout << "Input the amount of rainfall in inches for "                 
           <<  passedarray[i].month << ": ";
      cin  >> passedarray[i].rainFall;
   }
}

void sortArray (RainFallData passedarray[], int size)
{
   for (int i = 0; i < size - 1; i++)
      {
         for (int j = i+1; j < size; j++)
	    {
	       if (passedarray[i].rainFall < passedarray[j].rainFall)
	       {
		   RainFallData temp = passedarray[i];
		   passedarray[i] = passedarray[j];
		   passedarray[j] = temp;
                }
	     }
       }
}

void displayArray(RainFallData passedarray[], int size)
{
   cout << "\n\nThe amount of rainfall for each month in descending order\n"
        << "---------------------------------------------------------";

   for (int i = 0; i < size; i++)
   {
      cout << endl << passedarray[i].month << ":  " << passedarray[i].rainFall;
   }
}
Outer loops are simply selection sort loops: http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/selection-sort/
The three lines inside if condition can be replaces with simple and descriptive std::swap(passedarray[i], passedarray[j]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sortArray (RainFallData passedarray[], int size)
{
   for (int i = 0; i < size - 1; i++)
      {
         for (int j = i+1; j < size; j++)
	    {
	       if (passedarray[i].rainFall < passedarray[j].rainFall)
	       {
		   RainFallData temp = passedarray[i]; // This is was causing the swap or sorting to occur.
		   passedarray[i] = passedarray[j];
		   passedarray[j] = temp;
                }
	     }
       }
}



Here is how it goes

int x = 5;
int y = 6;
int temp = 0;

temp = x // temp gets 5
x = y // x gets 6
y = temp // y get 5

x is now = 6
y is now = 5

You have now manually swapped both integers.

This process repeats in iterations until the nested for loop is complete.

Also don't forget that your if statement is the one that is doing the checking and the one that let's the swapping occur only if the condition is true.

Note-This is if you're not using the swap function that's in the algorithm header file.
Last edited on
Thanks for the replies. So... when I create an array of structures, it's basically like [#] of instances of the structure? I guess that's how I think I finally understand it.

MiiNiPaa,
Thank you, I didn't know that existed!
Last edited on
I create an array of structures, it's basically like [#] of instances of the structure?
Yes. That is what arrays and container classes are for: to unite several instances of type in one variable.
Thanks again. You have very helpful.
Topic archived. No new replies allowed.