2017-05-14 27 views
-5

我是新來的c + +並試圖開發合併排序代碼。我用一個大小爲15的樣本數組對它進行了測試,但代碼發佈的答案並不正確。我無法弄清楚發生了什麼問題。這裏是我的代碼:C++合併排序問題

#include <stdlib.h> 
#include <stdio.h> 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include <unistd.h> 
#include <cstdlib> 

using namespace std; 

//two arrays, input 
int initial[15] = {200,1,2,9,8,98,6,44,5,4,67,15,3,2,0}; 
//for output 
int fin[15]; 

void sort(int* ini, int left, int right, int m); 

//saperate the input in a recursion 
void devide(int* ini, int left, int right){ 
    int m; 

    if(left < right){ 
      m = (left + right)/2; 
      devide(ini, left, m); 
      devide(ini, m+1, right); 

      sort(ini, left, right, m); 
    } 
} 


//sorting 
void sort(int* ini, int left, int right, int m){ 

    //first half, start at the first element in the input array 
    int first_h = left; 

    //second half, start at the first element after the 
    // middle element in the input array 
    int second_h = m+1; 

    //index for output array 
    int out = left; 

    //comparing, if both half of array have element 
    while(first_h <= m && second_h <= right){ 

      //put the smaller in the the output array 
      if(initial[first_h] < initial[second_h]){ 
       fin[out] = initial[first_h]; 
       first_h++; 
      } 
      else{ 
       fin[out] = initial[second_h]; 
       second_h++; 

      } 
      out++; 
    } 

    //if one of half of input is empty, put the rest element into the 
    // output array 
    while(first_h <= m){ 
      fin[out] = initial[first_h]; 
      out++; 
     first_h++; 
    } 
    while(second_h <= right){ 
      fin[out] = initial[second_h]; 
      out++; 
      second_h++; 
    } 
} 

int main(){ 
    devide(initial, 0, 14); 

    for(int i =0; i<15; i++){ 
      cout<<fin[i]; 
      cout<<","; 
    } 
    return 0; 
} 

啓動[]的輸出,這是鰭[]是:

5,4,67,15,3,2,0,200,1,2,9,8,98,6,44, 
+2

解決此類問題的正確工具是您的調試器。你應該逐行遍歷你的代碼,在堆棧溢出時詢問_before_。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 – Tas

+0

@Tas以下是完整的參考評論:_解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在debugger._ –

+0

@πάνταῥεῖ不確定詢問的最佳方式,但是我完全偷了你的評論(我希望沒關係)!我有整個評論供參考,但省略了最後一部分,因爲這或多或少是一個mcve – Tas

回答

0

,這樣你就沒有考慮到一個小的錯誤是,你是傳遞指針數組ini,但是在方法內部仍然使用方法initial,還有一件事是,你應該接受一個臨時數組,它會將有序值賦給你的ini數組。

所以,你的代碼應該是這個樣子

void sort(int* ini, int left, int right, int m){ 
    int first_h = left; 
    int second_h = m+1; 
    int out = left; 
    while(first_h <= m && second_h <= right){ 
      if(ini[first_h] < ini[second_h]){ 
       fin[out] = ini[first_h]; 
       first_h++; 
      } 
      else{ 
       fin[out] = ini[second_h]; 
       second_h++; 

      } 
      out++; 
    } 

    while(first_h <= m){ 
      fin[out] = ini[first_h]; 
      out++; 
     first_h++; 
    } 
    while(second_h <= right){ 
      fin[out] = ini[second_h]; 
      out++; 
      second_h++; 
    } 
    for(int i=left; i<out; i++) 
    { 
     ini[i] = fin[i]; 
    } 
} 

希望這有助於理解算法更好!

+0

是的,它的工作,非常感謝你,但我希望將結果保存到陣列fin [],我試圖改變ini fin。但它不能正常工作。 –

+0

我已經更新了我的答案 – zenwraight

+0

只是將它再次保存在翅片中,最後一個循環,現在可以使用翅片 – zenwraight