Three simple sort (one)--(bubble sort)

Source: Internet
Author: User

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:

    • Code:

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; -}

    • Bubble Comparison graph:

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)

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.