0 Basics C# Notes 07: Selection Sort Method


foreword

There are ten commonly used sorting methods, namely:

  • Bubble Sort;
  • selection sort ;
  • insertion sort;
  • Hill sort;
  • merge sort;
  • quick sort;
  • heap sort;
  • count sort;
  • bucket sort;
  • Radix sort.

Let's talk about selection sorting today

1. The principle of selection sorting

First, find the smallest element in the array, and second, swap it with the first element of the array (if the first element is the smallest element then it swaps itself). Second, find the smallest element among the remaining elements and swap it with the second element of the array. And so on, until the entire array is sorted. We call this method selection sort.
Selection sort is actually an optimization of bubble sort.
insert image description here

2. Use steps

 int[] arr = {
    
     1, 2, 4, 3, 28, 23, 12, 14, 99, 34, 55 };

 arr.ToList().ForEach(x => Console.Write(x + " "));

 for (int i =0;i<arr.Length-1;i++)
 {
    
    
     Console.WriteLine();
     
     Console.WriteLine("===========================================");
     int index = i;
     for(int j = i + 1; j < arr.Length; j++)
     {
    
    
         Console.WriteLine($"第{
      
      j-i}次比较");
         if (arr[index] > arr[j])
         {
    
    
             
             index = j;
         }
     }

     if(index!=i)
     {
    
    
         var temp = arr[i];
         arr[i] = arr[index];
         arr[index] = temp;
     }
     Console.WriteLine("===========================================");
     Console.Write($"第{
      
      i + 1}轮:");
     arr.ToList().ForEach(x => Console.Write(x + " "));
 }
 Console.WriteLine();
 Console.WriteLine("===========================================");
 arr.ToList().ForEach(x => Console.Write(x + " "));

3. Print the result

1 2 4 3 28 23 12 14 99 34 55
===========================================1次比较2次比较3次比较4次比较5次比较6次比较7次比较8次比较9次比较10次比较
===========================================1:1 2 4 3 28 23 12 14 99 34 55
===========================================1次比较2次比较3次比较4次比较5次比较6次比较7次比较8次比较9次比较
===========================================2:1 2 4 3 28 23 12 14 99 34 55
===========================================1次比较2次比较3次比较4次比较5次比较6次比较7次比较8次比较
===========================================3:1 2 3 4 28 23 12 14 99 34 55
===========================================1次比较2次比较3次比较4次比较5次比较6次比较7次比较
===========================================4:1 2 3 4 28 23 12 14 99 34 55
===========================================1次比较2次比较3次比较4次比较5次比较6次比较
===========================================5:1 2 3 4 12 23 28 14 99 34 55
===========================================1次比较2次比较3次比较4次比较5次比较
===========================================6:1 2 3 4 12 14 28 23 99 34 55
===========================================1次比较2次比较3次比较4次比较
===========================================7:1 2 3 4 12 14 23 28 99 34 55
===========================================1次比较2次比较3次比较
===========================================8:1 2 3 4 12 14 23 28 99 34 55
===========================================1次比较2次比较
===========================================9:1 2 3 4 12 14 23 28 34 99 55
===========================================1次比较
===========================================10:1 2 3 4 12 14 23 28 34 55 99
===========================================
1 2 3 4 12 14 23 28 34 55 99

Summarize

Properties: 1. Time complexity: O(n2) 2. Space complexity: O(1) 3. Unstable sorting 4. In-place sorting

Guess you like

Origin blog.csdn.net/BeanGo/article/details/131856528