您好,登錄后才能下訂單哦!
算法導(dǎo)論:主要關(guān)注的是程序的性能;速度令人渴望?。?!
排序算法是經(jīng)典算法
1、插入排序
(1)、算法模型
(2)、代碼實現(xiàn)
#include<stdio.h> void 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<i; j++){ ; } if(i != j){ for(n = i; n > 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)、核心代碼實現(xiàn)
#include<stdio.h> #include<malloc.h> 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); //以下的都是<,因為傳過來的是數(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)、完整代碼實現(xiàn)
#include<stdio.h> #include<malloc.h> 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; } //以下都是<=,因為傳過來的都是下標(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)秀;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。