Transferred from: https://www.cnblogs.com/AlwinXu/p/5424905.htmlQuick Sort (quickSort)The idea of a fast line: first select any data (usually the first number of the array) as the key data, and then put all the smaller than it in front of it, all the larger than its number is placed behind it, this process is called a quick sort.Baidu Encyclopedia to give the algorithm:A quick sorting algorithm is: 1) Set two variables I, J, the beginning of the order
, 86, 36] 4th time: [15, 14, 32, 54, 78, 36, 86] 5th time: [14, 15, 32, 54, 78, 36, 86] 6th times: [14, 15, 32, 54, 36, 78, 86] 7th times: [14, 1 5, 32, 36, 54, 78, 86] sorted array: [14, 15, 32, 36, 54, 78, 86]It can be seen that the optimized code is 29 times less repetitive than the original (36-7=29).Second, quick sortQuick Sort (Quicksort) is an improvement to the bubbling sort.Its basic idea is: by a trip to sort the data to be sorted into two s
The concept of fast sorting algorithmfast sorting is generally based on recursive implementations. The idea is this:1. Select a suitable value (ideally, the value is the best, but the first value of the array is generally used in the implementation), called "pivot" (pivot).2. Based on this value, the array is divided into two parts, the smaller one on the left and the larger on the right.3. To be sure, such a round down, this pivot position must be in the final position.4. Repeat the above proce
6 7 1 5 First use 2 as the benchmark, using the I-j two pointers are scanned from both sides, separating the elements smaller than 2 and the elements larger than 2. First compare 2 and 5, 5:2, J shift left 2 2 4 9 3 6 7 1 5 compare 2 and less than 2, so put 1 in the position of 22 1 4 9 3 6 7 1 5 Compare 2 and bis more than 2, so move 4 to the back 2 1 4 9 3 6 7 4 5 Compare 2 and7,2 and 6,2 and 3,2 and 9, all greater than 2, meet the conditions, and therefore unchangedAfter the first round of
when the i+ or J is complete, at which time the loop ends).*/Extension Array where element:comparable {Private Mutatingfunc quickpartition (Left:int, right:int)->int {var right = rightvar left = LeftRecord which is the base numberLet base = Self[left]Record base number positionLet BaseIndex = LeftFirst scan from the right to the left, find the first number smaller than base, but not to meetWhile left right = Right-1}Then scan from the left to the right to find the first number larger than base,
OrderHigh-speed sequencing (QuickSort) is also a sort algorithm for an input array that includes n arrays. The worst-case execution time is O (n^2).Although the worst-case execution time is worse. But high-speed sorting is generally the best useful choice for sorting. This is because its average performance is quite good. The expected execution time is O (NLGN). and the implied constant factor in O (NLGN) is very small. It also enables in-place sequen
from high point to find the first record less than PivotKey (here 27) and pivot record Exchange:27 38 65 97 76 13 49 49↑ (Low) ↑ (high)↑ (PivotKey)3, search backward from the point of low, find the first record greater than PivotKey (here is 65) and pivot records exchange each other:27 38 49 97 76 13 65 49↑ (Low) ↑ (high)↑ (PivotKey)4, repeat 2, 3 steps until Low=high.27 38 13 97 76 49 65 49↑ (Low) ↑ (high)↑ (PivotKey)27 38 13 49 76 97 65 49↑ (Low) ↑ (high)↑ (PivotKey)27 38 13 49 76 97 65 49↑ (
-last element becomes the second largest number in the array. The entire process continues until all elements have been sorted.Note: If no transformations occur in a traversal, then the next traversal is not necessary because all elements are already sorted.Java implementations:public static void Bubblesort (int[] list) {Boolean neednextpass = true;for (int k = 1; k 4. Fast sequencing (Time complexity O (NLOGN))The idea of a quick sort is to select an element of a primitive in the array, divid
first two aspects, and the third is a brief introduction because I have not found enough relevant information. In addition, this article also introduces parallel fast algorithms and introduces possible improvements to the fast Sorting Algorithm from another aspect.
Iv. References[1] Roger L. Wainwright. quicksort algorithms with an early exit for sorted subfiles, 1987[2] anany Levitin, translated by Pan Yan. Basics of algorithm design and analysis, 2
.
In general, for arrays, the space complexity of merging and sorting is O (n). You need to open an extra space of O (n) to accommodate arrays, to indicate the order after merging. However, for the linked list, you can save the overhead of this part of space. You only need to change the next pointer of the node to indicate the new order after merging, so the space complexity suddenly drops to O (1)
Summary:
(1) An interesting question. You need to think about whether the old conclusion is still
each time (generally, the complexity of binary is related to logn), it is O (n * logn)Worst case: in basic order, it degrades to bubble order, which is about N x n times, so it is O (n x N)
3,Stability
The original order may be disrupted because each time the element needs to be exchanged with the central axis. For example, if the sequence is 5 3 3 4 3 8 9 10 11, the sequence of 3 is disrupted. Therefore, fast sorting is unstable!
4,Implementation
Java implementation method.
Public class
no one answered.
The input information of the search is a string, and the top 10 of the top 10 input information is counted. Each time we input a string of no more than 3 million bytes, the memory usage is only 1 GB,Describe the concept, write computation (C), space and time complexity,This question is not very difficult. Use the hash method to hash these strings into different buckets, and then determine the maximum number of strings.
My program for question 4th
# Include # Include # Define Ma
Returns the number of pairs in the array and N./*Find the pair in an array... that sum up to particle numberCompany: LinkedinDate: 8/11/2011*/# Include # Include # Include
Int randomPartition (int A [], int low, int high );Void quickSort (int A [], int low, int high );Bool findPair (int A [], int size, int sum, std: pair Void quickSort (int A [], int low, int high){If (low> = high)Return;Int partition = ra
Problem description:Quick sortingAlgorithm
//******************************// Description: quick sorting algorithm.// Class Name: quicksort// Author: Hong Xiaojun// Time: 2004-11-2//******************************
Public class Quicksort { Public void Quicksort (Int [], Int Left, Int Right) // Left is the first element position of array A, and right is the
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.