国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C語言下排序算法詳解

2019-11-08 02:24:44
字體:
來源:轉載
供稿:網友

C語言下排序算法詳解

選擇排序法冒泡排序法交換排序法插入排序法折半(二分排序法)

yuanli xing 東西就不在這里贅述了,直接上代碼先,原理的東西都在代碼上面有注釋: sort.h

#ifndef __SORT_H_#define __SORT_H_//選擇排序算法extern void selectSort(int *num_pointer,const int num);//冒泡排序算法extern void bubbleSort(int *num_pointer,const int num);//冒泡排序算法2extern void bubbleSort2(int *num_pointer,const int num);//交換排序算法extern void swapSort(int *num_pointer,const int num);//插入排序法則extern void insertSort(int *num_pointer,const int num);//折半排序法則extern void celeritySort(int *num_pointer,const int num);#endif

sort.c

#include"sort.h"#include"swap.h"/***選擇排序法則:9,6,8,7,3*算法規則:每一次將數組中的最大的那個數值篩選出來,將這個值提取出來,與最前面沒有進行排序的數組元素進行比較*那前面沒有比較的數值的次數就是整個數組長度-1次,這也是外層循環的次數*內層循環也就是從沒有比較數組的后面一個數值開始,假設當前沒有比較的元素的位置是i的話,那么開始項的位置就是i+1**注意:選擇排序法只是將最大的數值的位置記錄下來,最后再進行一次互換,所以默認有一個記錄的下標;用來記錄位置,即pos*然后用后續的元素與這個最大的數值進行比較**選擇排序在整個排序過程中進行了N*(N-1)/2次排序,適用于數組數量比較小的數組*/extern void selectSort(int *num_pointer,const int num){ int pos; int i_count = 0; int j_count = 0; int *pointer = num_pointer; for( i_count = 0; i_count < num-1; i_count++) { pos = i_count; for(j_count = i_count + 1;j_count < num; j_count++) { if(*(pointer+pos) < *(pointer+j_count)){ pos = j_count; } } if(pos != i_count){ swap(pointer+i_count,pointer+pos); } } PRintf_array(num_pointer,num);}/***冒泡排序法則:*算法規則:冒泡排序法則是在排序的時候,每一次都是去比較相鄰的兩個數組的數值a[j]與a[j+1]比較,如果需要swap就會立馬進行swap,從而將最大的數值篩選出來,然后后一次的比較從前一個最大值的位置下一位元素開始,所以外層的循環為(數組長度-1)次*內層循環次數:如果是從數組的0位置開始比較的話,那么循環的內層比較次數為(數組長度-i-1)*如果是從最后一個位置開始來進行比較的話,內層的比較條件為(j=數組長度-1,j>=i;j--);**注意:不同的方向進行比較的時候,其判別條件也是不一樣的 **冒泡排序最好的是正序,只要一次,最差的是逆序,需要n×n次,冒泡排序相對是比較穩定的一種排序法,如果數組稍微有序,則效果是比較好的*/extern void bubbleSort(int *num_pointer,const int num){ int i_count = 0; int j_count = 0; int *pointer = num_pointer; for(i_count = 0;i_count<num;i_count++){ for(j_count = 0;j_count<num-i_count-1;j_count++){ if(*(pointer+j_count) < *(pointer+j_count+1)){ swap(pointer+j_count,pointer+j_count+1); } } } printf_array(num_pointer,num);}extern void bubbleSort2(int *num_pointer,const int num){ int i_count = 0; int j_count = 0; int *pointer = num_pointer; for(i_count = 1;i_count < num ;i_count++){ for(j_count = num -1 ;j_count >= i_count;j_count--){ if(*(pointer+j_count) > *(pointer+j_count-1)){ swap(pointer+j_count,pointer+j_count-1); } } } printf_array(num_pointer,num);}/***交換排序法則:每一次比較都會進行一次互換,比較的前一個數值從0,到num-1*比較的后值從1,num;(從前數值的后一項開始的,即從i_count+1開始);**注意:在每一次比較的時候都是會進行交換的**與冒泡排序是一樣的,正序是最快的,而倒序是最慢的,所以數組稍微有序的時候,效果是比較好的*/extern void swapSort(int *num_pointer,const int num){ int i_count; int j_count; int *pointer = num_pointer; for(i_count = 0; i_count < num-1;i_count++){ for(j_count=i_count + 1;j_count < num ;j_count++){ if(*(pointer+i_count) < *(pointer+j_count)){ swap(pointer+i_count,pointer+j_count); } } } printf_array(num_pointer,num);}/***插入排序法則**插入排序法則相對是比較復雜,其根本工作原理就是抽出一個數據,在前面的數據中尋找相應的位置,然后進行插入,然后繼續下一個數據,一直到數據完成排序*1:首先取出第一數值*2:取出第二個數值,如果第二個數值大于第一個數值,就放在第一個的后面。如果小于就放前面*3:取出第三個數值,與最后一個數值進行比較,如果大于就放后面。如果小于,就與前面一個數值進行比較,再判斷放在前面還是后面*4:以此類推,總是先與最后一個數值進行比較**優點:如果數組有序那么會具有較快的運算速度 */extern void insertSort(int *num_pointer,const int num){ int i_count = 0; int *pointer = num_pointer; int temp; int pos; //從0開始意思是第一次取出一個數值,從1開始就是默認拿出了第一個數值 for(i_count = 1;i_count<num;i_count++){ temp = *(pointer+i_count);//取出的數值 pos = i_count-1;//最后一個數值的位置 //當當前元素的前面位置>0并且當前位置的數值小于前面的數值的時候(取出的數值小于最后一個數值) while((pos>=0) && (temp < *(pointer+pos))){ swap(pointer+1+pos,pointer+pos); pos--; } *(pointer+pos+1) = temp; } printf_array(num_pointer,num);} /***折半排序法則*折半排序算法通常也叫做快速排序算法,是選擇中間的一個數值middle,然后把中間數值小的放在左邊,比中間數值大的數據放在右邊,然后對兩邊分別進行遞歸的方法進行重新調用;*1:先取出中間的數值*2:左邊去與中間middle進行比較,如果左邊的數值大于middle,篩選出來*3:右邊的數值與中間middle進行比較,如果右邊的小于middle,篩選出來,將左右兩邊的這兩個數值進行互換,然后再將pos各往下一個元素提一位,再次進行比較*這樣可以在第一次遍歷完了之后,將以中間middle為基準的數值,進行左右分布,*4:然后再次將左邊的一組進行單獨二分排序,將右邊的一組進行二分排序*/extern void celeritySort(int * num_pointer,const int left,const int right){ int *pointer = num_pointer; int i_count = left; int j_count = right; int middle = *(pointer+(left+right)/2);//拿到最中間的數的數值 do{ while((*(pointer+i_count) < middle) && (i_count<right)){ i_count++; } while((*(pointer+j_count) > middle) && (j_count>left)){ j_count--; } if(i_count <= j_count){ swap(pointer+i_count,pointer+j_count); i_count++; j_count--; } }while(i_count<=j_count); //遞歸左邊 if(left<j_count){ celeritySort(num_pointer,left,j_count); } //遞歸右邊 if(i_count<right){ celeritySort(num_pointer,i_count,right); } printf_array(num_pointer,right+1);}

