Selection Sort Algorithm Help

closed account (365X92yv)
I'm trying to sort an array of objects and I keep running into a problem with this function. I'll post it so you can take a look. It passes an array of objects which has three variables, string itemCode, and int stockSold, int stockRemain. What I need to do is sort all the objects by the stockRemain variable. When the function runs it discards all of my stockSold and stockRemain data and just leaves the itemCode data.

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
void selectionSort(Item list[], int length )
{
	int smallestPos;
	Item temp;
	// for every position in the array
	for( int i=0; i<length; i++ )
	{
	// find the smallest element starting at that position
		smallestPos = i;
		for( int j=i+1; j<length; j++ )
		{
			if ( list[j].stockRemain < list[smallestPos].stockRemain )
			{
				// found a smaller one, remember it and keep going
				smallestPos = j;
			}
		}
		// see if we found something smaller than list[i]
			if( list[i].stockRemain > list[smallestPos].stockRemain )
			{
				// we did find a smaller one, so swap with list[i]
				temp.stockRemain = list[i].stockRemain;
				list[i].stockRemain = list[smallestPos].stockRemain;
				list[smallestPos].stockRemain = temp.stockRemain;
			}
	}
	// done sorting the array
}


Take a look and see if you either need more of my code or if you can figure out what I need. Thanks.
Last edited on
1
2
3
				temp.stockRemain = list[i].stockRemain;
				list[i].stockRemain = list[smallestPos].stockRemain;
				list[smallestPos].stockRemain = temp.stockRemain;
You are just moving the stocRemain property. You need to swap the whole structure swap( list[i], list[smallestPos] );

When the function runs it discards all of my stockSold and stockRemain data and just leaves the itemCode data.
¿what do you mean? You aren't touching stockSold, ¿are you sure that is 'selectionSort' fault?
closed account (365X92yv)
What I mean is I have a function that goes through the object array and prints them out. When it prints there's nothing in the class members. I'm just gonna include all my code. I can't try to explain this without all my code.

main.cpp
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
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <cmath>
#include <string>
using namespace std;
#include "funcs.h"

int main()
{
	string filename;
	cout << "Welcome to the automated inventory sorting program. " << endl 
		<< "Make sure you have your list ready." << endl;
	cout << "Inventory List Input(Filename): ";
	cin >> filename;

	Item inventory[100];
	int i = 0;
	
	fileReader(filename, i, inventory);
	selectionSort(inventory, i);
	itemPrint(inventory, i);

	system("pause");
	return 0;
}


funcs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <cmath>
#include <string>

using namespace std;

class Item
	{
	public:
		string itemCode;
		int stockSold, stockRemain;
	};

int fileReader(string inventList, int i, Item inventory[]);
void itemPrint(Item items[],int count);
void selectionSort(Item list[], int length );


funcs.cpp
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
76
77
78
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <cmath>
#include <string>

using namespace std;

#include "funcs.h"

int fileReader(string inventList, int i, Item inventory[])
{
	ifstream inFile;
	inFile.open(inventList);
	int count;
    // Loop through the input file.
    while( !inFile.eof() )
	{
		inFile >> inventory[i].itemCode;
		inFile >> inventory[i].stockSold;
		inFile >> inventory[i].stockRemain;

		cout << i << ": " << inventory[i].itemCode << endl;
		cout << i << ": " << inventory[i].stockSold << endl;
		cout << i << ": " << inventory[i].stockRemain << endl;

		i++;
	}
	count = i;
	// Close the input file    
    inFile.close();

	return count;
}

void itemPrint(Item items[],int count)
{
	cout << "Sorted Records: " << endl;
	for(int i = 0; i<count;i++)
	{
		//cout << items[i].itemCode;
		//cout << items[i].stockSold;
		cout << items[i].stockRemain;
	}
}

void selectionSort(Item list[], int length )
{
	int smallestPos;
	Item temp;
	// for every position in the array
	for( int i=0; i<length; i++ )
	{
	// find the smallest element starting at that position
		smallestPos = i;
		for( int j=i+1; j<length; j++ )
		{
			if ( list[j].stockRemain < list[smallestPos].stockRemain )
			{
				// found a smaller one, remember it and keep going
				smallestPos = j;
			}
		}
		// see if we found something smaller than list[i]
		if( list[i].stockRemain > list[smallestPos].stockRemain )
		{
			// we did find a smaller one, so swap with list[i]
			temp.stockRemain = list[i].stockRemain;
			list[i].stockRemain = list[smallestPos].stockRemain;
			list[smallestPos].stockRemain = temp.stockRemain;
		}
	}
	// done sorting the array
	for(int i = 0; i<length;i++)
	{
		cout << "Spot " << i << ": " << list[i].stockRemain << endl;
	}
}


I realize I still have to swap the other members object data around. I just wanted to work on swapping one variable so that I know it can sort. Thats the sole mission I have right now.
Last edited on
closed account (365X92yv)
Here's the data in the txt file I was calling and trying to sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
619847GBE 641 998
418712IMB 107 867
227451GEM 789 181
981836KEA 747 171
986516IGU 303 71
501024BMU 895 743
455559SKK 764 234
706756EUU 687 730
500635MIS 841 670
341955IUS 32 907
791357GEK 391 284
933292AMK 581 901
929002IKA 20 177
474556IBK 532 537
342188MKI 649 367
359989USA 934 972
529875KIB 653 655
706784GGS 596 345
764956SEB 839 304
105170KKU 613 130


The last column is the one that needs to be sorted. But the thing is that each line should be moved together.
closed account (365X92yv)
NEVERMIND I GOT IT!!!!! I LOVE THIS STUFF! Thanks. I had a brain fart.
Topic archived. No new replies allowed.