我的程序的一小部分有问题,它制作一个随机数列表,然后 shell 对它们进行排序,现在它不会完成计算,这让我认为循环没有完成。我遇到了分段错误错误,但我设法通过解决我访问数组的一些问题来解决这个问题。无论如何,一双新鲜的眼睛可能对我有好处。抱歉,如果选项卡已关闭。
我认为问题在于 shell_results 数组
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*Initialize Random Array*/
int *unsorted_list[1000000];
int random_number;
int i;
for(i=0; i<1000000; i++){
srand ( time(NULL) );
random_number = rand();
unsorted_list[i] = random_number;
}
//Do C Shell Sort
double shell_results[10][2];
double clock_diff;
int j=1000000;
clock_t t0, t1;
int k;
for(i=0;i<10;i++){
/* Sort the list using shellSort and take the time difference */
t0 = clock();
shellSort(*unsorted_list, j);
t1= clock();
/*Take difference in time*/
clock_diff = (t1 - t0);
/*Add time and list length to the results array*/
shell_results[i][0] = (double)j;
shell_results[i][1] = clock_diff;
/*Check to make sure the array has been sorted*/
checkSort(*unsorted_list, j);
/*Re-initialize a longer array*/
//j+=1000000;
for(k=0; k<j; k++){
srand ( time(NULL) );
random_number = rand();
unsorted_list[k] = random_number;
}
}
return 0;
}
void shellSort(int *A, int n)
{
int gap , i , j , temp;
for (gap = n/2; gap>0; gap /=2)
for (i=gap; i<n; i++)
for(j = i-gap; j>=0 && &A[j] > &A[j+gap]; j-=gap){
temp = &A[j];
A[j] = &A[j + gap];
A[j + gap] = temp;
}
}
void checkSort(int *A, int n)
{
int i;
for(i=0;i<n;i++){
if(&A[i]>&A[i+1]){
printf("Error in sorting \n");
break;
}
}
}