我的 C 程序有什么问题?关于指针和选择排序

What's wrong with my C program? It's about pointer and selection sort

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

我在提供的代码中既没有找到快速排序也没有找到冒泡排序。

您似乎正在尝试使用选择排序。

我认为函数可以如下所示。

void selection_sort( int * a[], int n )
{
    int i;

    for ( i = 0; i < n; i++ )
    {
        int position = i;
        int j = i + 1;

        for ( ; j < n; j++ )
        {
            if ( *a[j] < *a[position] ) position = j;
        }

        if ( position != i )
        {
            int *temp = a[position];
            a[position] = a[i];
            a[i] = temp;
        }
    }

    for ( i = 0; i < n; i++ ) printf( "%d ", *a[i] );
    putchar( '\n' );
}