[Data structure and algorithm] Bubble Sorting

Source: Internet
Author: User

A sorting algorithm is a basic and common algorithm. Sorting algorithms are classified into internal sorting and external sorting.

Internal sorting means that the entire sorting process can be completed without accessing external storage.

External sorting means that when the party sorts a large volume of data, it is impossible to load all the data into the memory at a time. It can only read a part of the data from the external memory to the memory. After sorting the data in the memory, store the data in the external store, read the next part of the data from the external store to the memory, and merge and sort the sorted sub-parts.

Bubble Sorting is a sort method for adjacent data exchanges. The characteristics of Bubble sorting are that n elements need to be scanned for n-1 times, so sometimes the data may be scanned once or twice before sorting is completed, and subsequent scanning is obviously a waste of computing resources, so you can improve the Bubble Sorting by using the following code:

Public class bubblesort {public void bubblesort (INT [] In) {Boolean isbubble = false; int inlength = in. length; For (INT I = 0; I <inlength; I ++) {system. out. print ("no." + I + "Times:"); For (int K: In) {system. out. print (K + "");} system. out. println (); For (Int J = inlength-1; j> I; j --) {If (in [J] <in [J-1]) {isbubble = true; int TMP = in [J]; In [J] = in [J-1]; In [J-1] = TMP ;}} if (! Isbubble) {break;} else {isbubble = false ;}} public static void main (string [] ARGs) {bubblesort mbubblesort = new bubblesort (); int [] caseone = {6, 5, 4, 3, 2, 1, 10, 2}; int [] casetwo = {1, 6, 5, 2, 4, 3}; mbubblesort. bubblesort (caseone); For (int I: caseone) {system. out. print (I + "");} system. out. println (); mbubblesort. bubblesort (casetwo); For (int I: casetwo) {system. out. print (I + "");}}}



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.