我出一个,很简单的,Selection Algorithm,
该算法的功能是:1。Sorting the list in decreasing order, and then the Kth largest value will be in position K.
2。A related technique to this would be to find the largest value and then move it to the last location
in the list. If we again look for the largest value in the list, ignoring the value we already found,
we get the second largest value, which can be moved to the second last location in the list.
If we con tinue this process, we will find the Kth largest value on the Kth pass.
This gives the algorithm:
#include"iostream"
using namespace std;
void Find_Kth_Largest(int list[],int n,int k){
//list:the value to look through
//n:the size of the list
//k:the element to select
int i,largest,largest_location,j;
for(i=1;i<=k;i++){//for_1
largest=list[1];
largest_location=1;
for(j=2;j<=n-(i-1);j++){//for_2
if(list[j]>largest){
largest=list[j];
largest_location=j;
}//if
}//for_2
swap(list[n-(i-1)],list[largest_location]);//exchanging, making the largest elements
//in the last elements of the list
}//for_1
cout<<"The number is:"<<largest<<"\n";
}//Find_Kth_Largest
void swap(int &a,int &b){//exchange
int temp;
temp=a;
a=b;
b=temp;
}//swap
void main(){
int List[16],K,i;
cout<<"Please input the K:";
cin>>K;
cout<<"\n";
List[0]=0;
for(i=1;i<=15;i++){
List[i]=2*i-1;
List[0]++;//List[0] is stored the length of the List
}//for
cout<<"\n"<<List[0]<<"\n";//output the length
Find_Kth_Largest(List,List[0],K);
}
需要翻译[此贴子已经被zinking于2005-10-21 13:44:26编辑过]