2012-10-02 104 views
0

我有一个非常简单的(或者至少应该是)赋值,我必须在大量的随机数上运行bubblesort并查看它的执行时间。然后我必须做同样的事情,除了将数组分成一半,并在另一个线程中对另一个线程中的另一个线程进行排序,并且看看它是否更快。Bubblesort with C

我从来没有使用C之前,所以我完全无能为力指针,只有与Java一起工作。这里是我的代码,因为我只是想让bubblesort工作。

#include <string.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <stdlib.h> 
#include <libgen.h> 

int main() { 
    int *array[50000]; 
    for(int i = 0; i < 50000; i++) { 
    array[i] = 1; 
    } 
    bubbleSort(array, 50000); 
} 

void bubbleSort(int *numbers[], int *array_size) { 
    int i, j, temp; 
    for(i = (array_size - 1); i > 0; i--) { 
    for(j = 1; j <= i; j++) { 
    if(numbers[j-1] > numbers[j]) { 
    temp = numbers[j-1]; 
    numbers[j-1] = numbers[j]; 
    numbers[j] = temp; 
     } 
    } 
    } 
    for(int i = 0; i < 10; i++) { 
    printf(numbers[i]); 
    } 
} 

我想在这里做的所有事情是排序数组,然后打印出前十个数字,所以我知道它的工作。我收到各种指针错误。

"bubbleSort.c", line 11: warning: improper pointer/integer combination: op "=" 
"bubbleSort.c", line 13: warning: implicit function declaration: bubbleSort 
"bubbleSort.c", line 16: identifier redeclared: bubbleSort 
     current : function(pointer to pointer to int, pointer to int) returning void 
     previous: function() returning int : "bubbleSort.c", line 13 
"bubbleSort.c", line 18: warning: improper pointer/integer combination: op "=" 
"bubbleSort.c", line 21: warning: improper pointer/integer combination: op "=" 
"bubbleSort.c", line 23: warning: improper pointer/integer combination: op "=" 
"bubbleSort.c", line 28: warning: argument #1 is incompatible with prototype: 
     prototype: pointer to const char : "/usr/include/iso/stdio_iso.h", line 206 
     argument : pointer to int 
cc: acomp failed for bubbleSort.c 
+0

'阵列[i] = 1;'我知道,通过公平的掷骰和所有的决定,但是这不是与“随机数的阵列”旨在测试性能。 –

+0

哎呀,我改变了这一点,因为我得到了一个错误,并忘记在发布之前将它改回来。而不是一个我有rand()那里,但它不喜欢那个。现在让我改回它,看看错误是什么。 – user1704677

回答

4

此:

int *array[50000]; 

声明指针50000元素的数组来int,这可能不是你想要的。删除*

bubbleSort()原型中,你也应该删除虚假的星号。

请注意,星号意味着东西在C中,你不应该随机装饰你的代码与他们在任何你想要的。如果你不确定什么这意味着和,你应该有权访问一些教程信息,如果这是一个类。开始阅读。

+0

我原本一切都没有指针。如果可能的话,我想避免它们。不幸的是,这不是一门介绍性课程,我们有时会被抛入火中,并期望知道C.当我从bubbleSort方法的数组和参数中删除星号时,这里是我得到的错误这让我相信我需要星号。 'code'“bubbleSort。c“,第16行:标识符被重新声明:bubbleSort current:function(指向int,int的指针)返回void 上一个:function()返回int:”bubbleSort.c“,第13行'code' – user1704677

1

线11:你不应该申报int *array[]int array[]代替
线13:原型的功能或声明它的主要
线16以上:你宣布int *array_size但在主你给它是一个int
行18,21和23:相同。
第28行:从不使用带有可变格式字符串的printf! printf("%i, ", numbers[i]);就是这样。

你真的应该检查C编码基础

+0

谢谢。有些东西与我习惯的语言不同,我有一点工作要做,再次感谢。 – user1704677