我遇到了一小部分的問題,它使得隨機數列表出現,然後shell將它們排序,現在它不會完成計算,這使我認爲循環不是沒有完成。我得到了分段錯誤的錯誤,但我設法解決了一些問題,我如何訪問我的數組。無論如何,一套新的眼睛可能對我有好處。對不起,如果tabb關閉。在C中遇到故障分段錯誤
我認爲這個問題是與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;
}
}
}
您是否嘗試過使用調試器? – jogojapan 2013-03-07 01:42:09