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