Exchange sorting of sorting algorithms (two methods for fast sorting [same idea ]).

Source: Internet
Author: User

Exchange sorting of sorting algorithms (two methods for fast sorting [same idea ]).
1. Bubble Sorting

The idea of Bubble Sorting is very simple. First, compare the first and second of n elements. If the positions of the two elements are backward, the positions of the two elements are exchanged. Then, the second and third element keywords are compared, and so on, until the N-1 and n elements are compared. The above process describes the first sorting process of Bubble sorting. In the first sorting process, we place the element with the largest keyword to the most position in the sequence with n elements through the exchange operation. Then perform the second sort and perform the same operation on the first n-1 elements of the element sequence during the second sort, the result is that the elements with the keyword size are placed at the n-1 position by swapping. In general, the I-level sorting is to sort the first n-I + 1 element of the element sequence, the element with the largest keyword in the first n-I + 1 element is placed at the n-I + 1 position. The elements are sorted by keywords by a total of N-1 characters.


public void Bubble_Sort(int A[], int N) {for (int P = N - 1; P >= 0; P--) {int flag = 0;for (int i = 0; i < P; i++) {if (A[i] > A[i + 1]) {int temp = A[i];A[i] = A[i + 1];A[i + 1] = temp;flag = 1;}}if (flag == 0)break;}for (int i = 0; i < N; i++) {System.out.print(A[i] + " ");}}
[Efficiency Analysis]
Space efficiency: Only one secondary storage unit is used.
Time Efficiency: assuming that the number of elements to be sorted is n, n-1-1 sort is performed in total, and the J-1 keyword comparison is required for the one-click Bubble sorting of the subsequences of j elements. As a result, the total number of comparisons of Bubble Sorting is


Therefore, the time complexity of Bubble Sorting is round (n ~ 2 ). 2. Quick sorting Its charm is that it can determine the final correct position for an array element in each partition (the core of the Sorting Algorithm) (positioning accuracy at one time, this element is not considered in the next loop ). Quick sorting is a typical example of applying grouping to sorting. The basic idea of quick sorting is to use a pivot) the element divides the sequence of n elements into the left and right sub-sequences Ll and Lr. The element in the neutron sequence Ll is smaller than the pivot element, the elements in the subsequence Lr are larger than the pivot elements, and then the left and right subsequences are sorted quickly. After the Left and Right subsequences are sorted, the whole sequence is ordered, the sorting process of left and right subsequences ends when the subsequence contains only one element. At this time, the left and right subsequences are naturally ordered because they only contain one element. The process of rapid sorting is described in the three steps of the division and Control Law:
Partitioning steps: Use pivot element x to split the sequence into two parts. The elements in the left subsequence are smaller than x, and those in the right subsequence are greater than x;
Governance steps: recursive sorting of left and right subsequences;
Combination steps: None
From the description of the quick sorting algorithm, we can see that the implementation of the quick sorting algorithm depends on the process of dividing the sorting sequence according to pivot element x.

Public static void Quick_Sort (int [] A, int begin, int end) {if (begin <end) {// After the pivot is selected, it will remain in the middle, int key = A [begin]; int I = begin; int j = end; // place the large element on the right and the small one on the left, to implement subset division. While (I <j) {// the two ends alternate to the internal scan. While (I <j & A [j]> key) {// when the right element is greater than the pivot element, the pointer is shifted left if the condition is met. J --;} if (I <j) {// when the preceding conditions are met, change the positions of the two elements and shift the Pointer right from the start position. A [I] = A [j]; I ++;} while (I <j & A [I] <key) {// when the right element is greater than the pivot element, if the condition is met, the pointer is shifted to the right. I ++;} if (I <j) {A [j] = A [I]; j --;} A [I] = key; System. out. println (I); Quick_Sort (A, begin, I-1); Quick_Sort (A, I + 1, end) ;}} public static void quickSort (int [] n, int left, int right) {int pivot; if (left <right) {// pivot, the smaller element is left, for larger elements, the right direction = partition (n, left, right); // recursively calls the quick sorting for the left and right arrays until the order is completely correct. quickSort (n, left, minute-1); quickSort (n, hour + 1, right) ;}} public static int partition (int [] n, int left, int right) {int distinct tkey = n [left]; // The value of the selected pivot remains unchanged, and is eventually in the middle. The former is small and the latter is large while (left <right) {while (left <right & n [right]> = pivotkey) -- right; // move the element smaller than the pivot to the lower end. At this time, the right bit is equivalent to null, wait until n [left] = n [right] is added to the number greater than the limit tkey; while (left <right & n [left] <= Limit tkey) ++ left; // move the element larger than the pivot to the high end. At this time, the left bit is equivalent to null. Wait for the number of the elements whose height is smaller than the pivotkey to add n [right] = n [left];} // when left = right, a quick sorting is completed. At this time, the left bit is equivalent to null. Wait for tkey to add n [left] = Beijing tkey; return left ;}

Time Efficiency: the running time of the Quick Sort Algorithm depends on whether the Division is balanced, that is, the number of elements in the two subsequences is divided according to the pivot element sequence, whether the division is balanced depends on the pivot element used. On average, it is proved that, in all sorting methods of the same order of magnitude, the constant factor k of fast sorting is the smallest. Therefore, in terms of average time, quick sorting is considered to be the best internal sorting method currently.
The average performance of quick sorting is the best. However, if the sorting sequence is sorted by keyword or basic order at the beginning, the quick sorting is sorted by bubble, and the time complexity is n2 ). For improvement, you can use the random selection of pivot elements. The specific method is to randomly select an element in the sequence to be divided and then exchange it with r [low, using r [low] as the pivot element will greatly improve the performance of fast sorting in sequence order or basic order. When n is large, the possibility of the worst condition during its operation can be considered as nonexistent.
Space efficiency: Although the efficiency of fast sorting is better than that of the preceding Algorithm in terms of time, in terms of space, all the algorithms discussed earlier require only one auxiliary space, fast sorting requires a stack to implement recursion. If the sequence is evenly divided into two subsequences with similar lengths, the maximum depth of the stack is log n. However, in the worst case, the maximum stack depth is n.


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.