4
Reply

quick sort problem .. made the code function correctly .

SUNIL GUTTA

SUNIL GUTTA

Oct 21 2013 4:38 PM
1.1k
Hi


display unsored records first and then display sorted record using quick sort (according to ID). alsdeo repeat the above on three sets of student records.Its keep getting Error, but i dont have Idea~ please help me and check my code


#ifndef pro5_H
#define pro5_H


#include<iostream>
#include <cstring>
#include<iomanip>
#include<vector>


using namespace std;




class Student
{
private:
string nam;
int iden;
double grade;
string address;
int test1;
int test2;
int test3;
int test4;
int test5;
int test6;
int test7;
int test8;
int test9;
int test10;




public:
void setRecord( string name, int ID, double GPA, string addr, int t1,int t2,int t3,int t4,int t5,int t6,int t7,int t8,int t9,int t10);


void print();
int partition( vector<Student>array,int start, int end);
void quicksort(vector<Student>array, int start, int end);
void swap(Student &st1, Student &st2)


};






void Student::setRecord( string name, int ID, double GPA, string addr, int t1,int t2,int t3,int t4,int t5,int t6,int t7,int t8,int t9,int t10)
{


nam=name;
iden=ID;
grade=GPA;
address=addr;
test1=t1;
test2=t2;
test3=t3;
test4=t4;
test5=t5;
test6=t6;
test7=t7;
test8=t8;
test9=t9;
test10=t10;
}


int Student::partition(vector<Student> array,int first, int end)
{


int pivot, index, mid;
swap(array[first], array[mid]);
index=first;
pivot=array[first].iden;
for(int i= first+1; i<=end;i++)
{
if(array[i].iden<pivot)
{
index++;
swap(array[index],array[i]);


}
}
swap(array[first],array[index]);
return index;




}


void Student::quicksort(vector<Student> array,int first, int end)
{
int temp;
if(first< end)
{
temp=partition(array,first,end);
quicksort(array, first, temp-1);
quicksort(array, temp+1, end);
}
}
void swap(Student &st1, Student &st2)
{
Student temp= st1;
st1=st2;
st2=temp;
}


void Student::print()
{


cout<<nam<<" "<<iden<<" "<< grade<<" "<<address<<" "<<test1<<" "<<test2<<" "<<test3<<" "<<test4<<" "<<test5<<" "<<test6<<" "<<test7<<" "
<<test8<<" "<<test9<<" " <<test10<<" "<<endl;


}
#endif
#include "pro5.h"
#include <iostream>
#include <string>
#include <vector>


using namespace std;




int main()
{
Student v[20];
vector<Student>V;
v[0].setRecord("Ryan",1234,3.2, "Austin",50,90,80,57,87,98,87,39,70,89);
v[1].setRecord("Joo", 2345,3.4, "Dallas",56,87,67,98,67,90,56,45,67,84);
v[2].setRecord("Choo",3452,3.4, "Austin",76,90,89,90,93,90,98,90,99,76);
v[3].setRecord("Jay", 4123, 2.2, "San Marcos",45,67,56,44,34,23,76,54,34,43);
v[4].setRecord("Ryan",2345,4.0,"Houston", 34,25,56,78,72,81,67,89,67,56);
v[5].setRecord("koo", 3456,2.8,"Austin", 36,23,44,67,54,76,34,56,43,23);
v[6].setRecord("Lee", 9808,3.1,"Dallas",45,65,76,34,87,54,67,76,89,56);
v[7].setRecord("Kim", 7634,2.3,"Houston",45,78,97,67,89,90,89,99,87,65);
v[8].setRecord("Lisa",4560,3.5,"Dallas",67,89,90,87,89,78,67,56,90,87);
v[9].setRecord("Eric",9087,3.2,"Austin",34,56,78,87,98,67,78,67,78,87);
v[10].setRecord("Jean",8763,2.4,"Dallas",34,90,98,96,90,87,85,65,74,70);
v[11].setRecord("Bryan",7834,3.2,"Houston",87,87,89,87,67,89,67,76,76,56);
v[12].setRecord("Emi",9823,3.1,"Kyle",97,67,88,87,67,89,78,98,56,88);
v[13].setRecord("Tony",3423,3.0,"Corpus", 87,54,66,45,34,89,87,56,89,90);
v[14].setRecord("Tom",4567,2.4,"Plano", 54,67,54,78,98,90,778,87,49,87);
v[15].setRecord("Tylor",4321,1.8,"Dallas",34,43,34,56,12,23,0,0,12,43);
v[16].setRecord("Leah",2367,2.1,"Austin",43,56,78,99,87,65,43,44,78,65);
v[17].setRecord("Diana",5664,2.7,"Houston",32,56,87,97,67,87,98,67,67,78);
v[18].setRecord("Derrick",7964,2.1,"Plano",54,67,87,56,45,78,98,65,86,89);
v[19].setRecord("Corry",2317,3.3,"San Antonio",34,78,90,87,89,67,85,87,89,65);
//v[20].setRecord("Ray",1235,3.8,"Austin",87,89,98,78,89,90,98,87,90,78);
for(int i=0;i<20;i++)
{
v[i].print();
V.push_back(v[i]);
quicksort(V.push_back(v[i]), 0,v[i]);
cout<<"Sorted by Studnet ID: "<<endl;
cout<<"____________________________________________"<<endl;
cout<<endl;
v[i].print();
}


system("PAUSE");


return 0;
}

Cheers
Have a nice day

Answers (4)