2015-10-13 77 views
-2

我试图编写一个使用指针进行气泡排序的代码。 它给我没有错误,但结果产生随机数字或“分段错误”消息。 请问您是否可以查看我的代码并说出错位置?使用指针的C中的气泡排序

#include <stdio.h> 

int * input(int n); 
void print(int *ptr, int n); 
int * bubble_sort(int *ptr, int n); 
void swap(int *a,int *b); 

int main(){ 
     int n; 

     printf("Enter the number of the elements to be sorted:\n"); 
     scanf("%d",&n); 


     int *ptr; 
     ptr = input(n); 
     print(ptr,n); 

     int *ptr_sort; 

     ptr_sort = bubble_sort(ptr,n); 
//  bubble_sort(ptr,n); 
     print(ptr_sort,n); 
//  print(ptr,n); 

     return 0; 
} 

int * input(int n){ 
     int i; 
     int array[n]; 

     int *ptr; 

     ptr = array; 


     for(i=0;i<n;i++){ 
       printf("Enter element %d value: \n", i+1); 
       scanf("%d",ptr+i); 
     } 

     return ptr; 
} 

void print(int *ptr, int n){ 
     int i; 
     for(i=0;i<n;i++) 
       printf("%d\t",*(ptr+i)); 
     printf("\n"); 
} 

int * bubble_sort(int *ptr, int n){ 
     int i,j; 

     for(i=0;i<n-1;i++) 
       for(j=0;j<n-i-1;j++) 
         if(*(ptr+j)>*(ptr+j+1)) 
           swap((ptr+j),(ptr+j+1)); 

     return ptr; 
} 

void swap(int *a,int *b){ 
     int tmp; 
     tmp = *a; 
     *a = *b; 
     *b = tmp; 
} 

我尝试了一些调试,valgrid(这我不是很用),我收到的仅仅是第一打印完成后的以下信息:这一次后

==2318== Conditional jump or move depends on uninitialised value(s) 
==2318== at 0x400803: bubble_sort (ex18_buble_sort.c:58) 
==2318== by 0x40065D: main (ex18_buble_sort.c:21) 
==2318== 
==2318== Invalid write of size 4 
==2318== at 0x40088F: swap (ex18_buble_sort.c:68) 
==2318== by 0x40083B: bubble_sort (ex18_buble_sort.c:59) 
==2318== by 0x40065D: main (ex18_buble_sort.c:21) 
==2318== Address 0xf0000000f is not stack'd, malloc'd or (recently) free'd 

和许多其他的消息,但我猜一切都从这里开始。所以最有可能的问题是在bubble_sort函数中的某个地方。

请帮忙!

+4

'int array [n];'是一个局部数组,它将在函数input的执行结束后被销毁。修复它包括使用一个全局数组,为它动态分配内存并使用'static int array [n];' –

+1

简单地用int * array = malloc(n * sizeof(int))替换数组定义;''似乎修复'valgrind'报告的所有无效读/写操作。你也必须'释放'它虽然... – dragosht

回答

1

第一眼我看到了两个问题与您的代码:

 int array[n]; 

那本地阵列。您将不得不在输入函数之外声明该数组,以便从其他函数继续使用它。

其次,在你冒泡功能你做比较的定义阵列之外(如果你真的把它定义,目前,并没有什么,所以它指向垃圾,只有垃圾作品)

+0

'VLA'不允许为'static':'错误:'数组'的存储大小不是常量'是编译器错误。 – mch

+0

啊,该死的。我会编辑它。 – Magisch

0

下面是一个简单的使用指针进行气泡排序的示例。

void exchange(unsigned long *a, unsigned long *b) 
{ 
    unsigned long cache = *a; 
    *a = *b; 
    *b = cache; 
} 

void bubble_sort(unsigned long *numray, int n) 
{ 

    unsigned long varhold, c, d; 

    for (c = 0 ; c < (n - 1); c++) { 
     for (d = 0 ; d < n - c - 1; d++) { 
      if (*(numray + d) > *(numray + d + 1)) { 
       exchange((numray + d), (numray + d + 1)); 
      } 
     } 
    } 
} 

,并保存了一些麻烦,这里是一个 “司机” 的测试程序里面:

#include <stdio.h> 
#include <stdlib.h> 

void print_array(unsigned long *, int); 
void exchange(unsigned long *, unsigned long *); 
void bubble_sort(unsigned long *, int); 


int main(int argc, char *argv[]) 
{ 

    unsigned long c = 0; 
    unsigned long elements = 10000; 
    unsigned long *numray = malloc(elements * sizeof(unsigned long)); 

    srandom(time(NULL)); 
    for (; c < elements ; c++) 
     numray[c] = (random() % 9999999999) * 1000000000000000000; 

    print_array(numray, elements); 
    printf("====================\n"); 
    bubble_sort(numray, elements); 
    print_array(numray, elements); 

    return 0; 
} 

void bubble_sort(unsigned long *numray, int n) 
{ 

    unsigned long varhold, c, d; 

    for (c = 0 ; c < n - 1; c++) { 
     for (d = 0 ; d < (n - c - 1) ; d++) { 
      if (*(numray + d) > *(numray + d + 1)) { 
       exchange((numray + d), (numray + d + 1)); 
      } 
     } 
    } 

} 


void exchange(unsigned long *a, unsigned long *b) 
{ 
    unsigned long cache = *a; 
    *a = *b; 
    *b = cache; 
} 


void print_array(unsigned long *a, int b) 
{ 
    int c; 
    for (c=0 ; c < b ; c++) 
     printf("%lu\n", a[c]); 
} 
0

试试这个:(代码

#include <stdio.h> 

void input(int n); 
void print(int *ptr, int n); 
void bubble_sort(int *ptr, int n); 
void swap(int *a,int *b); 

int main(){ 
     int n; 

     printf("Enter the number of the elements to be sorted:\n"); 
     scanf("%d",&n); 
     input(n); 
     return 0; 
} 

void input(int n){ 
     int i; 
     int array[n]; 

     int *ptr; 
     for(i=0;i<n;i++){ 
       printf("Enter element %d value: \n", i+1); 
       scanf("%d",&array[i]); 
     } 
     print(array,n);//before sort 
     bubble_sort(array,n); 
     print(array,n);//after sort 
} 

void print(int *ptr, int n){ 
     int i; 
     for(i=0;i<n;i++) 
       printf("%d\t",*(ptr+i)); 
     printf("\n"); 
} 

void bubble_sort(int *ptr, int n){ 
     int i,j; 

     for(i=0;i<n-1;i++) 
       for(j=0;j<n-i-1;j++) 
         if(*(ptr+j)>*(ptr+j+1)) 
           swap((ptr+j),(ptr+j+1)); 
} 

void swap(int *a,int *b){ 
     int tmp; 
     tmp = *a; 
     *a = *b; 
     *b = tmp; 
} 
的一些变化
+0

让我知道它的工作与否。 –

+0

作品!但我最初的想法是使用main调用input,print和bubble_sort。所以,看起来我犯的最大错误就是我写输入函数的方式。它现在有效!谢谢! – maimun4itu

+0

然后,请让它正确答案:)。 –

0

我修复了我的bubble_sort函数,但它在数组被声明为输入函数中的本地数组时继续生成随机数。一旦我宣布它一切正常。

感谢您的意见和帮助!