C语言 在 C 函数中对数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15955458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort an array in a C function
提问by Jordan Kasper
So I need to pass the array to the function sort and have it sort it, it works outside but cant get it to work inside the function. Need to pass in the pointers just not sure how.
所以我需要将数组传递给函数 sort 并让它对其进行排序,它在外部工作但不能让它在函数内部工作。需要传入指针只是不确定如何。
#include <stdio.h>
void sort(int *number, int n) {
/* Sort the given array number, of length n */
int temp = 0, j, i;
for (i = 1; i < n; i++) {
for (j = 0; j < n - i; j++) {
if (number[j] > number[j + 1]) {
temp = number[j];
number[j] = number[j + 1];
number[j + 1] = temp;
}
}
}
}
int main() {
int n = 20;
int *ptr = malloc(n * sizeof *ptr);
int i = 0;
while (i < n) {
ptr[i] = rand() % 100;
i++;
}
int j = 0;
while (j < n) {
printf("%d , ", ptr[j]);
j++;
}
void sort(ptr, n);
printf("\n");
int x = 0;
while (x < n) {
printf("%d , ", ptr[x]);
x++;
}
}
回答by K Scott Piel
void sort(ptr, n);should just be sort(ptr, n);
void sort(ptr, n);应该只是 sort(ptr, n);
I could be wrong on this, but I believe in a C compiler, the first form is interpreted as a function prototype and the second an actual function call.
我可能是错的,但我相信在 C 编译器中,第一种形式被解释为函数原型,第二种形式被解释为实际的函数调用。
回答by djechlin
Firstly, when I compile I get two compiler warnings. Never ignore compiler warnings. In fact, in this case, one of the compiler warnings is happening on exactly the line in your code that is broken. In general compile with -Wall.
首先,当我编译时,我收到两个编译器警告。永远不要忽略编译器警告。事实上,在这种情况下,编译器警告之一恰好发生在代码中被破坏的行上。一般用-Wall.
Nextly, run this in a debugger. Compile with -gand use gdbfor instance. You can google a cheat sheet for how to get started with gdb pretty easily. Watch what is happening line by line on your program and you will know what the error is. In this particular case you would observe that the line in which you call your function is skipped over. This line is already being reported with a warning, so it should come as no surprise if it functions surprisingly.
接下来,在调试器中运行它。例如编译-g和使用gdb。您可以在 google 上搜索一个备忘单,了解如何非常轻松地开始使用 gdb。逐行观察程序中发生的情况,您就会知道错误是什么。在这种特殊情况下,您会发现调用函数的行被跳过。这条线已经被报告了一个警告,所以如果它的功能出人意料,那就不足为奇了。
回答by Abdul-Razak Adam
This piece of code sorts an array using pointers. Hope it helps
这段代码使用指针对数组进行排序。希望能帮助到你
//@author Abdul-Razak Adam
#include <stdio.h>
#include <stdlib.h>
void fillArray(int * array, int size);
void printArrayValue(int * array, int size);
int getIndexOfMin(int * array, int size);
void removeAnIndexFromArray(int* array, int size, int index);
int* sortArray(int* array, int size);
//test
int main (void){
int* SIZE; //size
int * array;
SIZE = (int*) malloc(sizeof(int));
scanf("%d", SIZE);
//printf("%i\n", *SIZE);
int size = *SIZE;
array = (int*) malloc (size * sizeof(int));
fillArray(array,size);
printArrayValue(array,size);
printf("..............................................\n");
printf("Array Sorted\n");
printf("..............................................\n");
array = sortArray(array,size); //get pointer to sorted array
printArrayValue(array,*SIZE);
//free memory
free(array);
free(SIZE);
}
//fill array with random values
void fillArray(int* array, int size){
int i = 0;
for (i = 0; i < size; i++){
*(array + i) = rand()%100;
}
}
//print value from array
void printArrayValue(int* array, int size){
int i = 0;
for (i = 0; i < size; i++){
printf("%i , ", *(array+i));
}
printf("\n");
}
//sort value of array
int* sortArray(int* array, int size){
int min = *array;
int originalSize = size;
int * temp;
temp = (int* )malloc(sizeof(int) * size);
int i = 0;
int index;
while(size > 0){
index = getIndexOfMin(array,size);
temp[i++] = array[index];
removeAnIndexFromArray(array, size --,index);
}
return temp;
}
//get the index of the minimum value in the array
int getIndexOfMin(int * array, int size){
int min = *array;
int i = 0 ;
int index = 0;
for (i = 0 ; i < size; i++){
if (min > *(array+i)){
min = *(array+i);
index = i;
}
}
return index;
}
//remove the minimum value from the array
void removeAnIndexFromArray(int* array, int size, int index){
*(array + index) = 0;
if (index != (size-1)){
int i = index;
for (i = index; i < (size -1); i++){
*(array + i) = *(array + (i+1));
}
}
}

