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

void swap(Record a, Record b)
{
        Record temp = a;
	a = b;
	b = temp;
}

is not swapping the structs (as copies are being made). Try passing them by reference swap(Record & a,Record & b) or by pointer.

Edited 6 Years Ago by jonsca: n/a

void swap(Record a, Record b)
{
        Record temp = a;
	a = b;
	b = temp;
}

is not swapping the structs (as copies are being made). Try passing them by reference swap(Record & a,Record & b) or by pointer.

Thanks for that correction but it still isn't working.
In the swap function i wrote

cout << a.name << " " << b.name<< endl

and the output is the same name twice.
So the line before calling the function

while(pivot.name < array[rightBoundary].name && rightBoundary > leftBoundary)
		{
			rightBoundary--;
		}

is going through til rightBoundary > leftBoundary and pivot.name < array[rightBoundary] does nothing.

Ok so i made some changes and it manages to sort some of the list but not quite there yet... Here is the 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.get();
			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(strcmp(pivot.name, array[rightBoundary].name) <= 0 && rightBoundary > leftBoundary)
		{
			rightBoundary--;
		}
		swap(array[leftBoundary], array[rightBoundary]);
		print(array);
                while(strcmp(pivot.name, array[rightBoundary].name) >= 0 && 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 << endl;
	}
	cout << endl;
}

I attached the test program that I used. Even with the character array in the struct it swaps things okay. Try yours in isolation from your program to gauge for sure.

EDIT: I think our posts crossed. I'll take another look at your current code.

Edited 6 Years Ago by jonsca: n/a

Attachments
#include <cstring>
#include <iostream>

struct Record
{
	int marker1;
	char marker2[10];
	

};

void swap(Record & a, Record & b)
{
    Record temp = a;
	a = b;
	b = temp;
	std::cout<<a.marker2<<" "<<b.marker2<<std::endl;
}

int main()
{
	Record a ={1,"this"};
	Record b = {2,"is"};

	std::cout<<a.marker1<<" "<< a.marker2<<std::endl;
	std::cout<<b.marker1<<" "<< b.marker2<<std::endl;
	swap(a,b);
	std::cout<<a.marker1<<" "<< a.marker2<<std::endl;
	std::cout<<b.marker1<<" "<< b.marker2<<std::endl;
	

	return 0;


}

hey, i was just interested in your program. I am a beginner c++ programer. I was wondering how you would sort an array of structs, simular to urs, but by sorting it by pointers as to make the program more efficient??

This article has been dead for over six months. Start a new discussion instead.