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:

New Topic/Question
Reply




MultiQuote





|