
algorithm
zhaori
Just a programmer
展开
-
merge sort
时间复杂度: O(nlogn) void merge(int A[], int low, int mid, int high) { int len = high-low+1; int* B = (int*)malloc(len*sizeof(int)); if (!B) printf("Failed to allocate memory\n"); int* pB = B; in原创 2012-06-20 13:57:50 · 714 阅读 · 0 评论 -
heap sort
// heap_sort.h // 大堆排序 #ifndef _HEAP_SORT_H_ #define _HEAP_SORT_H_ #include "utility.h" /* 左节点 */ int tree_left(int i) { return 2 * i + 1; } /* 右节点 */ int tree_right(int i) { return (2 * i + 2);原创 2012-06-14 14:55:37 · 884 阅读 · 0 评论 -
quick sort
#include #define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0]) #define CHIRS_CODE void print_arr(int arr[], int len) { int i=0; for (;i<len;i++) printf("%d, ", arr[i]); printf("\n"); } void quick原创 2012-06-10 22:43:12 · 703 阅读 · 0 评论 -
Bubble Sort
冒泡算法, 一种简单的排序算法 复杂度: O(n^2) 伪代码: bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do if A[i-1] > A[i] then原创 2012-06-06 15:40:27 · 927 阅读 · 0 评论 -
selection sort
选择排序算法, 一种简单的排序算法 复杂度: O(n^2) 伪代码: procedure selectSort( A : list of sortable items ) n = length(A) for i=0 to n-1 inclusive do min = i for j = i+1 to n inclusive do if A[j] <原创 2012-06-07 10:39:50 · 1755 阅读 · 0 评论 -
binary search
#include #define print_seek_result(key, index) \ {printf("Found %d at position %d\n", key, index);} #define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0]) #define KEY_NOT_FOUND -1 int binary_search(int原创 2012-06-13 21:35:34 · 603 阅读 · 0 评论