Sort an array in ascending order according to absolute value, but show real value C programming

154 Views Asked by At

I'm new at programming. This is the code that I've been trying to write, but obviously it doesn't show the original values, it prints the absolute value of the numbers of the array sorted in ascending order Any help is appreciated! Input example: 54 -2 6 0 82 59 22 1 -654 -21 The result I want: 0 1 -2 6 -21 22 54 59 82 -654

#include <stdio.h>
#define N 10
int main() {
    
     int array[N];
 int i, j;
    for (i = 0; i < N; i++)
    {
        scanf("%d", &array[i]);
    }
    for (i = 0; i < N; i++)
    {
        if (array[i] < 0)
            array[i] = -array[i];
        if (array[i] > 0)
            array[i] = array[i];
    int min_idx = 0;
    min_idx = i;
    for (i = 0; i < N - 1; i++)
    {
        min_idx = i;

        for (j = i + 1; j < N; j++)
            if (array[j] < array[min_idx])
            {
                min_idx = j;
            }
        if (min_idx != i)
        {
            int csere = array[i];
            array[i] = array[min_idx];
            array[min_idx] = csere;
        }
        
    }
    for (i=0; i<N; i++)
        printf("%d\n", array[i]);
    

    return 0;
}
0

There are 0 best solutions below