#include <iostream> // used for input and output.
#include <conio.h> // used for getch().
#include <cstdlib> // used for random number generation.
#include <time.h> // used to seed the random number generator.
#include <cmath> // used for floor
using namespace std;
void array_merger(int* array, int beg, int endd)
{
int median = floor((beg + endd) / 2); // midpoint
int min_pos = 0; // minimum index of the array
int new_beg = beg; // new beginning of the array index used to cycle through the array from left to right
int new_end = median + 1; // indexed at the midpoint of the array
// Temp array
int temp[endd-beg+1]; //declares and initializes a new temporary array to hold the unsorted data
while ( new_beg <= median && new_end <= endd ) // merges in sorted form the 2 arrays
if ( array[new_beg] < array[new_end] )
temp[min_pos++] = array[new_beg++];
else
temp[min_pos++] = array[new_end++];
while ( new_beg <= median ) // merges the remaining elements in left array
temp[min_pos++] = array[new_beg++];
while ( new_end <= endd ) // merges the remaining elements in right array
temp[min_pos++] = array[new_end++];
for ( int i = beg; i <= endd; i++ ) // moves from temp array to master array
array[i] = temp[i-beg];
}
void merge_sort(int* array, int beg, int endd)
{
if ( beg < endd ) // checks to see if there is more than one element in the array
{
int median = floor((beg + endd) / 2); // finds the midpoint of the array of 2 or more elements
merge_sort(array, beg, median); // uses recursion to further split the array in half (divide and conquer) on the left side
merge_sort(array, median + 1, endd); // now working back up from left to right
array_merger(array, beg, endd); // calls array_merger to put the pieces back together
}
}
// printer function
void print(int *array, int ARRAY_LENGTH) //prints results
{
for ( int i = 0; i < ARRAY_LENGTH; i++ ) // cycles through printing each element array from index 0 to the last index of the array
cout << array[i] << " ";
cout << endl;
}
//reusing your randomizeArray function
void randomizeArray(int array[], int length)
{
srand ( (unsigned) time(NULL)); // seed random number generator.
int i = 0;
do
{
array[i] = rand() % 100000 + 1; // a random number in the range of 1 to 100,000 is assigned.
i++;
} while (i < length);
}
void timeSort(int array[], int length)
{
clock_t startTime, endTime;
// Randomize values in array.
randomizeArray(array, length);
// Time array sort.
startTime = clock(); // Get starting time.
merge_sort(array, 0, length); // Sort array.
endTime = clock(); // Get ending time.
// Display algorithm's running time as difference between starting and ending time.
cout << "Merge sort time for an array of "
<< length
<< ": "
<< ( (float) endTime - (float) startTime) / CLOCKS_PER_SEC * 1000
<< " milliseconds." // On my machine, CLOCKS_PER_SEC is equal to 1000 and
// thus milliseconds is the correct unit.
<< endl;
}
int main()
{
//const int ARRAY_LENGTH = 20000; //sets the array length for timeSort (bigO)
//int test1[ARRAY_LENGTH]; // initializing an array for the first test
//timeSort(test1, ARRAY_LENGTH); // calling timeSort
//int test2[ARRAY_LENGTH*2];
//timeSort(test2, ARRAY_LENGTH*2);
//int test3[ARRAY_LENGTH*3];
//timeSort(test3, ARRAY_LENGTH*3);
//int test4[ARRAY_LENGTH*4];
//timeSort(test4, ARRAY_LENGTH*4);
const int ARRAY_LENGTH = 20; // tests to see if the logic is correct
int logic_test[ARRAY_LENGTH];
randomizeArray(logic_test, ARRAY_LENGTH);
cout << "Input: ";
print(logic_test, ARRAY_LENGTH); //uses print function to print unsorted array
merge_sort(logic_test, 0, ARRAY_LENGTH-1);
cout << "Output: ";
print(logic_test, ARRAY_LENGTH); //uses print function to print merge sorted array
getch();
return 0;
}
好的..所以我得到的错误是在array_merger函数内。初始化int temp [endd-beg + 1]时,它要求常量。这导致它无法执行该方法的其余部分。
同样,int中位数= floor((beg + endd)/ 2)也要求常数。
这些东西不会在代码块中引起问题,但在Visual Studio中会引起问题。
最佳答案
原因一定是编译器之间的差异。您的CodeBlocks很可能正在使用C++编译器的扩展,并允许使用可变大小的数组。 VisualStudio正在运行其严格的C++编译器,并且在C++中不允许使用可变大小的数组,并且该数组应始终具有固定的大小,在编译时即已知道。
根据您要实现的目标,解决方案将有所不同。