0 Basics C# Notes 06: Bubble Sort


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 bubble sort today


1. The principle of bubble sort

Bubble sorting is a relatively simple sorting algorithm, which is called Bubble Sort in English. It traverses all the data, and compares adjacent elements each time. If the order is inconsistent with the predetermined order, the position is exchanged; such a traversal will float the largest or smallest data to the top, and then repeat the same operation until all the data is in order.
insert image description here

2. Programming

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("===========================================");
    for (int k = arr.Length - 1; k > i; k--)
    {
    
    
        Console.Write($"第{
      
      arr.Length - k}次");
        if (arr[k] < arr[k - 1])
        {
    
    
            int temp = arr[k];
            arr[k] = arr[k - 1];
            arr[k - 1] = temp;
        }
        arr.ToList().ForEach(x => Console.Write(x + " "));
        Console.WriteLine();
    }
    Console.WriteLine("===========================================");
    Console.Write($"第{
      
      i+1}轮:");
    arr.ToList().ForEach(x => Console.Write(x + " "));
}
Console.WriteLine();
arr.ToList().ForEach(x => Console.Write(x + " "));

print result

1 2 4 3 28 23 12 14 99 34 55
===========================================1:1 2 4 3 28 23 12 14 99 34 552:1 2 4 3 28 23 12 14 34 99 553:1 2 4 3 28 23 12 14 34 99 554:1 2 4 3 28 23 12 14 34 99 555:1 2 4 3 28 12 23 14 34 99 556:1 2 4 3 12 28 23 14 34 99 557:1 2 4 3 12 28 23 14 34 99 558:1 2 3 4 12 28 23 14 34 99 559:1 2 3 4 12 28 23 14 34 99 5510:1 2 3 4 12 28 23 14 34 99 55
===========================================1:1 2 3 4 12 28 23 14 34 99 55
===========================================1:1 2 3 4 12 28 23 14 34 55 992:1 2 3 4 12 28 23 14 34 55 993:1 2 3 4 12 28 23 14 34 55 994:1 2 3 4 12 28 14 23 34 55 995:1 2 3 4 12 14 28 23 34 55 996:1 2 3 4 12 14 28 23 34 55 997:1 2 3 4 12 14 28 23 34 55 998:1 2 3 4 12 14 28 23 34 55 999:1 2 3 4 12 14 28 23 34 55 99
===========================================2:1 2 3 4 12 14 28 23 34 55 99
===========================================1:1 2 3 4 12 14 28 23 34 55 992:1 2 3 4 12 14 28 23 34 55 993:1 2 3 4 12 14 28 23 34 55 994:1 2 3 4 12 14 23 28 34 55 995:1 2 3 4 12 14 23 28 34 55 996:1 2 3 4 12 14 23 28 34 55 997:1 2 3 4 12 14 23 28 34 55 998:1 2 3 4 12 14 23 28 34 55 99
===========================================3:1 2 3 4 12 14 23 28 34 55 99
===========================================1:1 2 3 4 12 14 23 28 34 55 992:1 2 3 4 12 14 23 28 34 55 993:1 2 3 4 12 14 23 28 34 55 994:1 2 3 4 12 14 23 28 34 55 995:1 2 3 4 12 14 23 28 34 55 996:1 2 3 4 12 14 23 28 34 55 997:1 2 3 4 12 14 23 28 34 55 99
===========================================4:1 2 3 4 12 14 23 28 34 55 99
===========================================1:1 2 3 4 12 14 23 28 34 55 992:1 2 3 4 12 14 23 28 34 55 993:1 2 3 4 12 14 23 28 34 55 994:1 2 3 4 12 14 23 28 34 55 995:1 2 3 4 12 14 23 28 34 55 996:1 2 3 4 12 14 23 28 34 55 99
===========================================5:1 2 3 4 12 14 23 28 34 55 99
===========================================1:1 2 3 4 12 14 23 28 34 55 992:1 2 3 4 12 14 23 28 34 55 993:1 2 3 4 12 14 23 28 34 55 994:1 2 3 4 12 14 23 28 34 55 995:1 2 3 4 12 14 23 28 34 55 99
===========================================6:1 2 3 4 12 14 23 28 34 55 99
===========================================1:1 2 3 4 12 14 23 28 34 55 992:1 2 3 4 12 14 23 28 34 55 993:1 2 3 4 12 14 23 28 34 55 994:1 2 3 4 12 14 23 28 34 55 99
===========================================7:1 2 3 4 12 14 23 28 34 55 99
===========================================1:1 2 3 4 12 14 23 28 34 55 992:1 2 3 4 12 14 23 28 34 55 993:1 2 3 4 12 14 23 28 34 55 99
===========================================8:1 2 3 4 12 14 23 28 34 55 99
===========================================1:1 2 3 4 12 14 23 28 34 55 992:1 2 3 4 12 14 23 28 34 55 99
===========================================9:1 2 3 4 12 14 23 28 34 55 99
===========================================1:1 2 3 4 12 14 23 28 34 55 99
===========================================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. Stable sorting 4. In-place sorting

Bubble sort optimization

if (arr == null || arr.length < 2) {
    
    
    return arr;
}
int n = arr.length;
for (int i = 0; i < n; i++) {
    
    
    boolean flag = true;
    for (int j = 0; j < n -i - 1; j++) {
    
    
       if (arr[j + 1] < arr[j]) {
    
    
           flag = false;
           int t = arr[j];
           arr[j] = arr[j+1];
           arr[j+1] = t;
       }
   }
  //一趟下来是否发生位置交换
   if(flag)
      break;
}

Guess you like

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