3 Replies - 2852 Views - Last Post: 07 December 2009 - 08:23 AM Rate Topic: -----

#1 hailegebru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

sorting string with bubble,selection and insertion i am using borland

Posted 07 December 2009 - 05:43 AM

hey :
i am having slight trouble regarding the insertion sort ...when i run the code ,i am not able to see the output immediately .i am sure that the sorting is done perfectly ....
would u help me fixing the bug there?

how can i add a selection sort to this program?
#include<iostream.h>
  #include<conio.h>
  #include<string.h>
	 struct names{
	 char name[20];
	 //char mname[20];

 };

 void bubble();
 void insertionsort();

	 int main(){
	 clrscr();
	 int validate;
	  cout<<"enter 1  to use bubble sort	  enter 2  to use insertion sort";
	  cin>>validate;
	  if(validate==1){
	 bubble();
	 }
	 else if (validate==2){

	 cout<<"sorting using insertion:"<<endl;
	 insertionsort();
	 }
	 else
	 {
	 }
	 return 0;
 }



	 void bubble(){
	 int n;
	 char temp[20];
	cout<<"						   ########################################"<<endl;
	 cout<<"						  ########################################"<<endl;
	 cout<<"						  ########################################"<<endl;
	cout<< """"""""""<<"welcome to sorting program"<<endl;
	cout<<"   this is sorting with bubble"<<endl;
	 cout<<"how many name  u like to sort"<<endl;
	  cin>>n;
	 names * array=new names[n];


	 for(int i=0;i<n;i++) {

cout<<"Give me the   name"<<endl;

cin>>array[i].name;
	 }


	for (int x=1; x<n; x++)
	for (int y=0; y<n-x; y++)


	if ( strcmp ( array[y].name,
 array[y+1].name) >0){
	 strcpy(
temp,array[y].name);
	 strcpy(array[y].name, array[y+1].name );
	 strcpy(
 array[y+1].name, temp );
	 }



	 for(int  a=0;a<n;a++){

 cout<<"The sorted names are: "<<array[a].name<<endl;
	 }

	 getch();
 }

 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 //###############################################
 //############################################
 //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

	   void insertionsort(){
			int n;
			char temp[20];
		   //char mname[20];
		cout<<"how many names u want to sort";
		  cin>>n;
	  names * array=new names[n];


	 for(int i=0;i<n;i++) {

cout<<"Give me the   name"<<endl;

cin>>array[i].name;
}
for (int x=1; x<n; x++)
	for (int j=x; j>0; j--)


	if ( strcmp ( array[j].name, array[j-1].name) < 0){
	 strcpy(temp,array[j].name);
	 strcpy(array[j].name, array[j-1].name );
	 strcpy(
 array[j-1].name, temp );
	 }



	 for (int  d=0;d<n;d++){

		 cout<<"The sorted names are: "<<array[d].name<<endl;
			  }
}


*Mod Edit: added code tags: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: sorting string with bubble,selection and insertion i am using borland

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: sorting string with bubble,selection and insertion i am using borland

Posted 07 December 2009 - 06:11 AM

What do you wanna do with this line?
names * array=new names[n];

Was This Post Helpful? 0
  • +
  • -

#3 hailegebru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

Re: sorting string with bubble,selection and insertion i am using borland

Posted 07 December 2009 - 08:15 AM

View Postponcho4all, on 7 Dec, 2009 - 05:11 AM, said:

What do you wanna do with this line?
names * array=new names[n];

Was This Post Helpful? 0
  • +
  • -

#4 hailegebru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

Re: sorting string with bubble,selection and insertion i am using borland

Posted 07 December 2009 - 08:23 AM

names * array=new names[n]; i used this to dynamically allocate memory space of the name array .......actually the code is error free i want to show me how to add a selectionsort function to sort list of name from the user

the bubble sort acutually works fine.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1