我正在学习编程,并且已经创建了一个 void merge()
程序。我尝试添加二进制搜索方法和插入方法。该程序应按升序输出数组 A
和 B
的组合元素列表。 A
的元素应该放在B
的元素中。输出错误。
这是我的输出:
3 16
Inserting 512 into b at -1
2 0
61 154 170 275 426 509 612 653 677 703 765 897 908 512 503 512
这是我的代码:
#include<iostream>
#include<cmath>
using namespace std;
int binsearch(int array[], int first, int last, int search_key)
{
int index;
if (first > last)
index = -1;
else
{
int mid = (first + last) / 2;
if (search_key == array[mid])
index = mid;
else
if (search_key < array[mid])
index = binsearch(array, first, mid - 1, search_key);
else
index = binsearch(array, mid + 1, last, search_key);
} // end if
return index;
}
void insert(int A[], int B[], int n, int m, int i){
int j = n - m - 1, k = n - 1;
while (i >= 0 && j >= 0){
if (A[i] > B[j]){
B[k--] = A[i--];
}
else {
B[k--] = B[j--];
}
}
if (j<0){
while (i >= 0){
B[k--] = A[i--];
}
}
}
void merge(int a[], int b[], int m, int n) {
int a_size = m;
int b_size = n;
while (n != 0 && m != 0) {
printf("%d %d\n", m, n);
if (!(m > n)) {
int t = log(n / m);
int i = n + 1 - pow(2, t);
if (a[m - 1] < b[i - 1]) {
printf("Decreasing n\n");
n = n - pow(2, t);
}
else {
int k = binsearch(b, i - 1, n, a[m - 1]) + 1;
printf("Inserting %d into b at %d\n", a[m - 1], k - 1);
insert(a, b, b_size, k-3, m-1);
b_size++;
m = m - 1;
n = k;
}
}
else /* m > n */ {
int t = log(n / m);
int i = m + 1 - pow(2, t);
if (b[n - 1] < a[i - 1]) {
printf("Decreasing m\n");
m = m - pow(2, t);
}
else {
/*int k = binsearch(i - 1, m, b[n - 1], a) + 1;*/
int k = binsearch(a, i - 1, m, b[n - 1]) + 1;
printf("Inserting %d into a at %d\n", b[n - 1], k - 1);
insert(b, a, a_size, k-3, n-1);
a_size++;
n = n - 1;
m = k;
}
}
}
printf("%d %d\n", m, n);
}
int main(){
int m = 3;
int n = 16;
int A[] = { 87, 503, 512 };
int B[] = { 61, 154, 170, 275, 426, 509, 612, 653, 677, 703, 765, 897, 908 };
merge(A, B, m, n);
for (int i = 0; i<n; i++){
printf("%d ", B[i]);
}
system("pause>0");
return 0;
}
这是程序的逻辑:
Preliminary: A is an array of integers of length m and B is an array of integers of length n. Also the elements from both arrays are distinct (from the elements in both arrays) and in ascending order.
Step 1: if n or m is zero STOP. Otherwise if m>n, set t = [log (m/n)] and go to Step 4, else set t = [log (n/m)].
Step 2: compare A[m] with B[n + 1 – 2t]. If A[m] is smaller, set n = n – 2^t and return to Step1.
Step 3: using binary search (which requires exactly t more comparisons), insert A[m] into its proper place among B[n + 1 - 2t ] ... B[n]. If k is maximal such that B[k] < A[m], set m = m - 1 and n = k. Return to Step1.
Step 4: (Step 4 and 5 are like 2 and 4, interchanging the roles of n and m, A and B) if B[n] < A[m+1-2t ], set m := m - 2t and return to Step 1.
Step 5: insert B[n] into its proper place among the A’s. If k is maximal such that A[k] < B[n], set m = k and n = n - 1. Return to Step1.
最佳答案
一个问题是因为 binsearch。我认为你用错了。
binsearch 如果找不到元素,则返回 -1
,否则返回该元素在数组中的索引。而且我认为你的 binsearch
版本就我所见和测试的而言正是这样做的。
您正在另一个数组(B
或 A
)中搜索一个数组(A
或 B
)的元素) 。 binsearch 返回 -1
,因为 A
和 B
不同。 k
总是返回 -1
。
在 C/C++ 中数组是固定大小的。大小一旦确定就不能更改。 A
的大小是 3,B
的大小是 13。增加数组的大小是错误的。
附录
Binsearch 实验
#include<iostream>
#include<cmath>
using namespace std;
/* binsearch from The C Programming Language (Second Edition) */
int binsearch1(int search_val, int array[], int array_len) {
int low, high, mid;
low = 0;
high = array_len - 1;
while (low <= high) {
mid = (low + high) / 2;
if (search_val < array[mid])
high = mid - 1;
else if (search_val > array[mid])
low = mid + 1;
else
return mid;
}
return -1; /* no match */
}
/* binsearch from SO question: http://stackoverflow.com/q/34246941/1566187 */
int binsearch2(int array[], int first, int last, int search_key) {
int index;
if (first > last)
index = -1;
else {
int mid = (first + last) / 2;
if (search_key == array[mid])
index = mid;
else
if (search_key < array[mid])
index = binsearch2(array, first, mid - 1, search_key);
else
index = binsearch2(array, mid + 1, last, search_key);
}
return index;
}
/*
* Comparing binsearch from reference book and So question
*/
int main() {
int m = 3;
int A[] = {87, 503, 512};
int i = binsearch1(503, A, m);
int j = binsearch2(A, 0, m - 1, 503);
cout << "Elements are found at indices" << endl;
cout << i << ", " << j << endl;
i = binsearch1(99, A, m);
j = binsearch2(A, 0, m - 1, 99);
cout << "Element are not found, thus return is -1" << endl;
cout << i << ", " << j << endl;
return 0;
}
关于c++ - 我的插入方法和binsearch方法有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34246941/