# The sorting algorithm of data structure Java implementation (3)--Bubble sorting algorithm

Source: Internet
Author: User
Tags comparable sorts

Bubble sort is one of the simplest answers in exchange sequencing.

Sort Ascending:

`/** * Bubble Sort * ascending sort * * * @Overridepublic <t extends comparable<? Super t>> void Sortbyasc (t[] data) {if (data = = NULL | | data.length <= 1) {return;}  /** up to n-1 */for (int i = 0; i < data.length-1; i + +) {/** Sorts the current unordered interval, the range of J is in the Progressively shrinking */for (int j = 0; J < data.length -1-i; J + +) {if (Data[j].compareto (Data[j + 1]) > 0) {T temp = Data[j];d ata[j] = data[j + 1];d ata[j + 1] = temp;}}}}`

Sort Descending:

`/** * Bubble Sort * descending sort * * * @Overridepublic <t extends comparable<? Super t>> void Sortbydesc (t[] data) {if (data = = NULL | | data.length <= 1) {return;}  /** up to n-1 */for (int i = 0; i < data.length; i + +) {/** Sorts the current unordered interval, the range of J is in the Progressively shrinking */for (int j = 0; J < data.length-1 I J + +) {if (Data[j].compareto (Data[j + 1]) < 0) {T temp = Data[j];d ata[j] = data[j + 1];d ata[j + 1] = temp;}}}}`

The sorting algorithm of data structure Java implementation (3)--Bubble sorting algorithm

Related Keywords:

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.