Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to sort the array using the choice method in C?

+2 votes
asked Dec 3, 2020 by Miafina (900 points)

The code:

#include <stdio.h>
int main()
{
    float a[15]={2,6,7.5,4.1,5,2.3,6.2,3,1.5,4.9,0.3,12,3,14,1.4};
    int max=0;
    float buf=0;
    for(int i=0;i<15;i++){
        max=i;
        for(int j=i+1;j<15;j++){
            if(a[j]>a[max]){
              max=j;  
            }
              if(i!=max){
              buf=a[i];
              a[i]=a[max];
              a[max]=buf;   
            }
            
        }
    }
    for(int i=0;i<15;i++){
      printf("%.1f ", a[i]);  
    }
    return 0;
}

The result: 7.5 6.2 5.0 6.0 4.1 3.0 12.0 4.9 3.0 2.3 2.0 1.5 1.4 14.0 0.3.

As you can see, it's not what I want/

1 Answer

+1 vote
answered Dec 4, 2020 by Peter Minarik (19,180 points)
selected Dec 4, 2020 by Miafina
 
Best answer

My Solution

I'm not 100% sure what you mean by "sort the array using the choice method".

I guess what you mean is the Selection Sort algorithm.

Someone else was doing all kind of sorting algorithms and I did my own version for him. You can find various sorting algorithms implemented here.

I'll put the Selection Sort down here as well:

void SelectionSort(int a[], int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        int min = i;
        for (int j = i + 1; j < size; j++)
        {
            if (a[min] > a[j])
                min = j;
        }
        Swap(a[i], a[min]);
    }
}

You'll have to do a bit of work as my code works with integral numbers. You need to change it here and there to work with floats instead. After that you have to do call this method where you want to sort your array.

Your Solution

The Problems

Wrong Formatting

Your code is a bit hard to read as indentations are a bit messy. You should really stick to the same indentation for every level of the code.
See how the "if(i!=max){" is indented more than "if(a[j]>a[max]){", although they should be on the same level?

Swapping In Every Step

Your code swap the value of the maximum an the ith index in every iteration of the for j loop. This piece of code should be outside of this loop, in the for i loop.

The Fix

Below is your code reformatted and the swap logic moved to the right place:
#include <stdio.h>

int main()
{
    float a[15] = { 2, 6, 7.5, 4.1, 5, 2.3, 6.2, 3, 1.5, 4.9, 0.3, 12, 3, 14, 1.4 };
    int max = 0;
    float buf = 0;
    for (int i = 0; i < 15; i++)
    {
        max = i;
        for (int j = i + 1; j < 15; j++)
        {
            if (a[j] > a[max])
            {
                max=j;  
            }
        }

        if (i != max)
        {
            buf = a[i];
            a[i] = a[max];
            a[max] = buf;   
        }
    }
    
    for (int i = 0; i < 15; i++)
    {
        printf("%.1f ", a[i]);  
    }
    return 0;
}
commented Dec 4, 2020 by Miafina (900 points)
Thank you Peter! Now I understand this algorithm much better)))
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...