Algorithm Learning (Java language description)--bubble Sort algorithm __ Code

Source: Internet
Author: User

Bubble sort (Bubble sort) is a sort of interchange, the basic idea : 22 compare the keywords of adjacent records, if the reverse order is exchanged, until there is no reverse sequence of records.


The following is the code for the implementation:


public class Bubblesort {public
	
	static void (int arr[], int x, int y) {
		int tmp = arr[x];
		ARR[X] = Arr[y];
		Arr[y] = tmp;
	}

	public static void Bubblesort (int arr[]) {
		Boolean flag = true;
		for (int i=0;i<arr.length && flag;i++) { 	//If flag is true, exit loop
			flag = false;
			for (int j=arr.length-2;j>=i;j--) {	//j from back-forward loop
				if (Arr[j] > Arr[j+1]) {	//the previous one is greater than
					the last swap (arr, J, j+1);	The value of the Exchange Arr[j] and arr[j+1]
					flag = true;		If there is an Exchange flag set to True}}}
		
	


Test:

public static void Main (string[] args) {
		//TODO auto-generated method stub
		int a[] = {2, 5, 7, 4, 1, 9, 0, 3, 6 , 8};
		Bubblesort (a);
		for (int i=0;i<a.length;i++) {
			System.out.print (a[i]+ "");
		}
		System.out.println ();
	}

Results:



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.