题目放前面好了
官方伪代码:
CLRS2.3.5
int iteative_binary_search(int Array[], int v,int length) { int left = Array[0]; int right = Array[length-1]; int i = 0; int mid = (length / 2); while (left <= right&&i<length) { i++; if (v == Array[mid]) return mid; else if (v > Array[mid]) left = Array[mid + 1]; else right = Array[mid - 1]; } } int binary_search(int A[], int key, int p, int r) { int left = p; int right = r; while (left <= right) { int middle = left + ((right - left) >> 1); if (A[middle] > key) right = middle - 1; else if (A[middle] < key) left = middle + 1; else return middle; } return -1; }
CLRS 2.3.6
int Search_Insertion(int A[],int p,int r,int v){ int mid; if (p <= r){ mid = ((p + r) / 2); if (A[mid] == v){ return mid; } else if (A[mid] > v){ return Search_Insertion(A, p, mid - 1, v); } else return Search_Insertion(A, mid + 1, r, v); } return p; } void Insertion_Sort(int A[]){ int key,i; for (int j = 0; j < 10;j++){ key = A[j]; i = Search_Insertion(A, 1, j - 1, key); while (j>i) { A[j] = A[j - 1]; j = j - 1; A[i] = key; } } }
CSDN插入图片的玩意真操蛋。。。
放着了
时间: 2024-11-05 23:13:44