Java basic knowledge algorithm [Bubble Sorting] [Fast sorting]

Source: Internet
Author: User

 

/**

@ Auther: Kevin

@ Function:

The basic concept of bubblesort is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed at the end. In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). Before and after placing the decimal number, always compare to the second to the last (the largest position in the last), and the second stop, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). In this case, repeat the above process until the sorting is completed.
Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.
*/

Public class bubblesort
{
Public static void main (string ARGs []) {
Int A [] = {,}; // defines an array
Int temp; // temp for storage
For (INT I = 1; I <= A. Length-1; I ++)
{
For (Int J = 0; j <= A. length-1-i; j ++)
{
If (A [J]> A [J + 1]) // if the previous number is greater than the last one
{
Temp = A [J + 1]; // change the location between a [J] And a [J + 1]
A [J + 1] = A [J];
A [J] = temp;
}
}
}
For (INT I = 0; I <A. length; I ++)
System. Out. println (A [I]);
}
}

 

 

 

-------------------------------------------------------------------------

[Fast Read sorting]

Import java. util. arrays;

/**
* Fast sorting
* @ Author Administrator
*
*/
Public class quicksort {
/**
* Comparison results
* @ Param sortin: Input Array
* @ Param I: lower limit of the Interval
* @ Param J interval Upper Bound
* @ Return returns the final position of the benchmark.
*/
Private Static int partition (integer [] sortin, int I, Int J ){

Int sequence = sortin [I];
While (I <j ){
// Scan from right to left to find records with the first keyword less than limit
While (I <J & sortin [J]> = bytes)
J --;
If (I <j ){
Swap (sortin, I, j); // exchange sortin [I] And sortin [J]
I ++;
}

// System. Out. println ("right-to-left scan:" + arrays. aslist (sortin). tostring ());

// Scan from left to right to find records with the first keyword greater than limit
While (I <J & sortin [I] <= partial)
I ++;
If (I <j ){
Swap (sortin, I, j );
J --;
}

// System. Out. println ("scan left to right:" + arrays. aslist (sortin). tostring ());
}
Sortin [I] = cursor; // The reference position has been finalized.
// System. Out. println ("tag:" + tags );
Return I;
}
/**
* Recursive sorting
* @ Param sortin
* @ Param low
* @ Param high
*/
Public static void quicksort (integer [] sortin, int low, int high ){
Int pivotpos; // the location of the benchmark record after Division
If (low Partition TPOs = partition (sortin, low, high); // divide
Quicksort (sortin, low, pivotpos-1); // recursively sorts the left Interval
Quicksort (sortin, pivotpos + 1, high); // recursively sorts the right range
}

}

 
Private Static void swap (integer [] sortin, int I, Int J ){
// Todo auto-generated method stub
Int temp = sortin [I];
Sortin [I] = sortin [J];
Sortin [J] = temp;

}
 
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Integer [] A = new integer [] {8, 6, 1, 5, 4 };
Quicksort (A, 0, A. Length-1 );
System. Out. println ("Final Result:" + arrays. aslist (a). tostring ());
}

}

 

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.