【发布时间】:2012-10-02 15:43:51
【问题描述】:
我有一个非常简单(或至少应该是)的任务,我必须对大量随机数运行冒泡排序并查看其执行时间。然后我必须做同样的事情,除了将数组分成两半,一半在一个线程中排序,另一半在另一个线程中排序,看看这样是否更快。
我以前从未使用过 C,所以我对指针一无所知,只使用过 Java。这是我的代码,因为我只是想让冒泡排序工作。
#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
【问题讨论】:
-
array[i] = 1;我知道,由公平掷骰决定,但这并不是测试性能的“随机数数组”的意思。 -
哎呀,我已经更改了这个,因为我收到了一个错误并且忘记在发布之前将其更改回来。我在那里有 rand() 而不是一个,但它不喜欢那样。让我现在把它改回来,看看错误是什么。
标签: c unix bubble-sort