Java Bubble sort

Source: Internet
Author: User

The code is as follows:

public class Bubblesort {public static void Bubblesort (Datawrap [] data) {System.out.println ("start sort"); int length = Data.len gth;for (int i = 0;i < length-1;i++) {Boolean flag = false;for (int j = 0;j < length-1-i;j++) {//If the element at J Index is greater than the element indexed at j+1 if ( Data[j].compareto (data[j+1]) >0) {//exchange them datawrap Tep = data[j+1];d ata[j+1] = data[j];d ata[j] = Tep;flag = true;}} SYSTEM.OUT.PRINTLN (data);//If the interchange does not occur, it indicates that it is in an ordered state if (!flag) {break;}}} public static void Main (string[] args) {datawrap[] data = {new Datawrap (""), New Datawrap (""), new Datawrap (49, ""), New Datawrap (""), new Datawrap (+, ""), new Datawrap (9, "")}; System.out.println ("before sorting: \ n" + java.util.Arrays.toString (data)); Bubblesort (data); System.out.println ("After sorting: \ n" + java.util.Arrays.toString (data));}}

  

Java Bubble sort

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.