Data structure-bubble sort

Source: Internet
Author: User

The bubbling sort algorithm is the simplest of the sorting methods, but it is very slow to run, so the bubbling sort is a very good algorithm to start the sorting study.

Bubble sort after the first round of sorting (from small to large), although the data can not be sorted in order, but the largest data to the last, then we in the next few rounds, followed by the maximum value, until there is only one data, at this time, the data sorting is complete:

The following example:

6,12,16,7,5;

After the first round: 6,12,7,5,16; You only need to compare the first four data

After the second round: 6,7,5,12,16;

After the third round: 6,5,7,12,16;

After the fourth round: 5,6,7,12,16;

The sorting is complete at this time.

Java code

public static void Main (string[] args) {
		int b[]={6,12,1,16,7,5,1};
		Bubblesort (b);
		for (int i=0;i<b.length;i++) {
			System.out.print (b[i]+ "");
		}
	}
	public static void Bubblesort (int []b) {
		int temp=0;
		for
		(int i=b.length-1;i>0;i--) {
			//for
			(int j=0;j<i;j++) {
				if (b[j]>b[j+1]) {
					TEMP=B[J+1];
				    B[J+1]=B[J];
				    b[j]=temp;}}}}
	



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.