How do you display a selection sort?

I've been trying to display a selection sort array, but it never shows up. I'm trying to set an another array in ascending order.

Here's what I' have:
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
 void selectionSort (int [], int);

...       
     
 outFile<<"Employees in order entered:"<<endl;
    outFile<<" "<<endl;
    outFile<< "Employee Number"<<setw(20)<<"Hours Worked"<<setw(25)<<"PayRate per Hour"<<setw(22)<<"Wages"<<endl;
    
    
    for (int i=0; i<count; i++)
    {
        outFile <<setw(3)<< empId[i] << setw(20) << hours[i]<<setw(30)<<payRate[i]<<setw(25)<<wages[i] << endl;
        selectionSort (empId, SIZE);
    }
    
    
   
    
    outFile<<" "<<endl;
    outFile<<"Employee numbers in ascending order"<<endl;
    outFile<<" "<<endl;
    outFile<< "Employee Number"<<setw(20)<<"Hours Worked"<<setw(25)<<"PayRate per Hour"<<setw(22)<<"Wages"<<endl;    

...

//Function definition

void selectionSort(int array[], int size)
{
    int startScan, maxIndex, maxValue;
    for (startScan=0; startScan< (size-1); startScan++)
    {
        maxIndex= startScan;
        maxValue= array[startScan];
        for (int index = startScan +1; index<size; index++)
        {
            if (array[index] > maxValue)
            {
                maxValue= array[index];
                maxIndex= index;
            }
        }
        
        array[maxIndex]= array[startScan];
        array[startScan]= maxValue;
    }
        
    
}


This is what I keep getting as the outcome:
Employees in order entered:

Employee Number Hours Worked PayRate per Hour Wages
5658845 40 8 320
8777541 25.25 9.25 233.562
8451277 30 10.5 315
7895122 10 12 120
5658845 50 10.2 510
4520125 35.2 7.5 264

Employee numbers in ascending order

Employee Number Hours Worked PayRate per Hour Wages


But I really want to add another array is ascending order for the selection sort too :/


Last edited on
Do you have another for loop after line 22 like you do at line 10 to print out the contents of the array?

Line 13 - Do you mean to call the sort function after the for loop? after you print out the initial array contents?

It looks like you have parallel arrays for the different values (employee number, hours, payrate, wages), where the index number is the same across the arrays for one employee. If you just sort the employee number arrays, an employee number that was at index 0 of the original array could be at index 4 of the sorted array. Just wondering about matching up the data in the other arrays with the new order in the first.

Did you want ascending or descending order on the sort? I tested the selection sort and it seems to put the numbers from highest to lowest.

I honestly didn't know what I was doing. I just wanted the original set of arrays and then make a selection sort in ascending order of those arrays.

I meant ascending. I just got a bit mixed up when writing the definition, but I fixed the definition to ascending.
Last edited on
I changed the index to 4 and got rid of line 22, but it doesn't show up :/
The employee number switching from index 0 to index 4 was just an example of something that could happen if you have parallel arrays and just sort one of them.

I asked if you had another for loop to print out array values like you do at line 10 after line 22. Line 22 prints out the headers, I don't think you want to get rid of that.

e.g.

print headers (lines 5-7)
for loop to print unsorted array (lines 10-14, with call to selectionSort pulled outside)
sort array (line 13 now, but pull it out of the for loop)
print headers (line 19-22)
for loop to print sorted array (lines ???)
I fixed it to:
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
outFile<<"Employees in order entered:"<<endl;
    outFile<<" "<<endl;
    outFile<< "Employee Number"<<setw(20)<<"Hours Worked"<<setw(25)<<"PayRate per Hour"<<setw(22)<<"Wages"<<endl;
    
 
    for (int i=0; i<count; i++)
    {
        outFile <<setw(3)<< empId[i] << setw(20) << hours[i]<<setw(30)<<payRate[i]<<setw(25)<<wages[i] << endl;
        
    }


Also I DO want parallel arrays!
    
   
    
    outFile<<" "<<endl;
    outFile<<"Employee numbers in ascending order"<<endl;
    outFile<<" "<<endl;
     for (int i=4; i<count; i++)
     {
         outFile <<setw(3)<< empId[i] << setw(20) << hours[i]<<setw(30)<<payRate[i]<<setw(25)<<wages[i] << endl;
     }

    selectionSort (empId, SIZE);


and this is my outfile:

Employees in order entered:

Employee Number Hours Worked PayRate per Hour Wages
5658845 40 8 320
4520125 25.25 9.25 233.562
7895122 30 10.5 315
8777541 10 12 120
8451277 50 10.2 510
1302850 35.2 7.5 264

Employee numbers in ascending order

