c - 通过 malloc 函数选择排序

标签 c arrays malloc

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i,n,min,j;
    int *array; 
    printf("The no of elements >> ");
    scanf("%d\n",&n);
    printf("Making array\n" );
    array = (int *)malloc (sizeof(int) * n);
    printf("array made!\n");

    //INPUT NUMBERS
    for (i=0 ; i<n ; i++)
        {
            printf(">>Enter the element %d\n" , i+1);
            scanf("%d\n", &array[i]);
        }

    //SORT THE ARRAY
    for (i = 0; i < n; ++i)
        {
            min = array[0];
            for (int j = i+1; j < n; ++j)
            {
                if (array[i] > array[j])
                    min = array[j];
            }
            if (min != array[i])
            {
                int temp;
                temp = array[i];
                array[i]= min;
                array[j]=temp;
            }
        }

    //PRINTING ARRAY
    for (i=0 ; i<n ; i++)
        {
            printf("-- %d --\n" , array[i]);
        };
}

我在 c 语言中使用 Malloc 为数组编写了 SelectionSort 的特定代码。但是,它在“元素号>>”之后经历了 2 次输入,并且也不会排序。 如果我继续这样做,它会按以下方式给我一个未排序的数组

The no of elements >> 5
4
Making array
array made!
>>Enter the element 1
5
>>Enter the element 2
2
>>Enter the element 3
1
>>Enter the element 4
7
>>Enter the element 5
8
-- 7 --
-- 1 --
-- 1 --
-- 2 --
-- 1 --

我已经阅读了一些其他帖子并更正了我的代码,但这仍然行不通。 想不通为什么。

编辑 1 通过对代码进行一些更改确实解决了双输入问题。

int main()
{   int i,n,min,j;
    int *array; 
    printf("The no of elements >> ");
    scanf("%d",&n);
    array = (int *)malloc (sizeof(int) * n);

    for (i=0 ; i<n ; i++)
        {
            printf(">>Enter the element %d\n" , i+1);
            scanf("%d", &array[i]);
        }

    //SORT THE ARRAY
    for (i = 0; i < n; ++i)
        {
            min = array[i];
            for (int j = i+1; j < n; ++j)
            {
                if (array[i] > array[j])
                    min = array[j];
            }
            if (min != array[i])
            {
                int temp;
                temp = array[i];
                array[i]= min;
                array[j]=temp;
            }
        }

    //PRINTING ARRAY
    for (i=0 ; i<n ; i++)
        {
            printf("-- %d --\n" , array[i]);
        };
}

但是,现在输入还是不正确

The no of elements >> 5
Making array
array made!
>>Enter the element 1
5
>>Enter the element 2
4
>>Enter the element 3
6
>>Enter the element 4
9
>>Enter the element 5
2
-- 9 --
-- 2 --
-- 2 --
-- 2 --
-- 2 --

编辑 2 经过几次更正,离最终代码还有一些错误,我在

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n,min,j;
    int *array; 
    printf("The no of elements >> ");
    scanf("%d",&n);
    printf("Making array\n" );
    array = (int *)malloc (sizeof(int) * n);
    printf("array made!\n");

    //INPUT NUMBERS
    for (i=0 ; i<n ; i++)
        {
            printf(">>Enter the element %d\n" , i+1);
            scanf("%d", &array[i]);
        }

    //SORT THE ARRAY
    for (i = 0; i < n; i++)
        {
            min = i;
            for ( j = i+1; j < n; j++)
            {
                if (array[j] < array[i])
                    min = j;
            }
            if (min != i)
            {
                int temp;
                temp = array[i];
                array[i]= array[min];
                array[min]=temp;
            }
        }

    //PRINTING ARRAY
    for (i=0 ; i<n ; i++)
        {
            printf("-- %d --\n" , array[i]);
        };
}

这导致了一个有点排序的,但仍然不正确的数组

The no of elements >> 5
Making array
array made!
>>Enter the element 1
4
>>Enter the element 2
5
>>Enter the element 3
2
>>Enter the element 4
1
>>Enter the element 5
7
-- 1 --
-- 4 --
-- 2 --
-- 5 --
-- 7 --

编辑 3:最终代码 为了点击和试用,我编辑了几次代码。我终于发现这段代码可以正常工作。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n,min,j;
    int *array; 
    printf("The no of elements >> ");
    scanf("%d",&n);
    printf("Making array\n" );
    array = (int *)malloc (sizeof(int) * n);
    printf("array made!\n");

    //INPUT NUMBERS
    for (i=0 ; i<n ; i++)
        {
            printf(">>Enter the element %d\n" , i+1);
            scanf("%d", &array[i]);
        }

    //SORT THE ARRAY
    for (i = 0; i < n; ++i)
        {
            min = i;
            for ( j = i+1; j < n; ++j)
            {
                if (array[j] < array[min])
                    min = j;
            }
            if (min != i)
            {
                int temp;
                temp = array[i];
                array[i]= array[min];
                array[min]=temp;
            }
        }

    //PRINTING ARRAY
    for (i=0 ; i<n ; i++)
        {
            printf("-- %d --\n" , array[i]);
        };
}

但是我仍然有兴趣知道为什么++i/++j 分别与 i++/j++ 相比没有区别。

最佳答案

应该是:

scanf("%d",&n);

不是:

scanf("%d\n",&n);

编辑:

在外层 for 循环中,它应该是 min = array[i];

关于c - 通过 malloc 函数选择排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22270518/

相关文章:

c - 静态字符 *buf = NULL

c - 与括号一起使用时宏的行为会发生变化

java - GridLayout 删除 JPanel 之间的填充

malloc - NASM malloc 返回 NULL

c - 如何初始化 const float32x4x4_t (ARM NEON intrinsic, GCC)?

ios - 如何在 iOS 项目中使用 LibXtract?

Javascript 将以太坊 web3 json 对象转换为数组

c++ - 在 C++ 中,整数数组之间可以有空元素吗?

c - 包装 malloc - C

c - 结构内的动态内存