I've been looking at a book like deliberate come, huh? Algorithm
At that time on Amazon hanging out to pick the book, see the huge number of people recommend this algorithm book, say simple and easy to understand and buy reading. Indeed, the author's ability to describe algorithms is admirable. Let's review the usual algorithms.
In turn, I'll take a look at some of the common algorithms that I find interesting, this time it's fast track.
Quick Sort Introduction:
The central idea of the fast-line or the dichotomy, through the partition algorithm, the array needs to be sorted into two parts, and then recursive thinking repeated the process. Finally, the sorted minimum units are assembled in sequence to obtain the final data. The average time complexity is O (Nlogn), and the worst case is O (n squared).
Partition algorithm:
The partition algorithm is a classification algorithm, which simply divides a sequence into two parts, the first part satisfies a certain condition, and the latter part is the element that does not satisfy the condition. Perhaps the most famous application is the partition in the fast sort. We first find an element and then take that element as a datum point that will be less than all of him on one side and put all the numbers larger than him on the other side.
Let's look at the implementation of partition in the quick sort:
func partition (left int, right int, list []int) {ifLeft >=Right {return} I:=Left J:=Right temp:=List[left] forI! =J { forI < J && List[j] >=Temp {J-- } forI < J && List[i] <=Temp {i++ } ifI <J {List[i], List[j]=List[j], List[i]}} List[left], List[i]=List[i], List[left]}
We receive an array, a left start point, and a right end index as a parameter.
By default, the first number of the array is used as the index point, then the number less than his is placed on the left and the number greater than his is placed on the right.
Quick sort:
With recursive partition functions we can always divide the numbers into left and right sides, and then merge them together.
So the actual fast sort is only using recursion based on the partition algorithm. Finally get the result. The partition algorithm above uses two pointers to scan from both left and right. The scanning to the left and right two number exchange position, thus the two-point efficiency.
The complete code:
func quickSort (left int, right int, list []int) {ifLeft >=Right {return} I:=Left J:=Right temp:=List[left] forI! =J { forI < J && List[j] >=Temp {J-- } forI < J && List[i] <=Temp {i++ } ifI <J {List[i], List[j]=List[j], List[i]}} List[left], List[i]=List[i], List[left] QuickSort (left, I, list) quickSort (i+1, right, list)return}
Look for K-large or K-small numbers:
The same use of the partition algorithm makes it easy to find the small value of K and K in an unordered array. The principle is that by scanning we can find a few numbers in front of the datum points, if the words are in the positive order. When we have the number of k in front of our datum point, our number is actually k+1. The same search for the number of small k, only need to reverse. Then there are a few numbers in front of which our datum point is the k+1 small number.
Reference:
"Ah ha!" Algorithm, Halle
Partition algorithm and its application in detail (Golang implementation)