A method of sorting and administering strategies for fast sequencing

Source: Internet
Author: User

When it comes to algorithms, summer vacation is going to internship, then feel the importance of data structure and algorithm, although the sophomore time has been learned, but the basic use, resulting in the basic forget now, and now re-pick up, re-learned before and have not learned the algorithm to achieve once again!!!

Give yourself a task--an algorithm every day!!!

Fast Sorting algorithm : is an improvement of the bubble sort, if the initial sequence of records by the keyword Pivotnum ordered or basic order, it will degenerate into a bubble sort, at this time the complexity of O (N2). Otherwise, its average time complexity is O (n longn) , which uses the recursive principle, which has the best average performance in all orders of the same order of magnitude O (n longn) . In terms of average time, it is currently considered the best of an internal sorting method, it is unstable.

The basic idea is :

1. take a number from the array first as the base number.

2. Partitioning process, first from the back to look forward , and then back to the previous, will be larger than the number of numbers on its right, less than its number is all put on its left, equal to its number of positions unchanged, at the end of this partitioning process, The position of the base number is positioned in the middle of the array .

3. repeat the second step for the left and right intervals (recursively) until there is only one number for each interval, then the sorting is complete.

about which number as the base number, this is not recommended to take the first, it is recommended to randomly take a number as the base number, so that only to prevent extreme phenomena, you can prevent when an array is basically ordered, if the first one, then the time complexity of the N2 is turned back to O (), If you randomly take a number as the base number, you can avoid this behavior.

Look at the demo diagram:



The following is a better understanding, I will take the first number as the base number to demonstrate:



The whole sequence as an array, the 0th position as a pivotnum, first looking forward from the back, that is, first and last, if it is smaller than it, than it does not do any processing, the exchange of the back again after the former, that is, and the first element, than it is smaller than the exchange, larger than him. This cycle, a trip to the completion of the order, the left is smaller than the middle Axis pivotnum, the right is larger than the middle axis pivotnum, and then with the division of the method, respectively, the two separate arrays for a quick sort, so reciprocating, until only one element in each array is sorted.

The code to sort a trip is:

public static int sort (int[] data,int left,int right) {//each time a trip is made to sort int pivotnum = Data[left];while (left<right) {while ( Left<right && data[right]>=pivotnum) {right--;} Data[left] = data[right];//moves smaller than the pivot point to the left side, at which point the right bit is equal to NULL, waiting for the left side to be greater than the number of pivotnum to give it while (Left<right && Data[left] <=pivotnum) {left++;} Data[right] = data[left];//will be larger than the number of axes to move to the right end, when the left bit equivalent to empty, waiting for the right than Pivotnum small number to give it}//at this time Left==right, completed a lay sort, when left bit equivalent to empty, Wait for the pivot point that number pivotnum data[left] = pivotnum;//The returned LEFR is the middle position return left;}
Recursive implementations are quickly sorted:

public static void QuickSort (int[] Nums,int left,int right) {if (IsEmpty (nums)) return;if (left<right) {// Middle as the middle position, the element that is smaller than the element in its position is sorted on the left as an array, and the element larger than its location is sorted by the right as an array of int middle = sort (nums, left, right);// The list array is divided into two, and then the recursive sort//left and right array recursive calls are quickly sorted until the sort completes quickSort (nums, leave, middle-1); QuickSort (Nums, middle+1; right);}}


Finally, paste the entire code:

public class QuickSort {public static int sort (int[] data,int left,int right) {//each time a trip is made to sort int pivotnum = Data[left];while (le Ft<right) {while (Left<right && data[right]>=pivotnum) {right--;} Data[left] = data[right];//moves smaller than the pivot point to the left side, at which point the right bit is equal to NULL, waiting for the left side to be greater than the number of pivotnum to give it while (Left<right && Data[left] <=pivotnum) {left++;} Data[right] = data[left];//will be larger than the number of axes to move to the right end, when the left bit equivalent to empty, waiting for the right than Pivotnum small number to give it}//at this time Left==right, completed a lay sort, when left bit equivalent to empty, Wait for the pivot point that number pivotnum data[left] = pivotnum;//The returned LEFR is the middle position return left;} public static void QuickSort (int[] Nums,int left,int right) {if (IsEmpty (nums)) return;if (left<right) {// Middle as the middle position, the element that is smaller than the element in its position is sorted on the left as an array, and the element larger than its location is sorted by the right as an array of int middle = sort (nums, left, right);// The list array is divided into two, and then the recursive sort//left and right array recursive calls are quickly sorted until the sort completes quickSort (nums, leave, middle-1); QuickSort (Nums, middle+1; right);}} public static Boolean IsEmpty (int[] nums) {if (nums.length==0) {return true;} return false;}}

Test:

Input: 17 31 40 26 35 36 25 12 2 38

Output: 2 12 17 25 26 31 35 36 38 40



A method of sorting and administering strategies for fast sequencing

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.