Java Bubble sort

Source: Internet
Author: User

Package WMK;

Import Java.util.Scanner;

public class Maopaopaixu {


Public Maopaopaixu () {}

public void Maxtomin () {
Get console input with scanner
Scanner sc = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please input String:");
String s = sc.nextline ();
int len = S.length ();
int ints[] = new Int[len];
into an array
for (int i = 0; i < ints.length; i++) {
Ints[i] = integer.valueof (s.substring (i, i + 1));
}

Core code
Method One: The comparison of two adjacent elements, to the tail of the closer
for (int i = 0; i < ints.length-1; i++) {

for (int j = i + 1; j < Ints.length; J + +) {
if (Ints[i] > Ints[j]) {
int tem;
tem = Ints[i];
Ints[i] = Ints[j];
INTS[J] = tem;
}
}

}

Method Two: A comparison of two elements in the tail and the middle
for (int i = 0; i < ints.length-1; i++) {

for (int j = ints.length-1; j > i; j--) {
if (Ints[i] > Ints[j]) {
int tem;
tem = Ints[i];
Ints[i] = Ints[j];
INTS[J] = tem;
}
}

  }

for (int i = 0; i < ints.length; i++) {
System.out.print (Ints[i]);
}

}

public static void Main (string[] args) {
Maopaopaixu m = new Maopaopaixu ();
M.maxtomin ();
}

}

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.