JAVA Implementation of Bubble Sorting

Source: Internet
Author: User

Exchange sorting is a kind of sorting method by means of "Exchange". Its main idea is to select two records in the sequence to be sorted, compare their key codes, and exchange them if the reverse order is used.

Bubble Sorting: It is the simplest sorting method in exchange sorting. Its basic idea is to compare the key codes of adjacent records in pairs. If the reverse order is used, the key codes are exchanged until no reverse order is recorded.

Specific sorting process:

(1) the entire sequence of records to be sorted is divided into ordered and unordered areas. In the initial stage, the ordered area is empty, and the unordered area includes the entire record to be sorted.

(2) compare key codes of adjacent records in the forward and backward order of the unordered area. If the reverse order is used, the key code small record is moved forward, and the key code large record is moved back.

(3) Repeated execution (2), knowing that there is no reverse order record in the disordered area.


Package sort; import java. util. arrays;/*** Bubble Sorting ** @ author zhouhai **/public class BubbleSort {public static void main (String [] args) {// TODO Auto-generated method stub int a [] = new int [] {2, 3, 4, 1, 9, 34, 8, 7, 9 }; // define a test array sort (a); System. out. println (Arrays. toString (a);} public static void sort (int a []) {int len =. length; for (int I = 0; I <len; I ++) {for (int j = 0; j <len-i-1; j ++) {if (a [j]> a [j + 1]) {a [j] = a [j] + a [j + 1]; a [j + 1] = a [j]-a [j + 1]; a [j] = a [j]-a [j + 1] ;}}}


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.