Thursday, August 23, 2007

Insertion Sort

/* Question 2 Insertion Sort Algorithm

Description: Sorts ten names and coressponding telephone numbers

using a database to add names to the list, in decending order.

Files:

ASCII File = amx.dat found on a:\\ drive

CPP File = a57q1.cpp

Date: 17/9/2001 (Final Revision) */



#include // automacically includes

#include // for scrcmp function



int size; // size of the list



class List_class // class named List_class

{

private:

char name[30][10],phone[30][10]; //private data mebers for name and telephone numbers

public:

List_class(); //methods

void to_screen(int &pinhead);

void insertion_sort(char newname[],char newnumber[]);

};



//------------------------------------------------------------------



void main (void)

{

List_class name_list; // declaration of List_class object name_list

char newname[30],newnumber[10];

cout << "This is the list: \n"<
name_list.to_screen(size); // display to screen



cout << "Enter a new name to add to the list or -1 to exit:"; // prompt for new name

cin >> newname; // get the info

cout << "Enter a corresponding phonenumber to add to the list: "; //prompt for new telephone number

cin >> newnumber; //get the info



while(strcmp(newname,"-1")!=0) // while statement breaks on -1

{

name_list.insertion_sort(newname,newnumber) ; // insert new name correctly ordered

name_list.to_screen(size); // displays list to screen

cout << endl; // them's the breaks kid!

cout << "Enter a new name to add to the list or -1 to exit: ";

cin >> newname;

if (strcmp(newname,"-1")!=0)

{

cout << "Enter a coressponding phonenumber to add to the list: ";

cin >> newnumber;

} // end if

} // end while

}// end main



//---------------------------------------------------------



List_class::List_class() //reads the list in from a file

{

int a ; // tags the postition in the for loop

size = 0; //preliminary inialization

fstream infile ("a:/qu2/a57q2a.dat",ios::in); // call the data file named infile



for(a = 0;a < 10;a = a + 1) // for loop

{

infile >> name[a] >> phone[a]; // read the list

size++; //increment size

}// end for

infile.close(); // close the file

}// end constructor



//----------------------------------------------------------------



void List_class::to_screen(int &pinhead) //displays the list to the screen

{

int a, array; // tag the elements

array = pinhead; // inialization of array to max



fstream outfile("a:/qu2/a57q2b.dat",ios::out); // record new values

for(a = 0; a < array; a = a + 1) // for loop

{

cout<< name[a] << " "<< phone[a] << endl; // display name and telephone number to screen

outfile << name[a] << " " << phone[a] << endl; //write to file new.dat

}// end for

outfile.close(); // close the file

}// end to_screen



//---------------------------------------------------------------



void List_class::insertion_sort(char newname[],char newnumber[]) //sorts a new name into the sorted list

{

int old,spot = 0,spotfound = 0; // insertion sort mehtod variables





while (!spotfound && spot < size) // while loop while breaks if spot is not found and spot is less than size

{

if (strcmp(newname, name[spot])>0) // tests name position spot with newname

{

spotfound = 1; // true

}

else

{

spot = spot + 1; // move on

}// end if

}// end while

for (old = size; old > spot; old = old -1) //for loop

{

strcpy(name[old], name[old-1]); // copy name from old-1 into old variable

strcpy(phone[old], phone[old-1]); //coressponding telephone numbers

}// end for

size = size +1; //increment size



strcpy(name[spot], newname); // copies newname into name

strcpy(phone[spot], newnumber); //coressponding telephone numbers



}// end insertion_sort

No comments: