Bubble algorithm of sorting algorithm

Source: Internet
Author: User

First, the principle

(1) Compare the two adjacent elements. If the first one is bigger than the second one, swap them.

(2) for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number.

(3) Repeat the above steps for all elements except the last one.

(4) Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.

Second, the realization

Package com.example.sort;/** *  * @author Binchao *  */public class Bubblesort {public static void main (string[] Arg s) {//TODO auto-generated method stubint[] ar = {5, 58};ar = Bubblesort (AR); for (int i:ar) {System.out.pri NT (i + "");}}  /** * Bubble Sort * @param arr * @return */public static int[] Bubblesort (int[] arr) {int temp;for (int i = 0; i < arr.length; i++) {for (int j = 0; J < Arr.length-1; J + +) {//small float if (arr[j] > arr[j+1]) {temp = Arr[j];arr[j] = arr[j+1];arr[ J+1] = temp;}}} return arr;}}

Bubble algorithm of sorting algorithm

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.