演算法導論C語言實現: 演算法基礎
2.1 插入排序
#include <stdio.h>
#include <stdlib.h>
#define TRACE_SUBSTEPS 1
void func2_1() {
int A[] = {5, 2, 4, 6, 1, 3, 10, 3, 5};
int key = 0;
int j = 0, i = 0, k = 0;
//============print sub title===============
printf("2.1 Insertion-Sort\n\n");
//============print old Array===============
printf("Old Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
if (sizeof(A)/sizeof(int) <= 1) {
goto l_done;
}
//==================INSERTION-SORT==============
for (j = 1; j < sizeof(A)/sizeof(int); j++) {
key = A[j];
i = j - 1;
while(i >= 0 && A[i] > key) {
A[i+1] = A[i];
i = i - 1;
}
A[i + 1] = key;
#if TRACE_SUBSTEPS
printf("\tj=%d, and result:\n\t\tA[] = ", j);
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
#endif //TRACE_SUBSTEPS
}
l_done:
//============print sorted ARRAY=================
printf("Sorted Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
}
注意: 虛擬碼裡陣列下標是從1開始數的。
執行結果:
================第2章 演算法基礎==============
2.1 Insertion-Sort
Old Array: A[] = {5, 2, 4, 6, 1, 3, 10, 3, 5}
j=1, and result:
A[] = {2, 5, 4, 6, 1, 3, 10, 3, 5}
j=2, and result:
A[] = {2, 4, 5, 6, 1, 3, 10, 3, 5}
j=3, and result:
A[] = {2, 4, 5, 6, 1, 3, 10, 3, 5}
j=4, and result:
A[] = {1, 2, 4, 5, 6, 3, 10, 3, 5}
j=5, and result:
A[] = {1, 2, 3, 4, 5, 6, 10, 3, 5}
j=6, and result:
A[] = {1, 2, 3, 4, 5, 6, 10, 3, 5}
j=7, and result:
A[] = {1, 2, 3, 3, 4, 5, 6, 10, 5}
j=8, and result:
A[] = {1, 2, 3, 3, 4, 5, 5, 6, 10}
Sorted Array: A[] = {1, 2, 3, 3, 4, 5, 5, 6, 10}
請按任意鍵繼續. . .
習題:2.1-2
#include <stdio.h>
#include <stdlib.h>
#define TRACE_SUBSTEPS 1
void func2_1_1() {
int A[] = {5, 2, 4, 6, 1, 3, 10, 3, 5};
int key = 0;
int j = 0, i = 0, k = 0;
//============print sub title===============
printf("2.1-1 Insertion-Sort\n\n");
//============print old Array===============
printf("Old Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
if (sizeof(A)/sizeof(int) <= 1) {
goto l_done;
}
//==================INSERTION-SORT==============
for (j = (sizeof(A)/sizeof(int) - 2); j >= 0; j--) {
key = A[j];
i = j + 1;
while(i< (sizeof(A)/sizeof(int)) && A[i] > key) {
A[i-1] = A[i];
i++;
}
A[i-1] = key;
#if TRACE_SUBSTEPS
printf("\tj=%d, and result:\n\t\tA[] = ", j);
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
#endif //TRACE_SUBSTEPS
}
l_done:
//============print sorted ARRAY=================
printf("Sorted Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
}
執行結果:
================第2章 演算法基礎==============
2.1-1 Insertion-Sort
Old Array: A[] = {5, 2, 4, 6, 1, 3, 10, 3, 5}
j=7, and result:
A[] = {5, 2, 4, 6, 1, 3, 10, 5, 3}
j=6, and result:
A[] = {5, 2, 4, 6, 1, 3, 10, 5, 3}
j=5, and result:
A[] = {5, 2, 4, 6, 1, 10, 5, 3, 3}
j=4, and result:
A[] = {5, 2, 4, 6, 10, 5, 3, 3, 1}
j=3, and result:
A[] = {5, 2, 4, 10, 6, 5, 3, 3, 1}
j=2, and result:
A[] = {5, 2, 10, 6, 5, 4, 3, 3, 1}
j=1, and result:
A[] = {5, 10, 6, 5, 4, 3, 3, 2, 1}
j=0, and result:
A[] = {10, 6, 5, 5, 4, 3, 3, 2, 1}
Sorted Array: A[] = {10, 6, 5, 5, 4, 3, 3, 2, 1}
請按任意鍵繼續. . .
2.2-2 選擇排序
#include <stdio.h>
#include <stdlib.h>
#define TRACE_SUBSTEPS 1
void func2_2_2() {
int A[] = {5, 2, 4, 6, 1, 3, 10, 3, 11};
int j = 0, i = 0, z = 0, k = 0;
int tmp = 0;
//============print sub title===============
printf("2.2-2 Selection-Sort\n\n");
//============print old Array===============
//FIXME: A has 1 member.
printf("Old Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
if (sizeof(A)/sizeof(int) <= 1) {
goto l_done;
}
//=============SELECTION-SHORT=================
for (j = 0; j < (sizeof(A)/sizeof(int) - 1); ++j) {
i = j + 1;
z = j;
for(; i < (sizeof(A)/sizeof(int)); ++i) {
if (A[z] > A[i]) {
z = i;
}
}
if ( k != j) {
tmp = A[j];
A[j] = A[z];
A[z] = tmp;
}
#if TRACE_SUBSTEPS
//FIXME: A has 1 member.
printf("\tj=%d, and result:\n\t\tA[] = ", j);
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
#endif //TRACE_SUBSTEPS
}
l_done:
//============print sorted ARRAY=================
//FIXME: A has 1 member.
printf("Sorted Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
}
執行結果:
================第2章 演算法基礎==============
2.2-2 Selection-Sort
Old Array: A[] = {5, 2, 4, 6, 1, 3, 10, 3, 11}
j=0, and result:
A[] = {1, 2, 4, 6, 5, 3, 10, 3, 11}
j=1, and result:
A[] = {1, 2, 4, 6, 5, 3, 10, 3, 11}
j=2, and result:
A[] = {1, 2, 3, 6, 5, 4, 10, 3, 11}
j=3, and result:
A[] = {1, 2, 3, 3, 5, 4, 10, 6, 11}
j=4, and result:
A[] = {1, 2, 3, 3, 4, 5, 10, 6, 11}
j=5, and result:
A[] = {1, 2, 3, 3, 4, 5, 10, 6, 11}
j=6, and result:
A[] = {1, 2, 3, 3, 4, 5, 6, 10, 11}
j=7, and result:
A[] = {1, 2, 3, 3, 4, 5, 6, 10, 11}
Sorted Array: A[] = {1, 2, 3, 3, 4, 5, 6, 10, 11}
請按任意鍵繼續. . .
2.3 歸併排序(分治法)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define TRACE_SUBSTEPS 1
#define MAX_VALUE INT_MAX
static void merge(int *A, int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int *L = (int *)malloc((n1 + 1) * sizeof(int));
int *R = (int *)malloc((n2 + 1) * sizeof(int));
int i = 0, j = 0, k = 0;
if (L == NULL || R == NULL) {
printf("Error: Out of memery!\n");
goto l_ret;
}
//Here, don't use memcpy function.
for (i = 0; i < n1; i++) {
L[i] = A[p + i];
}
for (j = 0; j < n2; j++) {
R[j] = A[q + 1 + j];
}
L[n1] = MAX_VALUE;
R[n2] = MAX_VALUE;
i = 0; j = 0;
for ( k = p; k <= r; k++) {
if (L[i] < R[j]) {
A[k] = L[i];
i++;
} else {
A[k] = R[j];
j++;
}
}
l_ret:
if (L)
free(L);
if (R)
free(R);
}
static void merge_sort(int *A, int p, int r) {
int q = 0;
if (p < r) {
q = (p + r)/2;
merge_sort(A, p, q);
merge_sort(A, q + 1, r);
merge(A, p, q, r);
}
}
void func2_3() {
int A[] = {5, 2, 4, 6, 1, 3, 10, 3, 11};
int k = 0;
//============print sub title===============
printf("2.3 Divide&Conquer-Sort\n\n");
//============print old Array===============
//FIXME: A has 1 member.
printf("Old Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
merge_sort(A, 0, sizeof(A)/sizeof(int) - 1);
//============print sorted ARRAY=================
//FIXME: A has 1 member.
printf("Sorted Array: A[] = ");
for (k = 0; k < sizeof(A)/sizeof(int); k++) {
if (k == 0) {
printf("{%d,", A[k]);
} else if ( k == (sizeof(A)/sizeof(int) - 1)) {
printf(" %d}\n", A[k]);
} else {
printf(" %d,", A[k]);
}
}
}
執行結果:
================第2章 演算法基礎==============
2.3 Divide&Conquer-Sort
Old Array: A[] = {5, 2, 4, 6, 1, 3, 10, 3, 11}
Sorted Array: A[] = {1, 2, 3, 3, 4, 5, 6, 10, 11}
請按任意鍵繼續. . .
相關文章
- 排序演算法-C語言實現排序演算法C語言
- C語言實現A*演算法薦C語言演算法
- 快速排序演算法(C語言實現)排序演算法C語言
- PID演算法的C語言實現演算法C語言
- C語言實現九大排序演算法C語言排序演算法
- ORACLE 數字演算法的C語言實現Oracle演算法C語言
- 經典排序演算法的 C語言 | Java 實現排序演算法C語言Java
- 位反轉的最佳演算法(C語言實現)演算法C語言
- 演算法導論學習之三:排序之C語言實現:選擇排序,插入排序,歸併排序演算法排序C語言
- C語言基礎C語言
- 【作業系統】銀行家演算法實現(C語言)作業系統演算法C語言
- Perceptron演算法—C語言演算法C語言
- 基於落點打分的井字棋智慧下棋演算法(C語言實現)演算法C語言
- FP-Growth演算法全解析:理論基礎與實戰指導演算法
- 10個經典C語言演算法—零基礎小白必學C語言演算法
- 近100個C語言基礎演算法案例(初級篇)C語言演算法
- 10個經典的C語言面試基礎演算法及程式碼C語言面試演算法
- 【01】C語言基礎C語言
- c語言的基礎C語言
- 資料結構與演算法分析(c 語言描述)基數排序 陣列實現資料結構演算法排序陣列
- C語言基礎-C簡介C語言
- dijkstra演算法筆記(C語言實現,顯示路徑)演算法筆記C語言
- 《演算法導論》演算法
- c語言基礎知識C語言
- C語言入門基礎C語言
- C語言基礎函式C語言函式
- c語言基礎學習C語言
- C語言基礎-指標C語言指標
- 【分享】一些經典的C/C++語言基礎演算法及程式碼(三)C++演算法
- 圖論演算法遍歷基礎圖論演算法
- 【作業系統】頁面置換演算法(最佳置換演算法)(C語言實現)作業系統演算法C語言
- 演算法基礎思維導圖演算法
- aan 演算法 c 語言示例 (轉)演算法
- 最近最久未使用(LRU)頁面置換演算法 C語言實現演算法C語言
- 教你一步一步用c語言實現sift演算法C語言演算法
- C語言實現有向無環圖的拓撲排序演算法C語言排序演算法
- 共識演算法PoS及Go語言實現演算法Go
- 基數排序-單連結串列實現【資料結構與演算法分析(c 語言描述)】排序資料結構演算法