Bubbling sort Improved (Java)

Source: Internet
Author: User

Bubble sort

Bubble sort (bubble sort) is a sort of adjacent data exchange, which is simple and therefore an introductory case for sorting algorithms.

(1) The basic idea: in order to sort a group of numbers, the current is not yet ranked in the range of all the number, from top to bottom of the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.

(2) Example:

Package practice; Import Java.util.scanner;public class pseudo sort {public static int a[];//public static string b;public static string E= ""; public static String [] bb;static int c[]=new int[100];p ublic static void Main (string[] args) {Scanner s = new Scanner (s  ystem.in);  String str = NULL;        String str1= "";       System.out.println ("Please enter the string you want to enter:");       str = S.nextline (); System.out.println ("Please enter the number of inputs prepared");//Scanner scanner=new Scanner (system.in);//int n=scanner.nextint ();//System . OUT.PRINTLN ("number");////for (int i=1;i<=n;i++) {//Stringb=scanner.nextline ();//StringBuffer sb=new StringBuffer      (b);///}//System.out.println ("The string you entered is" +SB); Bb=str.split ("");///System.out.println (d.tostring ());//for (int i=0;i<d.length;i++) {////system.out.println (d [i]);    /str1+=d[i];//}///System.out.println (STR1); for (int i=0;i<bb.length;i++) {c[i]=integer.parseint (bb[i]);//C[i]=integer.parseint ((D[i]);} Bubblesort (); BubbleSort1 ();//Improved bubble sort for (int i=0;i<bb.length;i++) {System.out.print (c[i]+ "");}} private static void BubbleSort1 () {boolean flag=false;for (int i=0;i<bb.length;i++) {for (int j=i+1;j<bb.length;j+ +) {if (c[i]>c[j]) {int temp=c[i];c[i]=c[j];c[j]=temp;flag=true;} if (Flag==false) {break;} Elseflag=false;}}} public static void Bubblesort () {-(int i=0;i<bb.length;i++) {for (int j=i+1;j<bb.length;j++) {if (C[i]>c[j]) { int temp=c[i];c[i]=c[j];c[j]=temp;}}}}

One of the improved version of bubble sorting idea:

It is assumed that when the nth-pass data has been sequenced no longer needs to be changed, and the data will not change when scanning n+1. Therefore, you can set an identity variable flag to set its value to False before each scan. Set to True if there is data exchange. After scanning the value of the flag to determine, if false, indicating that there is no data exchange, the data has been sorted in order, there is no need for a subsequent scan.

 

Results:

Please enter the string you want to enter:
3 2 1
1 2 3

Bubbling sort Improved (Java)

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.