Selection sort algorithm C# and C (Ascending and Descending order)

Lionsure 2020-07-04 Original by the website

If you only sort some elements of the array(for example, only the top 100), the selection sort algorithm is a good choice. It puts the smallest(or largest) element behind the sorted element every cycle(outer loop). If you only sort the top 100 with the largest value, the outer loop only needs to be looped 100 times, which can improve the efficiency of program.

 

I. The basic idea of the selection sorting algorithm

Select sort selects the smallest(or largest) element each cycle(outer loop) and puts it behind the sorted elements until all elements are sorted.

At the beginning of each outer loop, the first element of array(or list) to be sorted is used as a reference, and the inner loop is used for comparison. If the first element is greater than the following elements(if (arr[j] < arr[index])), then write down the index of the following elements(index = j); after the inner loop ends, if the smallest element is not the first element of array to be sorted (if(index != i)), then exchange.

 

 

II. Selection sort algorithm C# (Ascending order)

/// <summary>
       /// Selection sort
       /// </summary>
       /// <param name="arr">Array to be sorted</param>

       private void SelectSort(int[] arr)
       {
              int len = arr.Length;
              int temp,index;
              for (int i = 0; i < len; i++)
              {
                     index = i;
                     for (int j = i + 1; j < len; j++)
                     {
                            if (arr[j] < arr[index])
                            index = j;
                     }

              if (index != i)
                     {
                            temp = arr[i];
                            arr[i] = arr[index];
                            arr[index] = temp;
                     }
              }
       }

 

Call:

int[] a = new int[] { 6, 2, 91, 38, 10, 45, 178, 27 };
       SelectSort(a);

foreach (int n in a)
       label.Text += n.ToString() + " ";

Output result: 2 6 10 27 38 45 91 178

 

 

 

III. Selection sort algorithm C# (Descending order)

Writing the ascending sort order is equivalent to writing the descending sort order, because the sorting code is exactly the same, just change the < to > in if(arr[j] < arr[index]), the code is as follows:

/// <summary>
       /// Selection sort
       /// </summary>
       /// <param name="arr">Array to be sorted</param>

       private void SelectSort(int[] arr)
       {
              int len = arr.Length;
              int temp,index;
              for (int i = 0; i < len; i++)
              {
                     index = i;
                     for (int j = i + 1; j < len; j++)
                     {
                            if (arr[j] > arr[index])
                            index = j;
                     }

              if (index != i)
                     {
                            temp = arr[i];
                            arr[i] = arr[index];
                            arr[index] = temp;
                     }
              }
       }

 

Call:

int[] a = new int[] { 6, 2, 91, 38, 10, 45, 178, 27 };
       SelectSort(a);

foreach (int n in a)
       label.Text += n.ToString() + " ";

Output result: 178 91 45 38 27 10 6 2

 

 

 

IV. Algorithm for selection sort in c programing (Ascending order)

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

/* arr is an array to be sorted, len is the length of array */
       void SelectSort(int* arr, int len)
       {
              int temp,index;
              for (int i = 0; i < len; i++)
              {
                     index = i;
                     for (int j = i + 1; j < len; j++)
                     {
                            if (arr[j] < arr[index])
                            index = j;
                     }

              if (index != i)
                     {
                            temp = arr[i];
                            arr[i] = arr[index];
                            arr[index] = temp;
                     }
              }
       }

void main ()
       {
              int a[8] = {6, 2, 91, 38, 10, 45, 178, 27};
              SelectSort(a, 8);

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

Output result: 2 6 10 27 38 45 91 178

 

 

 

V. Algorithm for selection sort in c programing (Descending order)

In descending order, just change the < to > in "if(arr[j] < arr[index])", the code is as follows:

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

/* arr is an array to be sorted, len is the length of array */
       void SelectSort(int* arr, int len)
       {
              int temp,index;
              for (int i = 0; i < len; i++)
              {
                     index = i;
                     for (int j = i + 1; j < len; j++)
                     {
                            if (arr[j] > arr[index])
                            index = j;
                     }

              if (index != i)
                     {
                     temp = arr[i];
                     arr[i] = arr[index];
                     arr[index] = temp;
                     }
              }
       }

void main ()
       {
              int a[8] = {6, 2, 91, 38, 10, 45, 178, 27};
              SelectSort(a, 8);

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

Output result: 178 91 45 38 27 10 6 2