2017-10-07 4 views
1

次のコードは、mergeSortアルゴリズムを使用して最大1500000個の数値の配列を並べ替えるコードです。しかし、私はラインのエラーを取得し続けるlong temp [] = new long[end - start + 1]; Mergesortアルゴリズムのエラー

私が得続けるエラーメッセージは:'initializing': cannot convert from 'long *' to 'long []'initialization with '{...}' expected for aggregate object.その特定の行を修正する方法はありますか?

#ifndef DataGen_h 
#define DataGen_h 
#include "RandomSupport.h" 
#include<limits> 
#include<iostream> 

void merge(long list[], long start, long end) { 

    long middle = (start + end)/2; 


    long temp [] = new long[end - start + 1]; 
    long temp_index = 0; 


    long left = start; 
    long right = middle + 1; 


    while((left <= middle) && (right <= end)) { 

     if (list[left] < list[right]) {    
      temp[temp_index] = list[left]; 
      temp_index++; 
      left++; 
     } else { 
      temp[temp_index] = list[right]; 
      temp_index++; 
      right++; 
     } 
    } 

    while(left <= middle) { 
     temp[temp_index] = list[left]; 
     temp_index++; 
     left++; 
    } 

    while(right <= end) { 
     temp[temp_index] = list[right]; 
     temp_index++; 
     right++; 
    } 

    for(long i = start; i <= end; i++) { 
     list[i] = temp[i - start]; 
    } 
} 


void mergeSort(long list[], long start, long end) { 
    if(start < end) { 

     long middle = (start + end)/2; 

     mergeSort(list, start, middle); 
     mergeSort(list, middle + 1, end); 

     merge(list, start, end); 
    } 
} 

void efficientRandomSortedList(long temp[], long s){ 
// Get a new random device 
randomizer device = new_randomizer(); 
// Get a uniform distribution from 1 to 1000 
uniform_distribution range = new_distribution(1, 15000000); 

for (long i = 0; i < s; i++) { 
    // At every cell of the array, insert a randomly selected number 
    // from distribution defined above 
    temp[i] = sample(range, device); 
} 

// Now sort the array using insertion_sort 
mergeSort(temp,0,s); 

答えて

1

new long[end - start + 1]は、メモリのブロックへのポインタを返すので、あなたはそれを受け入れるために、適切なlong *変数必要があります。

long *temp = new long[end - start + 1]; 

をおnew ...[]を使用しているので、あなたはに対応しdelete[]文を持っている必要がありますあなたがそれを使用し終えたら、メモリを解放してください。 merge()の末尾に次の文字を追加することを忘れないでください。

delete[] temp;