8451277 50 10.2 510
1302850 35.2 7.5 264


It seems I'm I'm kinda getting there, but it doesn't show all of them. Also it appears as if they're still in descending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

void selectionSort(int array[], int size)
{
    int startScan, minIndex, minValue;
    for (startScan= 0; startScan< (size-1); startScan++)
    {
        minIndex= startScan;
        minValue= array[startScan];
        for (int index = startScan +1; index<size; index++)
        {
            if (array[index] < minValue)
            {
                minValue= array[index];
                minIndex= index;
            }
        }
        
        array[minIndex]= array[startScan];
        array[startScan]= minValue;
    }
        
    
}



Also I DO want parallel arrays
Last edited on
It seems I'm I'm kinda getting there, but it doesn't show all of them.

This following :
1
2
3
4
for (int i=4; i<count; i++)
{
         outFile <<setw(3)<< empId[i] << setw(20) << hours[i]<<setw(30)<<payRate[i]<<setw(25)<<wages[i] << endl;
}


Should be :
1
2
3
4
for (int i=0; i<count; i++)
{
         outFile <<setw(3)<< empId[i] << setw(20) << hours[i]<<setw(30)<<payRate[i]<<setw(25)<<wages[i] << endl;
}
But it doesn't show it any order. It's exactly the same as the original one. I want to make this second array as a parallel array in ascending order.

THIS is what I want it to look like (below)


Employees in order entered:

Employee Number Hours Worked PayRate per Hour Wages
5658845 40 8 320
4520125 25.25 9.25 233.562
7895122 30 10.5 315
8777541 10 12 120
8451277 50 10.2 510
1302850 35.2 7.5 264

Employee numbers in ascending order


1302850 35.2 7.5 264
4520125 25.25 9.25 233.562
5658845 40 8 320
7895122 30 10.5 315
8451277 50 10.2 512
8777541 10 12 120
Last edited on
It is tough to sort a parallel array while managing the right order of the elements of other arrays too. Better use a struct instead.
Not sure what a struct is, but I NEED to have it in ascended order and I have no idea what do how to call out the selection sort. It seems like no matter where I put it, it doesn't show up.

Whenever I put the selection sort function right below the loop, it puts the ids as 0 like this.
0 40 8 320
0 25.25 9.25 233.562
0 30 10.5 315
0 10 12 120
0 50 10.2 510
0 35.2 7.5 264

Please help me :(
Last edited on
Please help I'm so desperate :(
You could sort the employees in descending order of employee number in at least 3 ways:
(a) overload operator > and call std::sort with std::greater<Employee> as it's custom predicate
(b) using a functor
(c) using lambda
The following program shows all three, search if any term is unfamiliar:
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>

struct Employee
{
    unsigned int m_employeeNumber = 0;
    double m_hoursWorked = 0;
    double m_payRate = 0;
    double m_wages = 0;
    bool operator > (const Employee& rhs)const
    {
        return m_employeeNumber > rhs.m_employeeNumber;
    }
};

struct EmployeeSort
{
    bool operator ()(const Employee& lhs, const Employee& rhs)
    {
        return lhs.m_employeeNumber > rhs.m_employeeNumber;
    }
};

std::ostream& operator << (std::ostream& os, const Employee& e)
{
    os << e.m_employeeNumber << " - " << e.m_wages << "\n";
    return os;
}


int main()
{

    std::ifstream inFile {"F:\\test1.txt"};
    std::vector<Employee> allEmployees{};
    while (inFile)
    {
        Employee temp{};
        inFile >> temp.m_employeeNumber >> temp.m_hoursWorked >> temp.m_payRate >> temp.m_wages;
        if(inFile)
        {
            allEmployees.push_back(std::move(temp));
        }
    }

    std::cout << "Un-sorted employees: \n";
    for (const auto& elem : allEmployees)std::cout << elem;

     std::sort(allEmployees.begin(), allEmployees.end(), std::greater<Employee>());
  //override operator > and calling std::sort with std::greater<Employee>

  /*  std::sort(allEmployees.begin(), allEmployees.end(), EmployeeSort());//functor*/

  /* std::sort(allEmployees.begin(), allEmployees.end(), [](const Employee& lhs, const Employee& rhs)
             {
                 return lhs.m_employeeNumber > rhs.m_employeeNumber;//lambda
             });*/
    std::cout << "\nSorted employees: \n";

    for (const auto& elem : allEmployees)std::cout << elem;

}



Last edited on
Please can you use the original compilers, so none of these please. I shouldn't use the others
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
Last edited on
closed account (48T7M4Gy)
You need to create another new array which keeps track of the order of the employeeid's. This array will be an array of the index values.
Topic archived. No new replies allowed.