Ok so i need to sort the data from a struct that has to be set out like so
struct Record {
char name[30];
int age;
int phone;
double salary;
};
Simple enough to fill the struct with the data from a file called records.txt but it isn't ordering. I have tried many ways of quicksorting but can't find out where i'm going wrong.
Anyways it would be great if you could point out what is wrongwith this code...
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
struct Record {
char name[30];
int age;
int phone;
double salary;
};
void quickSort(Record[], int, int);
int splitArray(Record[], Record, int, int);
void swap(Record[], Record[]);
void print(Record[]);
int main()
{
ifstream ins;
ins.open("records.txt");
if(ins.good())
{
int files = 0;
ins >> files;
Record Records[files];
for(int i=0; i<files; i++)
{
ins.getline(Records[i].name, 30, ',');
ins >> Records[i].age;
ins.get();
ins >> Records[i].phone;
ins.get();
ins >> Records[i].salary;
}
quickSort(Records, 0, files-1);
print(Records);
}
}
void quickSort(Record array[], int startIndex, int endIndex)
{
Record pivot = array[startIndex];
int splitPoint;
if(endIndex > startIndex)
{
splitPoint = splitArray(array, pivot, startIndex, endIndex);
array[splitPoint] = pivot;
quickSort(array, startIndex, splitPoint-1);
quickSort(array, splitPoint+1, endIndex);
}
}
int splitArray(Record array[], Record pivot, int startIndex, int endIndex)
{
int leftBoundary = startIndex;
int rightBoundary = endIndex;
while(leftBoundary < rightBoundary)
{
while(&pivot.name < &array[rightBoundary].name && rightBoundary > leftBoundary)
{
rightBoundary--;
}
swap(array[leftBoundary], array[rightBoundary]);
print(array);
while(&pivot.name >= &array[leftBoundary].name && leftBoundary < rightBoundary)
{
leftBoundary++;
}
swap(array[rightBoundary], array[leftBoundary]);
}
return leftBoundary;
}
void swap(Record a, Record b)
{
Record temp = a;
a = b;
b = temp;
}
void print(Record Records[])
{
for(int i=0; i<10; i++)
{
cout << Records[i].name << " ";
}
cout << endl;
}
Thanks