1.冒泡排序
void sort(int list[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = i; j < n; j++) { if (list[i] < list[j]) { temp = list[i]; list[i] = list[j]; list[j] = temp; } } } }
2.二分查找
int binsearch(int *list, int search, int num) { int middle; int left = 0; int right = num - 1; while (left <= right) { middle = (left + right) / 2; if (list[middle] > search) { right = middle - 1; } else if (list[middle] == search) { return middle; } else { left = middle + 1; } } return -1; }
递归版本
int binsearch(int *list, int search, int left, int right) { int middle; if (left <= right) { middle = (left + right) / 2; if (list[middle] > search) { return binsearch(list, search, left, middle - 1); } else if (list[middle] == search) { return middle; } else { return binsearch(list, search, middle + 1, right); } } return -1; }
3.
时间: 2024-09-17 04:30:03