中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

幾種常見(jiàn)的排序C實(shí)現(xiàn)

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
#include<stdio.h>
#include<stdlib.h>
//冒泡排序從小到大 第一趟得到最大的存到數(shù)組的最后,第二趟得到數(shù)組的第二大的,存到數(shù)組的倒數(shù)第二位。依次。。
void bubble_sort(int *p){
 int length=6 ;
 for(int i=0;i<length;i++){
  for(int j=0;j<length-i;j++){
   if(p[j]>p[j+1]){
   int t;
   t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
   }
  }
 }
 //打印輸出
 for(int i=0;i<length;i++){
   printf("%d\n",p[i]);
 }


}
//希爾排序
void shell(int * p){
 int length=6;
 //取增量
 int step=length/2;
 while(step>=1){
    //無(wú)序序列
 for(int i=step;i<length;i++){
     int temp=p[i];
 int j;
 //有序序列
 for(j=i-step;j>=0 && temp<p[j];j=j-step){
    
 p[j+step]=p[j];


  }
 p[j+step]=temp;


      
 }
   step=step/2;
 
 }
}
//選擇排序(選擇出最小的假定第一個(gè)為最小的 然后判斷 是否交換)
void selection_sort(int *p){
  int length=6;
  int min,j,i,t;
  for(i=0; i<length;i++){
   for(min=i,j=i+1;j<length;j++){
    if(p[min]>p[j]){
      min=j;
    }
   }
   if(min!=i){
   t=p[i];
   p[i]=p[min];
   p[min]=t;
   }
  
  }
   //打印輸出
 for(int i=0;i<length;i++){
   printf("%d\n",p[i]);
 }


}
//確定第一個(gè)數(shù)組元素的最終位置
int find_pos(int *a,int low,int high){
  int val=a[low];
  while(low<high){
   while(low<high && a[high]>=val)
    --high;
   a[low]=a[high];
   while(low<high && a[low]<=val)
    ++low;
   a[high]=val;
  
  }
   a[low]=val;
   return low;


}
//快速排序  遞歸調(diào)用
void quick_sort(int *a ,int low ,int high){


 int pos;
 if(low<high){
  pos=find_pos(a,low,high);
  quick_sort(a,low,pos-1);
  quick_sort(a,pos+1,high);
 
 }
}


//數(shù)組的兩兩合并操作,歸并操作
void merge(int *p,int *temp,int left,int middle,int right){
   //左指針尾
int leftend=middle-1;
//右指針頭
int  rightstart=middle;
//臨時(shí)數(shù)組的下標(biāo)
int tempindex=left;
//數(shù)組合并后的長(zhǎng)度
int templength=right-left+1;
//先循環(huán)兩個(gè)區(qū)間段都沒(méi)有結(jié)束的情況
while((left<=leftend)&&(rightstart<=right)){
//r如果發(fā)現(xiàn)有序列小的,則將此數(shù)放入臨時(shí)數(shù)組
if(p[left]<p[rightstart]){
temp[tempindex++]=p[left++];
}else{

 temp[tempindex++]=p[rightstart++];

}


}
//判斷左邊序列是否結(jié)束
while(left<=leftend){
 temp[tempindex++]=p[left++];
}
//判斷右邊序列是否結(jié)束
while(rightstart<=right){
  temp[tempindex++]=p[rightstart++];
}
//交換數(shù)據(jù)
for(int i=0; i<templength;i++){
 p[right]=temp[right];
 right--;

}






}
//歸并排序 ---數(shù)組的劃分


void  merge_sort(int *p ,int *temp,int left,int right){


if(left<right){
//取分割位置
int middle=(left+middle)/2;
     //遞歸劃分?jǐn)?shù)組左序列
merge_sort(p,temp,left,middle);
      //遞歸劃分?jǐn)?shù)組右序列
merge_sort(p,temp,middle+1,right);
//數(shù)組的合并操作
merge(p,temp,left,middle+1,right);



}


}




//插入排序
void insert_sort(int *a){
 int len=6;
 int i, j, temp;


 for(i = 1; i < len; i ++)
 {
  temp = a[i];
  for(j = i - 1; j >= 0; j --)
  {
   if(a[j] > temp)
   {
    a[j + 1] = a[j]; 
   }else
   {
    break;
   }
  }
  a[j + 1] = temp;
 }
}
int main(int argc, char* argv[])
{    
 int a;
 int b[]={35,67,23,12,65,99};
 //bubble_sort(b);
 //selection_sort(b);
 //quick_sort(b,0,5);
 shell(b);
 //insert_sort(b);
   for(int i=0;i<6;i++){
  printf("%d\n",b[i]);
  }


scanf("%a",&a);
return 0;
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:PHP下載遠(yuǎn)程文件到本地存儲(chǔ)的代碼

下一篇:一個(gè)用于處理cookie的php類(lèi)