使用指针在C中冒泡排序

时间:2015-10-13 13:16:46

标签: 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 个答案:

答案 0 :(得分:1)

乍一看,我发现您的代码有两个问题:

        int array[n];

这是一个本地阵列。您必须在输入函数之外声明该数组,以便继续从其他函数中使用它。

其次,在你的bubblesort函数中,你在定义的数组之外进行比较(如果你实际定义它,目前没有任何东西,所以它指向垃圾并且只用垃圾工作)

答案 1 :(得分: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));
            }
        }
    }
}

为了省去一些麻烦,这是一个&#34;驱动程序&#34;测试程序:

#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]);
}

答案 2 :(得分: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;
}

答案 3 :(得分:0)

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

感谢您的意见和帮助!