Java bubble sort Implementation and formula __java

Source: Internet
Author: User

Video parsing https://edu.csdn.net/course/play/7813

When soldiers queue in a TV series, they are arranged in the order of tall and short, and the taller and taller are compared by 22.

Bubble Sort:

The basic idea is to repeat the order of the entire array, compare two elements at a time (22 sorted), and then swap them if they do not match, repeating this until the number of numbers is no longer needed (the end condition).

Because it is like a bubble, light bubbles will float up, in the process of continuous floating, 22 exchange process occurred, so called bubble sort.

/**
 * * 
 * @author Zhang Chenguang * * * *
/public class Sortdemo {
	//Common static member variable publicly
	-static int[]arr={ 5,2,6,7,3};
	
	public static int[] Sort (int[]arr) {
		int n=arr.length;
		int temp;
		for (int i=0;i<n;i++) {for
			(int j=0;j<n-1;j++) {
				if (arr[j]>arr[j+1]) {
					//exchange
					TEMP=ARR[J] ;
					ARR[J]=ARR[J+1];
					Arr[j+1]=temp
		}
		}} return arr;
	}
	
	/**
	 * Insert sort
	/public static void Insertsort () {
		
	}
	/**
	 * sequence output array element;
	 */public
	static void PrintArray (int []arr) {for
		(int num:arr) {
			System.out.print (num+ "\ t");
		}
		System.out.println ();
	}
	/**
	 * Main method */public
	static void Main (string[] args) {
		sortdemo.sort (arr);
		Sortdemo.printarray (arr);
	}

The mnemonic method of coping with interview sequencing:

n numbers to sort,

22 relatively small forward;

Outer loop n-1,

inner layer circulation n-1-9;

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.