算法導(dǎo)論:主要關(guān)注的是程序的性能;速度令人渴望?。?!
成都創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、山西網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為山西等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
排序算法是經(jīng)典算法
1、插入排序
(1)、算法模型
(2)、代碼實(shí)現(xiàn)
#includevoid insertSort(int *a, int count); void showArray(int *a, int count); void showArray(int *a, int count){ int i; for(i = 0; i < count; i++){ printf("%d ", a[i]); } printf("\n"); } void insertSort(int *a, int count){ int i; int j; int n; int tmp; for(i = 1; i < count; i++){ tmp = a[i]; for(j = 0; a[i]>a[j] && j j; n--){ a[n] = a[n-1]; } a[j] = tmp; } } } void main(void){ int a[] = {2, 5, 7, 1, 11, 0, 6, 9}; int count = sizeof(a)/sizeof(int); printf("排序前輸出如下: "); showArray(a, count); insertSort(a, count); printf("排序后輸出如下: "); showArray(a, count); }
(3)、結(jié)果截圖
(4)、算法分析
插入排序最壞的情況:數(shù)組中所有元素全部逆序排列;
時間復(fù)雜度:O(n^2);
2、歸并排序
(1)、算法思想:
i、if n = 1; done
ii、遞歸排序,分2部分,在[0, n/2]和[n/2, n]
iii、將2部分歸并排序
(2)、核心代碼實(shí)現(xiàn)
#include#include void mergerSort(int *a1, int *a2, int **a3, int count1, int count2, int *count3); void showArray(int *a3, int count); void showArray(int *a3, int count){ int i; for(i = 0; i < count; i++){ printf("%d ", a3[i]); } printf("\n"); } void mergerSort(int *a1, int *a2, int **a3, int count1, int count2, int *count3){ int count; int i = 0; int j = 0; int n = 0; count = *count3 = count1 + count2; *a3 = (int *)malloc(sizeof(int) * count); //以下的都是<,因?yàn)閭鬟^來的是數(shù)組長度; while(i < count1 && j < count2){ if(a1[i] < a2[j]){ (*a3)[n++] = a1[i]; i++; }else if(a1[i] == a2[j]){ (*a3)[n++] = a1[i]; (*a3)[n++] = a2[j]; i++; j++; }else{ //剛才寫程序else(a1[i] > a2[j]),后發(fā)現(xiàn)else語句后面是沒有條件的!!! (*a3)[n++] = a2[j]; j++; } } while(i < count1){ (*a3)[n++] = a1[i]; i++; } while(j < count2){ (*a3)[n++] = a2[j]; j++; } } /* 歸并排序核心算法就是:將已經(jīng)排好序的2個數(shù)組進(jìn)行最終的排序過程; */ void main(void){ int a1[] = {1, 3, 5, 7}; int a2[] = {0, 2, 4, 6, 8, 9, 10}; int count1 = sizeof(a1)/sizeof(int); int count2 = sizeof(a2)/sizeof(int); int *a3 = NULL; int count3 = 0; mergerSort(a1, a2, &a3, count1, count2, &count3); showArray(a3, count3); free(a3); }
(3)、結(jié)果截圖
(4)、完整代碼實(shí)現(xiàn)
#include#include void mergeSort(int *a, int low, int high); void merge(int *a, int low, int mid, int high); void merge(int *a, int low, int mid, int high){ int i = low; int j = mid+1; int n = 0; int *a2; a2 = (int *)malloc(sizeof(int) * (high-low+1)); if(a2 == NULL){ return; } //以下都是<=,因?yàn)閭鬟^來的都是下標(biāo); while(i <= mid && j <= high){ if(a[i] < a[j]){ a2[n++] = a[i]; i++; }else if(a[i] == a[j]){ a2[n++] = a[i]; i++; j++; }else{ a2[n++] = a[j]; j++; } } while(i <= mid){ a2[n++] = a[i]; i++; } while(j <= high){ a2[n++] = a[j]; j++; } for(n = 0, i = low; i <= high; n++, i++){ //將a2中的元素復(fù)制回a中; a[i] = a2[n]; } free(a2); } void mergeSort(int *a, int low, int high){ int mid; if(low < high){ mid = (low + high) / 2; mergeSort(a, low, mid); mergeSort(a, mid+1, high); merge(a, low, mid, high); } } void main(void){ int a[] = {2, 4, 1, 7, 5, 6, 9, 10}; int count = sizeof(a)/sizeof(int); int i; mergeSort(a, 0, count-1); for(i = 0; i < count; i++){ printf("%d ", a[i]); } printf("\n"); }
(5)、結(jié)果截圖
(6)、算法分析
歸并排序的時間復(fù)雜度:樹高度log(n),一共要對n個元素進(jìn)行排序,所以為:O(nlogn);
在30個元素以內(nèi),插入排序性能更好,超過30個元素之后歸并排序的性能更加優(yōu)秀;