swap.h

#ifndef __SWAP_H__#define __SWAP_H_extern void swap(int *,int *);extern void printf_array(int *,int);#endif

swap.c

#include"swap.h"#include<stdio.h>extern void swap(int *num_pointer1,int *num_pointer2){ int temp; temp = *num_pointer1; *num_pointer1 = *num_pointer2; *num_pointer2 = temp;}extern void printf_array(int * pointer,int num){ int count = 0; for(count;count<num;count++){ printf("position:[%d]:value:%d/n",count,*(pointer+count)); }}

ceshi daima : sort_algorithm.c

#include<stdio.h>#include<stdio.h>#include"sort.h"#include"swap.h"int main(int argc,char *argv[]){ int select = 0; int array_length; int num = 0; printf("==============/n"); printf("1:selectSort/n"); printf("2:bubbleSort/n"); printf("3:bubbleSort2/n"); printf("4:swapSort/n"); printf("5:insertSort/n"); printf("6:celeritySort/n"); printf("==============/n"); scanf("%d",&select); printf("please input the array length you want!!/n"); scanf("%d",&array_length); int array_nums[array_length]; for(num ;num<array_length;num++){ printf("please input the array element/n"); scanf("%d",&array_nums[num]); } printf_array(array_nums,array_length); printf("---------------------------/n"); if(select == 1){ selectSort(array_nums,array_length); }else if(select == 2){ bubbleSort(array_nums,array_length); }else if(select == 4){ swapSort(array_nums,array_length); }else if(select == 3){ bubbleSort2(array_nums,array_length); }else if(select == 5){ insertSort(array_nums,array_length); }else if(select == 6){ celeritySort(array_nums,0,array_length-1); }else{ printf("select error/n"); } return 0;}

最后謝謝大家的訪問:歡迎大家持續訪問,有錯誤的地方希望各位看客能夠及時指出,謝謝


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西和县| 彝良县| 蚌埠市| 涿鹿县| 铁力市| 郑州市| 广宁县| 类乌齐县| 鹤壁市| 邓州市| 临漳县| 天气| 兴化市| 永嘉县| 射阳县| 长寿区| 汝南县| 宁明县| 南川市| 昌平区| 本溪市| 绵竹市| 邯郸县| 瑞昌市| 防城港市| 囊谦县| 青神县| 简阳市| 南皮县| 喀喇| 长春市| 灌云县| 临江市| 航空| 阳泉市| 罗定市| 西乡县| 调兵山市| 铅山县| 玛曲县| 桓仁|