菜,,,很菜,无法直视
#include <iostream>
#include <cstring>
#include<stdio.h>
int* sort(int arry[],int len)
{
printf("\n----------sort enter----------\n");
int arry_tmp;
for(int k=0;k<len;k++)
{
for(int i=0;i<len;i++)
{
if(arry[i]>arry[i+1] && i<len-1)
{
arry_tmp=arry[i+1];
arry[i+1]=arry[i];
arry[i]=arry_tmp;
printf("\narry now:");
for(int j=0;j<len;j++)
{//show sort result now
printf("%d ",arry[j]);
}
}
}
}
printf("\n--------sort end------------\n");
return arry;
}
int* merg2arry(int arr_a[],int len_a,int arr_b[],int len_b)
{//merge 2 sorted arrys
int len=len_a+len_b;
int arry[len]={0};
sort(arr_a,len_a);
printf("\n--------------------\nafter sort:");
{
for(int j=0;j<len_a;j++)
{//show sort result now
printf("%d ",arr_a[j]);
}
}
printf("\n\n\n--------------------\n");
sort(arr_b,len_b);
printf("\n--------------------\nafter sort:");
{
for(int j=0;j<len_b;j++)
{//show sort result now
printf("%d ",arr_b[j]);
}
}
//int len=(len_a>len_b) ? len_a:len_b;
printf("\n\n\n--------------------\n");
int j=0,k=0;
for(int i=0;i<len;i++)
{
for(;j<len_a||k<len_b;)
{
if(arr_a[j]<arr_b[k] && j<len_a && k<len_b)
//if(arr_a[j]<arr_b[k] )
{
arry[i]=arr_a[j];
j++;
}else{
arry[i]=arr_b[k];
k++;
}
printf("%d ",arry[i]);
}
//printf("%d ",arry[i]);
printf("\n");
}
return arry;
}
int main()
{
/*
*int arr1[]={5,1,9,0,4,8};
*int arr2[]={6,12,3,7,4,8,1,23,4,5,3};
*/
int arr1[]={5,1,4,9};
int arr2[]={6,12,3,7,4};
int len1=sizeof(arr1) / sizeof(int);
int len2=sizeof(arr2) / sizeof(int);
merg2arry(arr1,len1,arr2,len2);
/*
* int arr[]={6,12,3,7,4,8,1,23,4,5,3};
* int len=sizeof(arr) / sizeof(int);
* printf("\n--------------------\nbefore sort:");
*
* {
*
* for(int j=0;j<len;j++)
* {//show sort result now
* printf("%d ",arr[j]);
* }
* }
*
*
* sort(arr,len);
* printf("\n--------------------\nafter sort:");
* {
* for(int j=0;j<len;j++)
* {//show sort result now
* printf("%d ",arr[j]);
* }
* }
*/
return 0;
}
结果:
昨天这代码真垃圾,为什么不先合并后排序,更清晰简单。
#include <iostream>
#include <cstring>
#include<stdio.h>
int bublesort(int arry[],int len)
{
printf("\n\n----------sort enter----------\n");
int arry_tmp;
for(int k=0;k<len;k++)
{
for(int i=0;i<len;i++)
{
if(arry[i]>arry[i+1] && i<len-1)
{
arry_tmp=arry[i+1];
arry[i+1]=arry[i];
arry[i]=arry_tmp;
printf("\narry now:");
for(int j=0;j<len;j++)
{//show sort result now
printf("%d ",arry[j]);
}
}
}
}
printf("\n--------sort end------------\n\n");
return 0;
}
int merg2arry(int arr_a[],int len_a,int arr_b[],int len_b,int arry[])
{//merge 2 arrys into arry
int len=len_a+len_b;
int i=0;
printf("\n--------------------merge begin\n");
for(int j=0;j<len_a;j++,i++)
{//merge first arr
arry[i]=arr_a[j];
printf("%d ",arry[i]);
}
for(int k=0;k<len_b ;k++,i++)
{//merge second
arry[i]=arr_b[k];
printf("%d ",arry[i]);
}
printf("\n--------------------merge end\n");
return 0;
}
int main()
{
/*
*int arr1[]={5,1,9,0,4,8};
*int arr2[]={6,12,3,7,4,8,1,23,4,5,3};
*
*/
int arr1[]={5,1,4,9};
int arr2[]={6,12,3,7,4};
int len1=sizeof(arr1) / sizeof(int);
int len2=sizeof(arr2) / sizeof(int);
int arry_merg[len1+len2]={0};//put merged arry
merg2arry(arr1,len1,arr2,len2,arry_merg);
int len=sizeof(arry_merg) / sizeof(int);
printf("\n--------------------before sort:\n");
for(int j=0;j<len;j++)
{//show sort result now
printf("%d ",arry_merg[j]);
}
bublesort(arry_merg,len);
printf("\n--------------------after sort:\n");
for(int j=0;j<len;j++)
{//show sort result now
printf("%d ",arry_merg[j]);
}
return 0;
}
结果:
咦,时间怎么不一样?