C Program to do selection sort program

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to perform selection sort in C?

In this c program, we need to use the selection sort to sort the elements. Selection sort is one of the simplest sorting techniques used in c programming. In selection sort, we have two sublists, one is sorted and the other needs to be sorted. At first, the sorted list is empty and unsorted and has all the elements, then begin sorting to find the lowest or highest element. Depending on the order, interchange the position of the lowest element with the leftmost element in the list. Now the sorted sublist has one element finally all elements get inside the sorted sub-list from the unsorted list. In this c program, we are using two functions, one to find the highest element and one to sort the elements in the order we need. The find max function finds the largest of the element in the array and we call the exchange array function to exchange the position.

The logic of this c program is, first read the count of numbers the user wants to sort. Then read the numbers into an array using for loop. Then display the input array elements. After that, we have to start the sorting process. To sort, we call the function exchange(). The exchange() function first finds out the biggest number from the array by calling another function findmax() and place the biggest number at the last position of the array. Now the last position of the array is sorted. Now we will continue the sorting process with the n-1 element of the array. If the second last element is found then we will repeat the process with n-2 elements and so on. At last, we get the smallest number at the top.
 

ALGORITHM

STEP 1:Include the header files to use the built-in header files into the C program.

STEP 2: Declare an array and a variable N. Array is used to store numbers and N is used to store the limit of Numbers.

STEP 3: Declare the functions int findmax(int b[10], int k) and void exchang(int b[10], int k).

STEP 4: Read the array elements into the array using for loop.

STEP 5: Display the array elements using for loop.

STEP 6: Call the function exchang(array, N) to sort the array.

STEP 7: Now display the Sorted array is as the array elements using for loop.

Function int findmax(int b[10], int k)

STEP 1: Declare the variables max,j, and set max=0.

STEP 2: Using a for loop with j=1 and the condition j<=k do step 3.

STEP 3: check b[j] > b[max] ,if it is true then max=j.Repeat the step 2

STEP 4: Return max.

Function int  void exchang(int b[10],int k)

STEP 1: Declare the variables temp,big,j.

STEP 2: Using a for loop with j=k-1 and the condition j>=1 do step 3.

STEP 3: Find out the position of the maximum number by calling findmax(b,j).

STEP 4:Now place the biggest element at the position of j by using the method of swapping.

STEP 5:Decrement j by 1 and check the condition j>=1 and repeat step 3.

C Source Code

                                          #include <stdio.h>


void main()
{
  int array[10];
  int i, j, N, temp;
  int findmax(int b[10], int k); /* function declaration */
  void exchang(int b[10], int k);
  printf("Enter the value of N\n");
  scanf("%d", & N);
  printf("Enter the elements one by one\n");
  for (i = 0; i < N; i++)
  scanf("%d", & array[i]);
  printf("Input array elements...\n");
  for (i = 0; i < N; i++)
  {
    printf("%d\n", array[i]);
  }

  /* Selection sorting begins */

  exchang(array, N);
  printf("Sorted array is...\n");
  for (i = 0; i < N; i++)
  {
    printf("%d\n", array[i]);
  }

} /* End of main*/

/* function to find the maximum value */

int findmax(int b[10], int k)
{
  int max = 0, j;
  for (j = 1; j <= k; j++)
  {
    if (b[j] > b[max])
    {
      max = j;
    }
  }
  return (max);
}
void exchang(int b[10], int k)
{
  int temp, big, j;
  for (j = k - 1; j >= 1; j--)
  {
    big = findmax(b, j);
    temp = b[big];
    b[big] = b[j];
    b[j] = temp;
  }
  return;
}
                                      

OUTPUT

Enter the value of N
5

Enter the elements one by one
45
12
90
33
78
Input array elements
45
12
90
33
78

Sorted array is
12
33
45
78
90