【发布时间】:2017-09-24 02:04:12
【问题描述】:
我的 C 程序有什么问题?标题是从小到大对 n 个数字进行排序,当我运行它时,一切正常,但数字没有被排序。虽然想了很久,但就是不知道怎么解决。
代码如下:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void selection(int *a[], int n);
int main()
{
int n;
int i;
scanf("%d", &n);
int *a[n];
int b[n];
srand((int) time(0));
for (i = 0; i < n; i++)
{
b[i] = ((int) (rand() % 100));
a[i] = &b[i];
}
selection(a, n);
return 0;
}
void selection(int *a[], int n)
{
int i;
int j;
int position;
int temp;
for (i = 0; i < n - 1; i++)
{
position = i;
for (j = i + 1; j < n; j++)
{
if (*a[i] > *a[j])
position = j;
}
temp = *a[i];
*a[i] = *a[position];
*a[position] = temp;
}
for (i = 0; i < n - 1; i++)
printf("%d\n", *a[i]);
}
【问题讨论】:
-
代码真的是关于快速排序的吗?
-
你为什么要
quicksort而你的函数名是bubble...o.O -
@Junsong Huang 没有快速排序,也没有冒泡排序。我认为有写错的选择排序。:)
-
@JunsongHuang 当你说
quicksort,你的意思是qsort对吗?
标签: c arrays sorting pointers selection-sort