Bubble sort of Java basics

Source: Internet
Author: User

Bubble sort
    • Basic introduction

Bubble sort is the comparison of two adjacent elements, through the constant comparison, the larger number to sink, smaller to the top, which is the source of bubble sort name
The first trip starts with a number labeled 0 in the array, arr[0] greater than arr[1] to exchange their position, continue to compare arr[1] and arr[2], until Arr[n]
The second trip starts with a number labeled 1 in the array, arr[1] greater than arr[2] to continue to swap positions, continue to compare arr[2] and arr[3], until Arr[n]
And so on

    • Algorithm introduction (subsequent additions)

    • Performance Introduction (Subsequent additions)

    • Introduction to Suboptimal (Follow-up supplement)

    • Code presentation (for example from small to large)

package com.nbj.test;/***冒泡排序*/public class BubbleSort{    public static void main(String [] args){        int[] arr= {1,22,32,2,5,75,46,37,23,99};        for (int i = 0; i < arr.length-1;i++){            for (int j = 0; j < arr.length-1-i;j++){                if(arr[j] > arr[j+1]){                    arr[j+1] = arr[j] + arr[j+1];                    arr[j] = arr[j+1] - arr[j];                    arr[j+1] = arr[j+1] -arr[j];                }            }        }        for(int i =0 ;i < arr.length; i++){            System.out.println(arr[i]);        }    }}

Bubble sort of Java basics

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.