sort not working

S

SB

I have a class called Process with the following...
class Process
{
private:
int processID; // 2 digit number starting at 10
int memorySize; // size of memory allocated to each process
int timeQuantum; // execution time of each process

public:
Process(); // default constructor
void setProcessID(); // sets the process id
int getProcessID() const; // returns the process id
void setMemorySize(int); // sets the memory size
int getMemorySize() const; // returns the memory size
void setTimeQuantum(); // sets the time quantum
int getTimeQuantum() const; // returns the time quantum
};

Then I have a Scheduler class which contains Process object. Here is the
Scheduler class...
class Scheduler
{
private:
Process p; // holds a Process object
int processes; // number of process input by user
vector<Process> schedulerVector; // vector containing the process id, mem
size and time quantum of each process

public:
Scheduler(); // default constructor
void setProcesses(); // sets the number of processes
int getProcesses() const; // returns the number of processes
Process createProcess(int); // creates a new Process object
void createVector(); // populates the vector with the processes
vector<Process> getVector() const; // returns the vector of Processes
void displayVector() const; // displays contents of the vector
void swapVectorElements(Process&, Process&);// swaps two Process objects in
the vector
void sortVector(vector<Process>&); // sorts the vector based on time
quantum
void displayTimeQuantum() const; // displays processes according to
execution time
};

In the Scheduler class I create a vector and populate it with Process
objects, like so...
void Scheduler::createVector()
{
// populate the vector using processes as the loop counter since
// size of the vector is bound to the number of processes
for (int i = 0; i < processes; i++)
{
Process p = createProcess(i); // create a new Process
schedulerVector.push_back(p); // add each Process to the vector
}
}
I want to sort the vector based on the timeQuantum field in the Process
class. When I call the sort function, it seems to work but when I display
the vector after sorting, it is in the same order as it was before sorting.
I'm using pass by reference to swap as well. Here is the code...

void Scheduler::swapVectorElements(Process& p1, Process& p2)
{
cout<<endl<<"Stub for swapVectorElements"<<endl;
Process temp;

temp = p1;
p1 = p2;
p2 = temp;
}

void Scheduler::sortVector(vector<Process>& v)
{
cout<<endl<<"Sorting the vector of size "<<v.size()<<"..."<<endl;
int i;
int j;

for (i = 0; i < v.size(); i++)
{
for (j = 0; j < v.size(); j++)
{
if (v[j].getTimeQuantum() > v[j+1].getTimeQuantum())
{
swapVectorElements(v[j], v[j+1]);
}
}
}
}

Can anyone see why this is not working?

Thanks!
 
J

John Harrison

SB said:
I have a class called Process with the following...
class Process
{
private:
int processID; // 2 digit number starting at 10
int memorySize; // size of memory allocated to each process
int timeQuantum; // execution time of each process

[snip]


void Scheduler::sortVector(vector<Process>& v)
{
cout<<endl<<"Sorting the vector of size "<<v.size()<<"..."<<endl;
int i;
int j;

for (i = 0; i < v.size(); i++)
{
for (j = 0; j < v.size(); j++)
{
if (v[j].getTimeQuantum() > v[j+1].getTimeQuantum())
{
swapVectorElements(v[j], v[j+1]);
}
}
}
}

Can anyone see why this is not working?

Yes, your algorithm is wrong. Just try running it though by hand and you'll
see that it doesn't work.

Firstly it accesses beyond the bounds of the vector, secondly it doesn't use
i, but even when these two are corrected, it isn't a good alogithm.

Suggest you look up insertion sort in an algorithm book. Alternatively if
the purpose is not to learn how to code sorting algorithms but to actually
get the vector sorted you should use the std::sort function.

john
 
S

SB

Thanks for the tip. Could you explain how to use the std::sort function? I'm
running out of time and am having trouble getting it work.

Thanks
John Harrison said:
SB said:
I have a class called Process with the following...
class Process
{
private:
int processID; // 2 digit number starting at 10
int memorySize; // size of memory allocated to each process
int timeQuantum; // execution time of each process

[snip]


void Scheduler::sortVector(vector<Process>& v)
{
cout<<endl<<"Sorting the vector of size "<<v.size()<<"..."<<endl;
int i;
int j;

for (i = 0; i < v.size(); i++)
{
for (j = 0; j < v.size(); j++)
{
if (v[j].getTimeQuantum() > v[j+1].getTimeQuantum())
{
swapVectorElements(v[j], v[j+1]);
}
}
}
}

Can anyone see why this is not working?

Yes, your algorithm is wrong. Just try running it though by hand and you'll
see that it doesn't work.

Firstly it accesses beyond the bounds of the vector, secondly it doesn't use
i, but even when these two are corrected, it isn't a good alogithm.

Suggest you look up insertion sort in an algorithm book. Alternatively if
the purpose is not to learn how to code sorting algorithms but to actually
get the vector sorted you should use the std::sort function.

john
 
R

Russell Hanneken

SB said:
Could you explain how to use the std::sort function?

#include <algorithm>

// . . .

bool orderedByTimeQuantum (Process const &p1, Process const &p2)
{
return p1.getTimeQuantum() < p2.getTimeQuantum();
}

// . . .

void Scheduler::sortVector(vector<Process>& v)
{
std::sort(v.begin(), v.end(), orderedByTimeQuantum);
}

But I wonder: why are you passing an argument to sortVector?

If the point of the function is to sort the member of Scheduler called
"schedulerVector," why not just refer to schedulerVector, instead of
making someone pass the vector in to the function as an argument?
sortVector is a member of Scheduler, so it has access to private members
of the class.

And who is calling sortVector? No function outside the class has access
to schedulerVector, so no outside function could pass it as an argument.
Only member functions of Scheduler could pass schedulerVector to
sortVector. But if that's what you're doing, why make sortVector public
rather than private?

If the purpose of sortVector is to give the calling function the ability
to sort *any* vector of Process objects that it might happen to have,
why make sortVector a member of Scheduler at all?
 
J

John Harrison

Russell Hanneken said:
#include <algorithm>

// . . .

bool orderedByTimeQuantum (Process const &p1, Process const &p2)
{
return p1.getTimeQuantum() < p2.getTimeQuantum();
}

// . . .

void Scheduler::sortVector(vector<Process>& v)
{
std::sort(v.begin(), v.end(), orderedByTimeQuantum);
}

But I wonder: why are you passing an argument to sortVector?

Yes, my guess is that this should be

void Scheduler::sortVector()
{
std::sort(schedulerVector.begin(), schedulerVector.end(),
orderedByTimeQuantum);
}

The comment in the header file says 'sort the vector' not 'sort a vector'.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top