Home › Forums › C Programming › Quick Sort for a Struct
- This topic has 2 replies, 2 voices, and was last updated 15 years, 11 months ago by GWILouisaxwzkla.
- AuthorPosts
- December 14, 2008 at 9:48 pm #2168DixieFuquaParticipant
Hey i am new to c plus plus, and really need some help!
Basically i have information, in a file, first name, second name and idnumber stored in a struct for a number of people as such:struct person {
char idnumber [numbersize];
char firstname [namesize];
char secondname [namesize];};
And i need to be able to sort it according to either first name, second name or in ascending idnumber using a quick sort. Is it possible to write a general function and use pointers to assign the relevant piece of information to sort by e.g first name etc?. Then, is it possible to call the function and print the sorted list?
If someone could show me some helpful code or some hints it would be greatly appreciated.
Thanks. - December 15, 2008 at 8:03 pm #3498GWILouisaxwzklaParticipant
The structs will form a linked list , correct? The sorting key will be variable amongst the struct field members ( the user will chose a struct member to sort the data on ) ?? Is the list to be “disconnected” for sorting ( structs will be moved and the list reconnected ) or is data to be shifted ( probably easier )…
- January 6, 2009 at 11:56 pm #3499GWILouisaxwzklaParticipant
Heres a way to do this with a doubly linked list with quicksort. This version of quicksort uses the last item in the
list as a pivot element when partitioning.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : January,6,2008<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:51:46 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream ><br />#include < fstream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />#include < iomanip ><br />#include < ctype.h ><br /><br />using namespace std;<br /><br />//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<<br /><br />const int MAX_NAME = 50;<br />const int MAX_FILE_NAME = 50;<br />const char STOP_SYMBOL = '*';<br />enum keys { firstName , lastName , idNumber };<br />keys sortKey ;<br />#define OUTPUT_SPACES 15<br />//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br /><br /><br /><br />struct person<br />{<br /><br />char firstName [ MAX_NAME ] ;<br />char lastName [ MAX_NAME ] ;<br />char idNumber [ MAX_NAME ] ;<br />person * prior;<br />person * next;<br />};<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />bool readInputFile ( char * fileName , person *& frontList , person *& backList , int & itemsRead );<br />void quickSort ( person ** front , person ** last , int numberItems );<br />void swap ( person ** first , person ** last , person ** front , person ** back );<br />void print ( person * list );<br />void insertionSort ( person * list , int numberOfItems );<br />void destroyList ( person * list );<br />char * key ( person * node );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />person * front , * back;<br />int items;<br /><br />sortKey = firstName;<br />readInputFile ( "c:\programs\data.txt" , front , back , items );<br />print ( front );<br />quickSort ( & front , & back , items );<br />cout << endl << endl;<br />cout << "final ************************" << endl;<br />print ( front );<br />destroyList ( front );<br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : readInputFile<br />Parameters :<br /><br />fileName a(n) char * ,<br />frontList a(n) person *& ( person *& ) ,<br />backList a(n) person *& ( person *& ) ,<br />itemsRead a(n) int &<br /><br /><br />Returns: bool type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />bool readInputFile ( char * fileName , person *& frontList , person *& backList , int & itemsRead )<br />{<br /><br /><br />fstream inputFile;<br />person * temp;<br />char ch;<br />inputFile.open ( fileName ); //open input file<br />if ( inputFile.fail () )<br />{<br />cout << "input file did not open! " << endl;<br />return false;<br />}<br />temp = new person; //make new node in list<br />if ( temp == 0 ) //if allocation error<br />{<br />cout << "allocation error! " << endl;<br />return false;<br />}<br />itemsRead = 1; //start count at 1<br />frontList = backList = temp; //set front and back pointers to new node<br />temp -> prior = 0; //set new nodes prior to empty<br />inputFile >> temp -> firstName; //get nodes data from file<br />inputFile >> temp -> lastName;<br />inputFile >> temp -> idNumber;<br />ch = inputFile.peek(); //check for end of file character<br />while ( isspace ( ch ) ) //skip white space<br />{<br />inputFile.get();<br />ch = inputFile.peek();<br />}<br />while ( ch != STOP_SYMBOL ) //while not at end of file<br />{<br />temp -> next = new person; //make new item in the list<br />if ( temp -> next == 0 ) //check allocation<br />{<br />cout << "allocation error! " << endl;<br />destroyList ( frontList );<br />return false;<br />}<br />temp -> next -> prior = temp; //set temp to point to new node<br />temp -> next -> next = 0; //set next to empty<br />temp = temp -> next;//move list pointer to next node<br />backList = temp; //make new node the back of list<br />inputFile >> temp -> firstName; //get data<br />inputFile >> temp -> lastName;<br />inputFile >> temp -> idNumber;<br />ch = inputFile.peek(); //get white space<br />while ( isspace ( ch ) )<br />{<br />inputFile.get();<br />ch = inputFile.peek();<br />}<br />itemsRead ++;//count items read<br />}<br /><br />return true;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : quickSort<br />Parameters :<br /><br />front a(n) person *& ( person *& ) ,<br />last a(n) person *& ( person *& ) ,<br />numberItems a(n) int ( int )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void quickSort ( person ** low , person ** high , int numberItems )<br />{<br /><br /><br />person * savedLow = * low; //lowt<br />person * savedHigh = * high;//hight<br />person * tempLow , * tempHigh;<br />person * pivot; //pivot for sorting<br /><br />if ( numberItems <= 1 ) //nothing to sort<br />return;<br />if ( numberItems == 2 ) //if 2 items then swap if needed<br />{<br />tempLow = * low;<br />tempHigh = * high;<br />if ( strcmp ( key ( tempLow ) , key ( tempHigh ) ) > 0 ) //swap items if out of order<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br />* low = tempLow; //reset low and high pointers<br />* high = tempHigh;<br />return ; //end function<br />}<br /><br /><br /><br />pivot = savedHigh; //make the highest element in the array the pivot<br />tempLow = savedLow; //start sorting at the lowest item in the list<br />tempHigh = ( * savedHigh ).prior; //start sorting at the second to last item<br />int lowCount = 1; //start counting at the first item for the low pointer<br />int highCount = numberItems - 1; //start counting at the second to last item for high count<br /><br />do<br />{<br /><br />//while items are less than the pivot move right<br />while ( lowCount <= highCount && ( strcmp ( key ( tempLow ) , key ( pivot ) ) < 0 || strcmp ( key ( tempLow ) , key ( pivot ) ) == 0 ) )<br />{<br /><br /><br />tempLow = tempLow -> next;<br />lowCount ++; //increase count of items behind pointer<br />if ( ! tempLow )<br />break;<br />}<br />//while items are greater than the pivot move right<br />while ( lowCount <= highCount && ( strcmp ( key ( tempHigh ) , key ( pivot ) ) > 0 || strcmp ( key ( tempLow ) , key ( pivot ) ) == 0 ) )<br />{<br /><br /><br />tempHigh = tempHigh -> prior;<br />highCount --; //decrease count of items before pointer<br />if ( ! tempHigh )<br />break;<br />}<br /><br />if ( tempHigh ) //if top pointer has not crossed end of list , swap items<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br /><br />} while ( lowCount <= highCount && tempHigh -> prior && tempLow -> next );<br /><br />if ( tempHigh ) //if high pointer has not passed the end to the list , exchange high and low pointers<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br /><br />//put pivot in middle of large item and small item lists<br />tempHigh = savedHigh;<br />swap ( & tempLow , & tempHigh , & savedLow , & savedHigh );<br />//sort list of small items<br />quickSort ( & savedLow , & tempLow -> prior , lowCount - 1 );<br />//sort list of large items<br />quickSort ( & tempLow -> next , & savedHigh , numberItems - lowCount );<br />//reset low and high pointers<br />* low = savedLow;<br />* high = savedHigh;<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : swap<br />Parameters :<br /><br />first a(n) person *& ( person *& ) ,<br />last a(n) person *& ( person *& )<br /><br /><br />Returns: Void type<br />Comments: first<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void swap ( person ** left , person ** right , person ** front , person ** back )<br />{<br /><br /><br />if ( ! * left || !* right ) //if left or right pointer is null , stop<br />return;<br />if ( * left == * right )<br />return;<br />//reset the front and back pointers<br />if ( ( * front ) == ( * left ) ) //if left == front<br />{<br /><br />* front = * right; //right is new front after swap<br />}<br />else if ( ( * front ) == ( * right ) ) //if right == front<br />{<br /><br />* front = * left; //left is new front after swap<br />}<br />if ( ( * back ) == ( * left ) ) //if left == back<br />{<br /><br />* back = * right; //right is back after swap<br />}<br />else if ( ( * back ) == ( * right ) ) //if right is back<br />{<br /><br />* back = * left; //left is back after swap<br />}<br /><br /><br /><br />person * tempN , * tempP;<br /><br />tempN = ( * left ) -> next; //save left's next and prior node setting<br />tempP = ( * left ) -> prior;<br /><br />//case 1: right node follows left in list<br />if ( ( * right ) -> prior == * left )<br />{<br />( * left ) -> prior = ( * right ); //right is left's new prior node<br />( * left ) -> next = ( * right ) -> next; //left's next node is right's old prior node<br />if ( ( * left ) -> next ) //if left had a next node<br />( * left ) -> next -> prior = ( * right ); //set that nodes prior to right<br />( * right ) -> next = ( * left ); //right's next now is left<br />( * right ) -> prior = tempP; //right's new prior is left's old prior<br />if ( tempP ) //if left's old prior is not null<br />tempP -> next = ( * right ); //left's old prior's next is right<br />}<br />//case 2: left follows right in the list<br />else if ( ( * left) -> prior == ( * right ) )<br />{<br />( * left ) -> prior = ( * right ) -> prior; //left's prior is now right's prior<br />( * left ) -> next = ( * right ); //left's next is now right<br />if ( ( * right ) -> prior ) //if right's prior is not null<br />( * right ) -> prior -> next = ( * left ); //set its next to left<br />( * right ) -> prior = ( * left ); //right comes after left node now<br />( * right ) -> next = tempN; //right's next is left's old next<br />if ( tempN ) //if left's old next is non-null<br />tempN -> prior = ( * right ); //set its prior to right<br />}<br />//case 3: the two nodes are seperated by other nodes<br />else<br />{<br />( * left ) -> prior = ( * right ) -> prior; //left's prior equal's right's<br />( * left ) -> next = ( * right ) -> next; //left's next equal's right's<br /><br />if ( ( * right ) -> next ) //if right's next is non-null<br />( * right ) -> next -> prior = ( * left ); //set it's prior to left<br />if ( ( * right ) -> prior )//if right's prior is non-null<br />( * right ) -> prior -> next = ( * left ); //it's next now equals left<br />( * right ) -> prior = tempP; //right's prior now == left's<br />( * right ) -> next = tempN; //right's next now == left's<br />if ( tempP ) //if left's prior was non-null<br />tempP -> next = ( * right ); //set its next to right<br />if ( tempN ) //if left's next was non-null<br />tempN -> prior = ( * right );//set its prior to right<br />}<br /><br /><br />person * temp = ( * left ); //swap left and right pointers to save<br />person * temp2 = ( * right );//position of pointers in the list<br />( * left ) = temp2;<br />( * right ) = temp;<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : print<br />Parameters :<br /><br />list a(n) person * ( person * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void print ( person * list )<br />{<br /><br />cout << endl;<br /><br />cout << setiosflags ( ios::left );<br />cout << setw ( OUTPUT_SPACES ) << "first name"<br /><< setw ( OUTPUT_SPACES ) << "last name"<br /><< setw ( OUTPUT_SPACES ) << "id number" << endl << endl;<br />while ( list ) //while not at the end of the list<br />{<br />//output name ect....<br />cout << setw ( OUTPUT_SPACES ) << list -> firstName << " " << setw ( OUTPUT_SPACES ) << list -> lastName << " " << setw ( OUTPUT_SPACES ) << list -> idNumber << endl;<br />list = list -> next;<br /><br />}<br /><br />return;<br />}<br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : destroyList<br />Parameters :<br /><br />list a(n) person * ( person * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void destroyList ( person * list )<br />{<br /><br />person * temp;<br /><br />while ( list ) //for each item in the list , destroy the list<br />{<br />temp = list;<br />list = list -> next;<br />delete temp;<br />}<br /><br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : key<br />Parameters :<br /><br />first a(n) person * ( person * ) ,<br />second a(n) person * ( person * )<br /><br /><br />Returns: char type<br />Comments: use a macro for this......<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />char * key ( person * node )<br />{<br /><br />switch ( sortKey )<br />{<br />case firstName:<br />return node -> firstName;<br />case lastName:<br />return node -> lastName;<br />case idNumber:<br />return node -> idNumber;<br /><br />};<br /><br /><br />}<br />input:
123456789101112131415161718192021<br />bob johns 222<br />suzie barker 333<br />adam west 555<br />zack wilde 111<br />janet woods 444<br />qubert johns 666<br />barak obama 777<br />carly simon 888<br />curt cobain 999<br />tom gadja 1010<br />larry bear 1111<br />pilar nuffer 1212<br />mikey scalcee 1313<br />old puff 1414<br />charolette phifer 1515<br />don dokken 46464<br />adam ant 555<br />willodean mcqueen 222<br />*<br />output ( sorting with first name as key ):
1234567891011121314151617181920212223<br />first name last name id number<br /><br />adam ant 555<br />adam west 555<br />barak obama 777<br />bob johns 222<br />carly simon 888<br />charolette phifer 1515<br />curt cobain 999<br />don dokken 46464<br />janet woods 444<br />larry bear 1111<br />mikey scalcee 1313<br />old puff 1414<br />pilar nuffer 1212<br />qubert johns 666<br />suzie barker 333<br />tom gadja 1010<br />willodean mcqueen 222<br />zack wilde 111<br />Press any key to continue<br />
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.