Java Implementation of Bubble Sorting for sorting algorithms and java for sorting algorithms

Source: Internet
Author: User
Tags rounds

Java Implementation of Bubble Sorting for sorting algorithms and java for sorting algorithms

Bubble Sorting by Sorting Algorithm

1. Bubble Sorting is the basis for fast sorting, but the sorting speed is slow.
2. Basic Idea: Compare 1st elements in a sequence with 2nd elements. If the former is greater than the latter, the two elements are exchanged; otherwise, the two elements are not exchanged;
Then compare the 2nd elements with the 3rd elements. If the former is greater than the latter, the two elements are exchanged; otherwise, the two elements are not exchanged;
So forth, until the n-1 element is compared with the n element, if the former is greater than the latter, the two elements are exchanged; otherwise, the two elements are not exchanged;
After such sorting, the maximum values of n elements are placed at the nth position;
After that, the first n-1 elements are processed in the same process, so that the maximum value of the n-1 elements is placed at the n-1 position;
Then repeat the above process for the previous N-2 elements... until a certain sort process does not show the element exchange position action, sorting ends.

3. Process instance

 

The original array is:
--------------------------------------
[86, 2, 27, 65, 62, 71, 28, 38, 69, 56]
1st
[2, 27, 65, 62, 71, 28, 38, 69, 56, 86]
2nd
[2, 27, 62, 65, 28, 38, 69, 56, 71, 86]
3rd
[2, 27, 62, 28, 38, 65, 56, 69, 71, 86]
4th
[2, 27, 28, 38, 62, 56, 65, 69, 71, 86]
5th
[2, 27, 28, 38, 56, 62, 65, 69, 71, 86]
6th
[2, 27, 28, 38, 56, 62, 65, 69, 71, 86]
7th
[2, 27, 28, 38, 56, 62, 65, 69, 71, 86]
8th
[2, 27, 28, 38, 56, 62, 65, 69, 71, 86]
9th
[2, 27, 28, 38, 56, 62, 65, 69, 71, 86]
10th
[2, 27, 28, 38, 56, 62, 65, 69, 71, 86]
--------------------------------------
The sorted array is:
[2, 27, 28, 38, 56, 62, 65, 69, 71, 86]

 

I. Initial Bubble Sorting

Import java. util. Arrays;

/**
*
* @ Title BubbleSort
* @ Describe Bubble Sorting
* @ Author Zhang fuchang
* @ Date 3:56:30, January 1, October 1, 2016
*/
Public class BubbleSortLow {

Public static void main (String [] args ){
// Declare an integer Array
Int [] array = new int [10];
// Use loops and random numbers to initialize the Array
For (int I = 0; I <array. length; I ++ ){
Array [I] = (int) Math. round (Math. random () * 100 );
}
System. out. println ("original array :");
System. out. println ("--------------------------------------");
System. out. println (Arrays. toString (array ));
Array = bubbleSort (array );
System. out. println ("--------------------------------------");
System. out. println ("the sorted array is :");
System. out. println (Arrays. toString (array ));
}

/**
*
* Function: the basic idea of Bubble Sorting is to constantly compare two adjacent numbers, so that larger elements are constantly moved back. After a round of comparison, the maximum number is selected. After 2nd rounds of comparison, the maximum number is selected,
* And so on.
*
*
* Parameter: int [] array
*
* Return type: int []
*/
Private static int [] bubbleSort (int [] array ){
// Use a temporary array instead of the original array
Int [] arr = array;
For (int I = 0; I <arr. length; I ++ ){
System. out. println ("th" + (I + 1) + "");
For (int j = 0; j <arr. length-1-I; j ++ ){
If (arr [j]> arr [j + 1]) {
Int temp = arr [j];
Arr [j] = arr [j + 1];
Arr [j + 1] = temp;
}
}
// Print the array of each trip
System. out. println (Arrays. toString (arr ));
}
Return arr;
}

}

Ii. Improved Bubble Sorting

 

Import java. util. Arrays;

/**
*
* @ Title BubbleSort
* @ Describe Bubble Sorting
* @ Author Zhang fuchang
* @ Date 3:56:30, January 1, October 1, 2016
*/
Public class BubbleSortHigh {

Public static void main (String [] args ){
// Declare an integer Array
Int [] array = new int [10];
// Use loops and random numbers to initialize the Array
For (int I = 0; I <array. length; I ++ ){
Array [I] = (int) Math. round (Math. random () * 100 );
}
System. out. println ("original array :");
System. out. println (Arrays. toString (array ));
System. out. println ("--------------------------------------");

Array = bubbleSort (array );
System. out. println ("--------------------------------------");
System. out. println ("the sorted array is :");
System. out. println (Arrays. toString (array ));
}

 

/**
*
* Function: the basic idea of Bubble Sorting is to constantly compare two adjacent numbers, so that larger elements are constantly moved back. After a round of comparison, the maximum number is selected. After 2nd rounds of comparison, the maximum number is selected,
* And so on.
*
*
* Parameter: int [] array
*
* Return type: int []
*/
Private static int [] bubbleSort (int [] array ){
// Use a temporary array instead of the original array
Int [] arr = array;
For (int I = 0; I <arr. length; I ++ ){
System. out. println ("th" + (I + 1) + "");
// Indicates the switch. The initial value is false.
Boolean swap = false;

For (int j = 0; j <arr. length-1-I; j ++ ){
If (arr [j]> arr [j + 1]) {
// If switched, set the switch flag to true
Swap = true;
Int temp = arr [j];
Arr [j] = arr [j + 1];
Arr [j + 1] = temp;
}
}
System. out. println (Arrays. toString (arr ));
// If the switch flag is false, that is, if there is no switch, the nearest loop is exceeded.
If (! Swap ){
Break;
}
}
Return arr;
}

}

 

Related Article

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.