1. Introduction:
in the process of learning skills, no one technology is the best! In different situations, different technologies will have different advantages!
The basic idea of bubble sorting is to compare two adjacent elements each time, and swap them if they are in the wrong order:
- Let's take a look at "bubble sort" by simply drawing
The so-called bubble sort is this, the small number to float, the big number to sink!
2. Bubble Sort:
1 Static voidMain (string[] args)2 {3 int[] arr = {1,5,2,7,3 };4 foreach(intIteminchBubble (arr))5 {6 Console.WriteLine (item);7 }8 Console.readkey ();9 }
Ten Public Static int[] Bubble (int[] arr) One { A intTemp//temporary variables for swapping - for(inti =0; I < arr. length-1; i++)//According to Figure 1, a total comparison of N-1 wheel - for(intj =1; J < arr. Length-i; J + +)//As shown in Figure 1, after each round of comparison, you can compare less once the { - if(Arr[j-1] >Arr[j]) - { -temp = arr[j-1]; +Arr[j-1] =Arr[j]; -ARR[J] =temp; + } A } at returnarr; -}
Figure 1
In the process of data processing, bubble sort with more! Next you can see the simple sort (ii)--(bucket sort)
Three simple sort (one)--(bubble sort)