- 论坛徽章:
- 0
|
input:
a 142 3 2 -12
b 31 4 2 1
ouput:
a 142 2 1 -12
b 31 4 3 2
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <assert.h>
using namespace std;
void swapdata(int& x, int &y){
int temp;
temp=x;
x=y;
y=temp;
return;
}
void Display(int data[],int begin,int end){
if(data==NULL) return;
for(int index=begin;index<=end;index++){
cout<<data[index]<<" ";
}
cout<<endl;
}
//data1 and data2 are sorted descend
bool select(int total[],int array1[],int array2[],int len){
assert(total !=NULL &&array1 !=NULL && array2 != NULL && len >0 );
int sum1,sum2,index1,index2;
sum1=sum2=index1=index2=0;
while(index1<len && index2<len) {
if(sum1>sum2) {
sum2+=*total;
array2[index2++] = *total++;
}
else {
sum1+=*total;
array1[index1++] = *total++;
}
}
while(index1<len)
array1[index1++] = *total++;
while(index2<len)
array2[index2++] = *total++;
return true;
}
int* mergesort(int data1[],int data2[],int length1,int length2) {
assert(data1 != NULL && data2 != NULL);
assert(length1 >=0 && length2 >=0 );
int index=0,len1=0,len2=0;
int *data3=new int[length1+length2];
while( len1<length1 && len2<length2 ){
data3[index++]= data1[len1]>data2[len2]? data1[len1++]:data2[len2++];
}
while(len1<length1)
data3[index++]=data1[len1++];
while(len2<length2)
data3[index++]=data2[len2++];
cout<<"merge sorted ->"<<endl;
Display(data3,0,length1+length2-1);
return data3;
}
/*
*
*/
bool myqsort2(int data[],int begin,int end){
bool result=true;
if( data==NULL || end < 0 || begin < 0 || begin > end)
return false;
if(begin==end)
return result;
int compare_point,i,j,pilotkey;
srand((unsigned int)time(NULL));
compare_point=begin + rand()%(end-begin+1);
pilotkey=data[compare_point];
swapdata(data[compare_point],data[end]);
i=begin;
j=end;
while(i<j) {
while(data[i] >= pilotkey && i<j)
i++;
if(i<j)
swapdata(data[i],data[j--]);
while(data[j] <= pilotkey && i<j)
j--;
if(i<j)
swapdata(data[i++],data[j]);
}
myqsort2(data,begin,i-1);
myqsort2(data,i+1,end);
}
//descending sort
bool myqsort(int* data,int begin,int end){
bool result=true;
int compare_point,midvalue,pivot,index;
int tempdata;
if( data==NULL || end < 0 || begin < 0 || begin > end)
return false;
if(begin==end)
return result;
//generate a random number between begin and end
srand((unsigned int)time(NULL));
compare_point=begin + rand()%(end-begin+1);
midvalue=data[compare_point]; //use the middle point
//exchange pivot value into the last element of array
swapdata(data[compare_point],data[end]);
index=begin;
pivot=begin-1;
while(index <= end){
if(data[index] > midvalue){
if((++pivot) != index){
swapdata(data[pivot],data[index]);
}
}
index++;
}
//exchange pivot value into the last element of array
swapdata(data[++pivot],data[end]);
myqsort(data,begin,pivot-1);
myqsort(data,pivot+1,end);
return result;
}
int main(int argc, char** argv) {
int data1[]={142,3,2,-12};
int data2[]={31,4,2,1};
int *data3=NULL;
cout<<endl<<"before selection, data list -> "<<endl;
Display(data1,0,sizeof(data1)/sizeof(int)-1);
Display(data2,0,sizeof(data2)/sizeof(int)-1);
myqsort(data1,0,sizeof(data1)/sizeof(int)-1);
myqsort(data2,0,sizeof(data2)/sizeof(int)-1);
data3=mergesort(data1,data2,4,4);
// cout<<endl<<"after merge sorting"<<endl;
// Display(data3,0,7);
select(data3,data1,data2,4);
cout<<endl<<"after selection,data list -> "<<endl;
Display(data1,0,sizeof(data1)/sizeof(int)-1);
Display(data2,0,sizeof(data2)/sizeof(int)-1);
delete []data3;
return 0;
}
|
|