LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   selection sort compiles but does not sort the array as desired (http://www.linuxquestions.org/questions/programming-9/selection-sort-compiles-but-does-not-sort-the-array-as-desired-633399/)

ganesha 04-06-2008 05:35 AM

selection sort compiles but does not sort the array as desired
 
here is the program that i had wrote plese compile on your machine and suggest the error

#include<iostream>
using namespace std;
void select(int *const,int const,bool(*)(int,int));
void swap(int *const,int *const);
bool ascending (int,int);
bool descending(int,int);
int main()
{
const int arraysize=5;
int a[arraysize]={23,1,34,12,7};
int order;
cout<<"\nunsorted array is:";
for(int i=0;i<arraysize;i++)
cout<<" "<<a[i];
cout<<endl;
cout<<"\n enter order 1 for listing of elements in ascending order \n 2 for listing in descending order "<<endl;
cin>>order;
if(order==1)
{
cout<<"\n array in ascending order is :";
select(a,arraysize,ascending);
}
else
{
cout<<"\n array in descending order is :";
select(a,arraysize,descending);
}
for(int i=0;i<arraysize;i++)
cout<<" "<<a[i];
cout<<endl;
return 0;
}
void select(int *const arr,const int size,bool (*compare)(int ,int ))
{
int smallorlargest;
for(int i=0;i<size;i++)
{
smallorlargest=i;
for(int j=i+1;j<size;j++)
{
if(!(*compare)( arr[smallorlargest],arr[j]))
{
smallorlargest=j;
}
swap( &arr[smallorlargest],&arr[i] );
}
}
}
void swap(int *const a,int *const b)
{
int x=*a;
*a=*b;
*b=x;
}
bool ascending(int x,int y)
{
return x < y;
}
bool descending(int p,int q)
{
return p > q;
}

lali.p 04-06-2008 07:52 AM

Code:

#include<iostream>
using namespace std;
void select(int *const,int const,bool(*)(int,int));
void swap(int *const,int *const);
bool ascending (int,int);
bool descending(int,int);
int main()
{
const int arraysize=5;
int a[arraysize]={23,1,34,12,7};
int order;
cout<<"\nunsorted array is:";
for(int i=0;i<arraysize;i++)
cout<<" "<<a[i];
cout<<endl;
cout<<"\n enter order 1 for listing of elements in ascending order \n 2 for listing in descending order "<<endl;
cin>>order;
if(order==1)
{
        cout<<"\n array in ascending order is :";
        select(a,arraysize,ascending);
}
else
{
                cout<<"\n array in descending order is :";
                select(a,arraysize,descending);
}
for(int i=0;i<arraysize;i++)
cout<<"  "<<a[i];
cout<<endl;
return 0;
}
void select(int *const arr,const int size,bool (*compare)(int ,int ))
{
        int smallorlargest;
        for(int i=0;i<size;i++)
        {
                        smallorlargest=i;
                for(int j=i+1;j<size;j++)
                {
                        if(!(*compare)( arr[smallorlargest],arr[j]))
                        {
                                smallorlargest=j;
                        }
                }
                swap( &arr[smallorlargest],&arr[i] );
        }
}
void swap(int *const a,int *const b)
{
        int x=*a;
          *a=*b;
            *b=x;
}
bool ascending(int x,int y)
{
return x < y;
}
bool descending(int p,int q)
{
return p > q;
}

The above code is the correct version of your code. The bold line should be outside the inner for loop and not inside the inner for loop as in your code.



Regards
lali.cpp

ganesha 04-20-2008 07:44 AM

ppppppppppppppppppppppppppp


All times are GMT -5. The time now is 08:02 AM.