Sort algorithm 1-bubble sort

Source: Internet
Author: User

Now that we start to talk about sorting algorithms, the first and foremost must be familiar--------bubble sort

Although everyone began to learn the program contact the earliest, but also the simplest is the bubble sort, but still have a lot of work two or three years of people can not write a full bubble sort code, today let us no matter is new or old, let us start to warm and know new

Bubble Sort is a simple sort algorithm. 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 algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence. It's a good understanding of the literal meaning.

The bubbling sorting algorithm works as follows:

    1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
    2. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. When this is done, the final element will be the maximum number.
    3. Repeat the above steps for all elements, except for the last one.
    4. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

Because of its brevity, bubbling sequencing is often used to introduce the concept of algorithms to students who are getting started with programming.

The dynamic demo is as follows:

The C # code is as follows:

1  Public Static voidBubblesort (int[] intarray)2     {3         intTemp;4          for(intI=0; i<intarray.length-1;++i)5         {6              for(intj=0; j<intarray.length-1-i;++j)7             {8                 if(intarray[j]>intarray[j+1])9                 {TenTemp =Intarray[j]; OneINTARRAY[J] = intarray[j +1]; AIntarray[j +1] =Temp; -                 } -             } the         } -}

Sort algorithm 1-bubble sort

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.