C # Bubble sort

Source: Internet
Author: User

Bubble Sort Method:

1: Introduction:

Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.

It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.

The name of the algorithm derives from the fact that the larger element will slowly "float" to the top of the sequence by exchanging it.

2: Fundamentals:

The basic concept of bubble sorting (bubblesort) is to compare the number of adjacent two numbers in turn, placing the small number in front, and the larger number. Put it in the back. That is, in the first trip: first and second numbers are compared, small numbers are placed in front, and large numbers are behind. Then compare the second and third numbers, put the small number in front, put the large number behind, and continue until you compare the last two numbers, put the small number in front, and the large number in the back. At the end of the first trip, we put the biggest number in the end. In the second pass: The comparison is still starting from the first logarithm (since the first number is no longer less than the second number because it is possible to exchange the second and third numbers), the small number is placed in front, the large number is placed behind, it is compared to the second-to-last number (the penultimate position is already the largest), the second end Get a new maximum number in the penultimate position (actually the second largest number in the whole sequence). So go on, repeat the above process until the final sort is finished.

3: Basic algorithm:

Here we need to make a declaration in advance, we use the array to store data, do not start with subscript 0, so that, we can easily correspond to the comparison, the first data subscript is 1.

To store the data to be sorted into an array of a[1]-a[n], we assume that we need to arrange the data in a small to large order.

For example, now we need to sort the data 30,10,20,60,25,40.

First trip:

A comparison is made between the first element and the fifth element, followed by the adjacent data.

1: Compare the first data with the second one, and get the following exchange:

10,30,20,60,25,40

2: Compare the second data with the third one, and then get:

10,20,30,60,25,40

3: Compare the third data with the fourth one, and then get:

10,20,30,60,25,40

4: Compare the fourth data with the fifth data, and get the following exchange:

10,20,30,25,60,40

5: Compare the fifth data with the sixth data, and get the following exchange:

10,20,30,25,40,60

At this point, the first trip to compare the end of the exchange, the largest number 60 is already in position 6th.

The second trip and N will follow the first trip, but note that the nth number of the nth order is already nth, so the number of times per order will be reduced one time until the number of orders is only one time, see the code in detail.

4: Code:

Core code:

 Public int[]Pop(int[] listi) {//Array null throws an exception    if(Listi = =NULL)Throw NewArgumentNullException ("Listi");//Store temporary values that need to be bubbled    inttemp =0;//Traversal from the first value of the array to the second-lowest value     for(inti =0; I < listi.length-1; i++) {//From a value greater than I 1 to start traversing to the end        //Here The comparison is always greater than I value, because the previous value has been bubbling complete         for(intj = i +1; J < Listi.length; J + +) {//If the previous value is greater than the latter value, they swap position            if(Listi[i] > Listi[j]) {//Swap locationtemp = Listi[i];                 Listi[i] = Listi[j];             LISTI[J] = temp; }         }     }returnListi; }

Full code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.threading.tasks;namespace Bubble Sort Experiment {class Program {Static  Public int[]Pop_(int[] listi) {//Array null throws an exception            if(Listi = =NULL)Throw NewArgumentNullException ("Listi");//Store temporary values that need to be bubbled            inttemp =0;//Traversal from the first value of the array to the second-lowest value             for(inti =0; I < listi.length-1; i++) {//From a value greater than I 1 to start traversing to the end                //Here The comparison is always greater than I value, because the previous value has been bubbling complete                 for(intj = i +1; J < Listi.length; J + +) {//If the previous value is greater than the latter value, they swap position                    if(Listi[i] > Listi[j]) {//Swap locationtemp = Listi[i];                        Listi[i] = Listi[j];                    LISTI[J] = temp; }                }            }returnListi; }Static voidMain (string[] args) {int[] A = {3,5, at,6,3, the,4,8,5,8,3};foreach(intIinchPop_ (a)) Console.Write (i.tostring () +",");        Console.ReadLine (); }    }}

C # Bubble sort

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.