LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-06-2008, 06:35 AM   #1
ganesha
LQ Newbie
 
Registered: Aug 2007
Location: INDIA
Distribution: PURE SLACKWARE 12.1
Posts: 28

Rep: Reputation: 15
Post 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;
}
 
Old 04-06-2008, 08:52 AM   #2
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
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
 
Old 04-20-2008, 08:44 AM   #3
ganesha
LQ Newbie
 
Registered: Aug 2007
Location: INDIA
Distribution: PURE SLACKWARE 12.1
Posts: 28

Original Poster
Rep: Reputation: 15
ppppppppppppppppppppppppppp
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java - problems with selection sort gomez6211 Programming 1 09-23-2006 12:54 AM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 09:40 PM
php array sort inconsistent results rblampain Programming 2 04-03-2006 01:34 AM
Hello (sort of...) Gethyn LinuxQuestions.org Member Intro 1 10-15-2004 01:10 PM
sort pantera Programming 5 05-26-2004 08:36 PM


All times are GMT -5. The time now is 11:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration