C# implements selection sort

C# implements selection sort

Process dismantling

Suppose there is an existing array, as follows

insert image description here
The basic sorting code is as follows

static void Main(string[] args)
{
    
    
    int[] array = new int[] {
    
     3, 5, 6, 2, 3, 8, 1 };//替换代码
    BaseSort(array, 0, 6);//替换代码
    
    for (int i = 0; i < array.Length; i++)
    {
    
    
        Console.Write(array[i] + " ");
    }
    
    Console.WriteLine();
    Console.ReadKey();
}

public static void BaseSort(int[] array, int start, int end)
{
    
    
    int minIndex = start;
    
    for (int i = start + 1; i <= end; i++)
    {
    
    
        if (array[minIndex] > array[i])
        {
    
    
            minIndex = i;
        }
    }

    if (minIndex != start)
    {
    
    
        int temp = array[start];
        array[start] = array[minIndex];
        array[minIndex] = temp;
    }
}
  1. Replace the replacement code with the following code and output the array
int[] array = new int[] {
    
     3, 5, 6, 2, 3, 8, 1 };
BaseSort(array, 0, 6);
//以下标0的值为最小值,从下标 0 + 1 到下标6寻找唯一更小值,然后与最小值进行互换

insert image description here

  1. Replace the replacement code with the following code and output the array
int[] array = new int[] {
    
     1, 5, 6, 2, 3, 8, 3 };
BaseSort(array, 1, 6);
//以下标1的值为最小值,从下标 1 + 1 到下标6寻找唯一更小值,然后与最小值进行互换

insert image description here

  1. Replace the replacement code with the following code and output the array
int[] array = new int[] {
    
     1, 2, 6, 5, 3, 8, 3 };
BaseSort(array, 2, 6);
//以下标2的值为最小值,从下标 2 + 1 到下标6寻找唯一更小值,然后与最小值进行互换

insert image description here

  1. Replace the replacement code with the following code and output the array
int[] array = new int[] {
    
     1, 2, 3, 5, 6, 8, 3 };
BaseSort(array, 3, 6);
//以下标3的值为最小值,从下标 3 + 1 到下标6寻找唯一更小值,然后与最小值进行互换

insert image description here

  1. Replace the replacement code with the following code and output the array
int[] array = new int[] {
    
     1, 2, 3, 3, 6, 8, 5 };
BaseSort(array, 4, 6);
//以下标4的值为最小值,从下标 4 + 1 到下标6寻找唯一更小值,然后与最小值进行互换

insert image description here

  1. Replace the replacement code with the following code and output the array
int[] array = new int[] {
    
     1, 2, 3, 3, 5, 8, 6 };
BaseSort(array, 5, 6);
//以下标5的值为最小值,从下标 5 + 1 到下标6寻找唯一更小值,然后与最小值进行互换

insert image description here
So far, the values ​​of the array are arranged in ascending order.

Algorithm implementation

  1. Every time the selection algorithm is sorted, the smallest (largest) value will be selected and placed at the top . In total to select and put array.Length - 1 time.
  2. For the first selection, select subscript 0 as the current minimum (large) value , from subscript 0 + 1 to the end of the array subscript array.Length - 1 to find the minimum (large) value, if it is smaller (larger) than the current minimum value ) , the exchange is performed.
  3. For the second selection, select subscript 1 as the current minimum (large) value , from subscript 1 + 1 to the end of the array subscript array.Length - 1 to find the minimum (large) value, if it is smaller (larger) than the current minimum value ) , the exchange is performed.
  4. For the third selection, select subscript 2 as the current minimum (large) value , from subscript 2 + 1 to the end of the array subscript array.Length - 1 to find the minimum (large) value, if it is smaller (larger) than the current minimum value ) , the exchange is performed.

code show as below

static void Main(string[] args)
{
    
    
    int[] array = new int[] {
    
     3, 5, 6, 2, 3, 8, 1 };
    SelectionSort(array);
    
    for (int i = 0; i < array.Length; i++)
    {
    
    
        Console.Write(array[i] + " ");
    }
    
    Console.WriteLine();
    Console.ReadKey();

}

//选择排序
public static void SelectionSort(int[] array)
{
    
    
    for(int i = 0; i < array.Length - 1; i++)
    {
    
    
        int minIndex = i;
        for(int j = i + 1; j < array.Length; j++)
        {
    
    
            if(array[minIndex] > array[j])
            {
    
    
                minIndex = j;
            }
        }
        
        if(minIndex != i)
        {
    
    
            int temp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = temp;
        }
    }
}

Complexity and Stability

insert image description here

  • Optimal time complexity: the array has been sorted, but two for loops are still required for comparison
  • Worst time complexity: sorting the array in reverse order requires two for loops for comparison
  • Average time complexity: both the best and worst are O(n 2 ), so the average is also O(n 2 )
  • Space complexity: You need to use the temp variable to exchange two values ​​in the array.
  • Instability: After the selection algorithm, the following numbers are ranked first (for example: 3)

Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.

Guess you like

Origin blog.csdn.net/qq_46051312/article/details/124399922