Sorting algorithm (selection + bubble + insertion sort C# code writing)

Sorting algorithm (selection + bubble + insertion sort C#)

1. Selection sort

Algorithm idea:
in an array array[N]
(1) find the smallest number in 0~N-1, put it in array[0]
(2) find the smallest number in 1~N-1, put it in array[1]
(3) Find the smallest number in 2~N-1, put it in array[2]
...
code implementation

public static void Selection(int[] arr)//选择算法,找到数组中最小值,放到[0],继续在剩下的数组元素中找到最小值,放到[1]。。。。。
{
    
    
    if (arr == null || arr.Length < 2)//筛选数组
    {
    
    
        return;
    }
    for (int i = 0; i < arr.Length - 1; i++)//注意,在对于最后一个数时不必再进行比较,如果要比较的话Swap函数就不可以用异或方式来写,会把值变成0
    {
    
    
        int minIndex = i;//初始化最小序号是i
        for (int j = i + 1; j < arr.Length; j++)
        {
    
    
            minIndex = arr[minIndex] > arr[j] ? j : minIndex;
        }
        Swap1(arr, minIndex, i);
    }
}
public static void Swap1(int[] arr, int minIndex, int i)//交换函数
{
    
    
    int mid = arr[minIndex];
    arr[minIndex] = arr[i];
    arr[i] = mid;
}

2. Bubble sort

Algorithm ideas
An array array[N]
(1) Compare the positions of array[0] and array[1]. array[0]>array[1], exchange the two numbers, otherwise do not move
(2) Then compare array[1] and array[2], until compare array[N-2] and array[N-1], this way After a trip, the number of array[N-1] is determined
(3) Next, from array[0] to array[N-2], array[N-2] is determined.
(4) array[0] to array[N-3], determine array[N-3].
...
code implementation

public static void Bubble(int[] arr)//冒泡排序法
{
    
    
    if (arr == null || arr.Length < 2)//筛选数组
    {
    
    
        return;
    }

    for (int i = arr.Length - 1; i > 0; i--)//交换的趟数
    {
    
    
        for (int j = 0; j < arr.Length - 1; j++)//j<arr.Length-1,防止数组越界,因为有j+1
        {
    
    
            if (arr[j] > arr[j + 1])//比较,大的数字后移,先确定的是最后的数字,即最大的数字
                Swap(arr, j, j + 1);
        }
    }
}
public static void Swap(int[] arr, int minIndex, int i)
{
    
    //这种交换方式虽然快但是容易出问题
    arr[minIndex] = arr[minIndex] ^ arr[i];
    arr[i] = arr[minIndex] ^ arr[i];
    arr[minIndex] = arr[minIndex] ^ arr[i];
}

Note: The swap used by the bubble sort method is different from the swap used by the selection sort above. This swap method uses an XOR operation. When using the swap exchange of the XOR operation, it is necessary to ensure that the two numbers exchanged are two independent areas, that is, the two points to the memory are two things, and it is not recommended to use it if it cannot be guaranteed.

3. Insertion sort

Algorithm idea:
an array array[N]
(1) Ensure the order between array[0]~array[0]
(2) Ensure the order between array[0]~array[1], if array[1]< array[0], swap. If array[1]>=array[0], the order between array[0]~array[1] is ensured...
(
3) The order between array[0] and array[m] is guaranteed [at this time 0 m- 1 The position is in order】
Looking from the array[m] to the left, if array[m]<array[m-1], exchange, compare array[m-1] and array[m-2] again, if array[m-1]<array[m-2], exchange, continue to compare. If array[m-1]>=array[m-2], then stop the comparison (because it has been determined that the numbers in positions 0~m-2 are in order), and array[0]~array[m is guaranteed at this time ] in order.
(4) Ensure that array[0]~array[3] are in order...

Code

public static void InsertSort(int[] arr)
{
    
    
    if (arr == null || arr.Length < 2)
    {
    
    
        return;
    }
    for (int i = 1; i < arr.Length; i++)//保证0——i有序
    {
    
    
        for (int j = i - 1; j >= 0 && arr[j] > arr[j+1]; j--)//若左边大于i,调换,不大于的话不用换,因为已经保证了前面是有序的,所以一但不大于则可以中止判断,不必再看看前面
        {
    
    
            Swap1(arr, j, j+1);
        }
    }
}
public static void Swap1(int[] arr, int minIndex, int i)//交换函数
{
    
    
    int mid = arr[minIndex];
    arr[minIndex] = arr[i];
    arr[i] = mid;
}

If you have any questions, please leave a message ( =•ω•= )m

Guess you like

Origin blog.csdn.net/weixin_51565051/article/details/129603564