Implement exchange sorting in Java

Source: Internet
Author: User

Exchange sorting is another frequently used internal sorting policy, commonAlgorithmFor Bubble sorting and quick sorting, Bubble sorting and quick sorting are classified as exchange sorting because the main work of these two algorithms is to continuously exchange elements to achieve sorting.

Bubble Sorting is the most typical exchange sorting. Its algorithm is: (assume that data is stored in array a [n)

1. compare a [0] And a [1]. If a [0]> A [1], a [0], a [1], then compare the new A [1] (possibly the original A [0]) and a [2]. If a [1]> A [2], then exchange a [1], a [2], and so on until a [N-2] And a [n-1] are compared, so that the maximum number in a is "bottom, that is, a [n-1] is the maximum value in array.

2. continuing Step 1 from the beginning, the difference is that this comparison ends with a [N-2], so that the secondary arguments in array a are swapped to the location of a [N-2.

3. In n-1 array a is sorted in the ascending order. (You can also determine whether there is exchange. If there is no exchange in a comparison, it indicates that array A has sorted out the order ).

JAVA Implementation of Bubble Sorting is as follows:

 
Public static void bubblesort (INT [] elements) {for (INT I = elements. length-1; I> 0; I --) {for (Int J = 0; j <I; j ++) {If (elements [J]> elements [J + 1]) {swap (elements, J, J + 1 ); // exchange two elements }}} Private Static void swap (INT [] elements, int I, Int J) {int temp = elements [I]; elements [I] = elements [J]; elements [J] = temp ;}

The implementation of Bubble Sorting is relatively simple. The time complexity is O (n * n), and the space complexity is O (1 ).

Another typical type of exchange sorting is fast sorting, which is highly efficient, but has a large space complexity. Because quick sorting uses recursion, the implementation of recursion requires a stack. The algorithm for fast sorting is as follows: (assume that the data is stored in array a [n)

1. If the length of the array to be compared is 0 or 1, no comparison is required and the result is returned directly.

2. if the length of the array to be compared is greater than 1, a central value (Centrum) is randomly selected and then traversed from both ends of the array, in addition, the elements found from the left traversal greater than the Centrum and those found from the right traversal less than the centrum are exchanged until the array traversal is complete (I .e: the index pointed by the Left traversal pointer is greater than or equal to the index pointed by the right traversal pointer ).

3. exchange the central element and the elements pointed to by the traversal pointer on the right. In this way, the original array divides the central element into two arrays, and the elements of the Left array are not greater than the central element, the elements in the array on the right are not smaller than the central node, and then the two sub-arrays are sorted quickly.

Java implementation of quick sorting is as follows:

 Public static void quicksort (INT [] elements, int begin, int end) {If (begin 
  
    = begin) & (elements [Back]> centrum) Back --; If (front 
   
  

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.