A simple sort of bubble sort

Source: Internet
Author: User

A simple sort of bubble sort

The bubbling sort algorithm runs very slowly, conceptually it is the simplest of the sorting algorithms, so the bubbling sort algorithm

It's a very good algorithm to start with when sorting techniques are being researched.

Use the bubble sort algorithm to sort the baseball team players

If a person is myopic like a computer program, he can only see the two neighboring baseball players standing in front of him. In this difficult situation, how to rank the players. Assume that there are N players, and each player is numbered from left to right, from 0 to N-1, depending on the location of the station.

The bubbling sort routine performs the following: Starting from the leftmost side of the queue, compare the players at position No. 0 and location 1th. If the player on the left (number No. 0) is high, let two players swap. If the players on the right are tall, they do nothing. Then move right one position and compare the players at position 1th and number 2nd. And just like, if the player on the left is high, then two players exchange positions, the specific sort process as shown:

Here are the rules to follow:

1. Comparison of two players.

2. If the player on the left is high, then two players swap positions.

3. Move one position to the right and compare the following two players.

Follow this queue and compare it to the far right side of the queue. Although not all of the players have been sequenced, the top players have indeed been ranked on the far right. This is OK, because each time you compare two players, you will be able to swap his or her position with the highest player until he or she reaches the far right of the queue. This algorithm is called the cause of bubble ordering: Because the largest data item always "Bubbles" to the top of the array when the algorithm executes. The following figure is the arrangement of the players after the first trip.


4. Then when you hit the first scheduled player, return to the leftmost queue and start the next sequence again. Continue the process until all the players have been scheduled to do so.

The following are the specific code implementations of the algorithm:

public void Bubblesort (int[] arry) {
	int temp = 0;
	for (int i = 0; i < arry.length; ++i) {
		//Because each sort is sorted once, the top of the array determines a position, that is, each bubble once
		///The next time is only ordered
		in the array (Arry.length-i-1) for (int j = 0; J < arry.length-i-1; ++j) {
			//swap locations, sort large numbers backwards                       if (Arry[j] > Arry[j+1]) {
				temp = Arry [j];
				ARRY[J] = arry[j+1];
				ARRY[J+1] = temp;}}
	}
	Print (arry);//PrintOut
}


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.