I am doing a non comparison based sort based on counting sort.
I have done a simulation on paper but I am having trouble actually coding it.
I know i need to create a count array and then after counting use another array that sums it up cumulatively and then with that make a new array that uses the cumulative sum to sort it. How can I get the count array to keep track? that part confuses me
Can anyone guide me in the right direction?
int schoolToIndex(string school) {
if (school == "UCB") return 0;
if (school == "UCD") return 1;
if (school == "UCI") return 2;
if (school == "UCLA") return 3;
if (school == "UCM") return 4;
if (school == "UCSD") return 5;
if (school == "UCSF") return 6;
cerr << "Unknown school " << school << endl;
return -1;
}
/*
* Sorts students by school. An implementation of counting sort.
* @param students array already sorted by ID
* @param len Array length
*/
void sortByGroupById2(Student students[], int len) {
int temp[len];
int count[7];
// Creates an array where schools are converted to ints by using schoolToIndex
for(int i = 0; i < len; i++) {
temp[i] =schoolToIndex(students[i].getSchool());
}
for(int i =0; i < 7; i++) {
count[i]=0;
}
for(int j =0; j < len; j++) {
count[temp[j]]++;
cout << j << " " << count[j] << endl;
}
}
test.txt file im using
Joe Paarmann,10000010,UCSF
Otha Baloy,10000053,UCSD
Alton Hamblet,10000110,UCM
Jessie Merle,10000345,UCI
Lawanda Doell,10000455,UCM
Alva Zajicek,10000479,UCI
Chester Kemfort,10000529,UCD
Alice Mines,10000579,UCI
Gary Vongunten,10000875,UCM
Adrian Shbi,10001036,UCSD
countarray better be seven items wide, no less. Start by getting the counts correct. Just get the school index in atmp. If it is >= 0 (and for safety, < 7) just++count[tmp];. When finishedcount[]will have the counts you need. That will at least get you started.std::sort.