Bubble sort Algorithm (JAVA) __c language

Source: Internet
Author: User

Bubble Sort Algorithm (JAVA)

I. Overview

The day before yesterday, someone asked the next sorting algorithm implementation, so draw time to write a bubble sorting algorithm. The bubble sort algorithm works by starting with the first number, then comparing the current number with each subsequent number, and swapping the current two digits if the current number is greater than any of the following digits. The bubble sort algorithm can be used more efficiently when the data is small (10000 lengths of the shape array may be sorted by about 351ms), but the efficiency of the bubble sort is relatively low when the array is long, and can be used for fast or merge.

Second, the code

The key code for the bubble algorithm is as follows:

Package com.csdn.algorithm;

Import Java.util.Random;  /** * Test Bubbling sort * * @author Administrator * */public class Testbubblesort {/** * * @param args/public static
		void Main (string[] args) {//1. Construct data Random arandom = new Random ();
		int narraylenght = 60000;
		int[] Adata = new Int[narraylenght];
		for (int i = 0; i < adata.length i++) {Adata[i] = Arandom.nextint (10000);
		//2. Sort Long lstarttime = System.currenttimemillis ();
		int[] Asortresult = Bubblesort (adata);

		Long lendtime = System.currenttimemillis ();

		3. Print sort when System.out.println ("+ Narraylenght +"] number, spents "+ (Lendtime-lstarttime) +" MS ");
		3. Print result//for (int i = 0; i < asortresult.length; i++) {//System.out.println (Asortresult[i]); /** * Bubbles the array of incoming plastic arrays in ascending order <BR/> * Algorithm complexity: O (n^2) <BR/> * An array with less than 10,000 data will not time out (about a second) <br/&gt
	 ; * But if the larger need to use the fast row or merge O (n*log2 (n)) * * @param _aintegerarray * @return/private static int[] BubbLesort (int[] _aintegerarray) {//1. Parameter check if (_aintegerarray = = null) {return _aintegerarray; //2. Start with the first number, and then compare the previous number with the following number, take a small value for (int i = 0; i < _aintegerarray.length; i++) {for (int j = i + 1; j < _aintegerarray.length;
					J + +) {if (_aintegerarray[i] > _aintegerarray[j]) {//Exchange two number int ntemp = _aintegerarray[i];
					_aintegerarray[i] = _aintegerarray[j];
				_AINTEGERARRAY[J] = ntemp;
	}}//3. Return value returns _aintegerarray;
 }

}


Iii. Execution Time Statistics

Number of sort [10], spents 0ms

Number of sort [100], spents 1ms

Number of sort [1000], spents 3ms

Number of sort [10000], spents 351ms

Number of sort [20000], spents 1326ms

Number of sort [30000], spents 2714ms

Number of sort [60000], spents 9054ms

Other lengths of array sorting time can be tested by program

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.