Java and java

Source: Internet
Author: User

Java and java
Zookeeper

Bubble Sorting is the traversal every time. Compare adjacent numbers. The former is greater than the latter, and the maximum value is constantly moved back until it is sunk to the last position. The key point of the algorithm is to determine the boundary of each loop;

The latter two algorithms improve Bubble sorting to a certain extent, but compared with other sorting algorithms, Bubble sorting performance is still poor.

// Bubble sort public class Bubble_Sort {// the most primitive solution public void bubble_sort1 (int [] data) {int n = data. length; for (int I = 0; I <n; I ++) {// pay attention to the index range of the loop to avoid overflow for (int j = 0; j <n-I-1; j ++) {if (data [j]> data [j + 1]) {swap (data, j, j + 1) ;}}}// improve the algorithm by introducing a flag to determine whether a cycle has been moved. If there is no movement, it indicates that // sorting has been completed, public void bubble_sort2 (int [] data) {int n = data. length; boolean flag = true; // indicates whether int index is moved = n-1 ;// Indicates the index of the last digit of the loop. // once moving, the loop continues while (flag) {flag = false; for (int j = 0; j <index-1; j ++) {if (data [j]> data [j + 1]) {swap (data, j, j + 1); flag = true ;}} index --;} // improved algorithm 2: When a traversal is in progress, the last m bit is not converted, it indicates that the next n bits are larger than the current maximum number. // sort by bubble. Each time the values are sunk to the maximum value, the following bits must have sorted public void bubble_sort3 (int [] data) {int n = data. length; int index = n-1; while (index! = 0) {int k = 0; for (int j = 0; j <index-1; j ++) {if (data [j]> data [j + 1]) {swap (data, j, j + 1); k = j ;}} index = k ;}} // The reference implementation cannot be used like C ++, so you have to use the data array to change the private void swap (int [] data, int a, int B) {int temp = data [a]; data [a] = data [B]; data [B] = temp;} public void print_array (int [] data) {for (int num: data) {System. out. print (num); System. out. print ("") ;}} public static void main (String [] args) {Bubble_Sort bubble_Sort = new Bubble_Sort (); int data [] =, 5, 24, 57}; bubble_Sort.bubble_sort3 (data); bubble_Sort.print_array (data );}}

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.