SelectionSort craziness.

I'm currently doing a project but my selection sort isn't sorting properly.

The jist is that i must sort based on these sub structs of time.

If the received time is the same, then i must sort by expiration time.

Any ideas what I did wrong?

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
  void compareshipments (Shipment shipments[], int totalShipments)
{
    int i,j;    //Initialized in loop
	int iMin;   //Initialized in loop


	for (j = 0; j < totalShipments - 1; j++)
	{
	    iMin = j;


	    for ( i = j + 1; i < totalShipments; i++)
	    {
	        if (shipments[i].shipmentrecieved.year <shipments[iMin].shipmentrecieved.year)
	        {
	            iMin = i;
	        }
	        else if (shipments[i].shipmentrecieved.year==shipments[iMin].shipmentrecieved.year)
	        {
                swap( shipments[j], shipments[iMin]);
	            if (shipments[i].shipmentrecieved.month <shipments[iMin].shipmentrecieved.month)
                {
	            iMin = i;
                }
                else if (shipments[i].shipmentrecieved.month==shipments[iMin].shipmentrecieved.month)
                {
                    swap( shipments[j], shipments[iMin]);
                    if (shipments[i].shipmentrecieved.day <shipments[iMin].shipmentrecieved.day)
                    {
                    iMin = i;
                    }
                    else if (shipments[i].shipmentrecieved.day==shipments[iMin].shipmentrecieved.day)
                    {
                        swap( shipments[j], shipments[iMin]);
                        if (shipments[i].expdate.year <shipments[iMin].expdate.year)
                        {
                        iMin = i;
                        }
                        else if (shipments[i].expdate.year==shipments[iMin].expdate.year)
                        {
                            swap( shipments[j], shipments[iMin]);
                            if (shipments[i].expdate.month <shipments[iMin].expdate.month)
                            {
                            iMin = i;
                            }
                            else if (shipments[i].expdate.month==shipments[iMin].expdate.month)
                            {
                                swap( shipments[j], shipments[iMin]);
                                if (shipments[i].expdate.day <shipments[iMin].expdate.day)
                                {
                                iMin = i;
                                }

                    }
                }

            }


	}
}
}
}
}
There's a little too much swap()ing going on in there.

I recommend you rewrite your comparison as a function.

(And also, since your function is mis-named, fix that.)

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
bool compareTwoShipments( const Shipment& a, const Shipment& b )
{
  // return true only if a compares less than b by (year, month, and day)
  return (a.year < b.year) or ((a.year == b.year) 
    and ((a.month < b.month) or ((a.month == b.month)
      and (a.day < b.day))));
}

void sortShipments( Shipment shipments[], int totalShipments )
{
  int i, j;  // loop counters
  int iMin;  // index of element to swap with element j

  for (j = 0; j < totalShipments - 1; j++)
  {
    // Find the next shipment in the remaining unsorted shipments
    iMin = j;
    for (i = j + 1; i < totalShipments; i++)
    {
      if (compareTwoShipments( shipments[i], shipments[iMin] ))
      {
        iMin = i;
      }
    }
    swap( shipments[j], shipments[iMin];
  }
}

It is late and my brain may have failed here. But that should be right...

Hope this helps.
Topic archived. No new replies allowed.