折半插入排序:
/*********************************************** 折半插入排序 ***********************************************/ #include<stdio.h> #include<string.h> #include<math.h> #include<ctype.h> #include<stdbool.h> #include<stdlib.h> void BinaryInsertSort(int* a, int n) { int i, j, k, low, high, m; for(i = 1; i < n; i++) { low = 0; high = i - 1; while(low <= high) { m = (low + high) / 2; if(a[m] > a[i]) { high = m - 1; } else { low = m + 1; } } if(j != i - 1) { int temp = a[i]; for(k = i - 1; k >= high + 1; k--) { a[k + 1] = a[k]; } a[k + 1] = temp; } } } void printArray(int* a, int n) { for(int i = 0; i < n; i++) { printf("%d ", a[i]); } printf(" "); } int main() { int a[7] = {5, 2, 1, 8, 10, 23, 22}; BinaryInsertSort(a, 7); printArray(a, 7); return 0; }
折半查找算法:
/*********************************** 折半查找算法 ***********************************/ #include<stdio.h> #include<string.h> #include<math.h> #include<ctype.h> #include<stdbool.h> #define COMPARE(x,y) (((x) < (y)) ? -1 : ((x) == (y)) ? 0 : 1) /* 非递归代码 */ int binsearch(int list[], int searchchnumm, int left, int right) { int middle; while(left <= right) { middle = (right + left) / 2; switch(COMPARE(list[middle], searchchnumm)) { case -1: left = middle + 1; break; case 0: return middle; break; case 1: right = middle - 1; break; default: break; } } return -1; } /* 递归代码 */ int binsearch(int list[], int searchchnumm, int left, int right) { int middle; if(left <= right) { middle = (left + right) / 2; switch(COMPARE(list[middle], searchchnumm)) { case -1: return binsearch(list, searchchnumm, middle + 1, right); break; case 0: return middle; break; case 1: return binsearch(list, searchchnumm, left, middle - 1); break; default: break; } } return -1; } int main() { int list[] = {2, 4, 5, 1, -3, 6, 8, 10, 55, 23}; int searchnum = 5; int length; length = sizeof(list) / sizeof(int); printf("%d ", length); printf("%d ", binsearch(list, searchnum, 0, length)); return 0; }
冒泡算法:
/*********************************************** 冒泡排序 ***********************************************/ #include <stdio.h> void bubbleSort(int arr[], int count) { int i = count, j; int temp; while(i > 0) { for(j = 0; j < i - 1; j++) { if(arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } i--; } } int main() { //测试数据 int arr[] = {5, 4, 1, 3, 6}; //冒泡排序 bubbleSort(arr, 5); //打印排序结果 int i; for(i = 0; i < 5; i++) { printf("%4d", arr[i]); } }
快排:
/*************************************** 快排 ***************************************/ #include<stdio.h> #include<string.h> #include<math.h> #include<ctype.h> #include<stdbool.h> void swap(int* a, int* b) //交换两元素的值 { int t; t = *a; *a = *b; *b = t; } void printArray(int a[], int count) //打印数组元素 { int i; for(i = 0; i < count; i++) { printf("%d ", a[i]); } printf(" "); } void quick_sort(int a[], int left, int right) { int i = left + 1, j = right; int key = a[left]; if(left >= right) { return; } /* 从i++和j--两个方向搜索不满足条件的值并交换 * * 条件为:i++方向小于key,j--方向大于key */ while(1) { while(a[j] > key) { j--; } while(a[i] < key && i < j) { i++; } if(i >= j) { break; } swap(&a[i], &a[j]); if(a[i] == key) { j--; } else { i++; } } /* 关键数据放到‘中间’ */ swap(&a[left], &a[j]); if(left < i - 1) { quick_sort(a, left, i - 1); } if(j + 1 < right) { quick_sort(a, j + 1 , right); } } int main(void) { int a[] = {3, 5, 4, 6, 9, 7, 8, 0, 1}; int n = sizeof(a) / sizeof(*a); printArray(a, n); quick_sort(a, 0, n); printArray(a, n); return 0; }