簡體   English   中英

我的insert方法和binsearch方法有問題嗎?

[英]Is there something wrong with my insert method and binsearch method?

我正在學習編程,並且已經創建了一個void merge() 我嘗試添加二進制搜索方法和插入方法。 程序應按升序輸出數組AB的組合元素的列表。 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;
}

這是程序的邏輯:

初步 :A是一個長度為m的整數數組,而B是一個長度為n的整數數組。 同樣,兩個數組中的元素都是不同的(不同於兩個數組中的元素)並且升序排列。

步驟1 :如果n或m為零,則停止。 否則,如果m> n,則設置t = [log(m / n)]並轉到步驟4,否則設置t = [log(n / m)]。

步驟2 :將A [m]與B [n + 1 – 2 t ]比較。 如果A [m]較小,則設置n = n – 2 ^ t並返回到步驟1。

第3步 :使用二分查找(恰好需要進行t次比較),將A [m]插入B [n + 1-2 t ] ... B [n]中的適當位置。 如果k最大值使得B [k] <A [m],則設置m = m-1且n = k。 返回到步驟1。

步驟4 :(步驟4和5類似於2和4,互換n和m,A和B的作用),如果B [n] <A [m + 1-2 t ],則設置m:= m-2 t並返回到步驟1。

步驟5 :將B [n]插入A中的適當位置。 如果k最大值使得A [k] <B [n],則設置m = k且n = n-1。返回步驟1。

一個問題是由於binsearch 我認為您使用錯誤。 binsearch如果找不到元素,則返回-1 ,否則它將返回該元素在數組中的索引。 而且據我所見和測試,您認為您的binsearch版本確實可以做到。
您正在搜索另一個數組( BA )中一個數組( AB )的元素。 binsearch返回-1 ,因為AB是不同的。 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM