-
Quick sort is an improvement on bubbling sorting. Its basic idea is: divide the data to be sorted into two independent parts by lying sorting, all the data of one part are smaller than all the data of the other part, and then quickly sort the two parts of the data according to the secondary method, and the whole sorting process can be carried out recursively, so as to achieve that the whole data becomes an ordered sequence.
Suppose the array you want to sort is a[1] ......a[n], first select an arbitrary data (usually the first data) as the key data, and then put all the numbers larger than it in front of it, and all the numbers larger than it behind it, this process is called a lying quick sort. The algorithm for a lying quicksort is:
1) Set two variables i and j, i:=1 and j:=n at the beginning of sorting;
2) Take the first array element as the key data and assign a value to x, that is, x:=a[1];
5) Repeat step until i=j;
In this question, the initial key data x = 46;
a[1] a[2] a[3] a[4] a[5] a[6]
After the first exchange is carried out (according to the third step of the algorithm from back to front, find the one less than 46).
After the second exchange is carried out (according to the algorithm the fourth, do not find the one greater than 46 from the front to the back).
After the third exchange is carried out (according to the third step of the algorithm to find the one less than 46 from back to front, at this time i=4).
After the fourth exchange (according to the algorithm the fourth, do not find greater than 46 from the front to the back).
At this point, j=4 is found, and the quicksort of this trip is over.
The set of data up to 46 [40,38] was all less than 46
The set of data after 46 [56,79,80] is greater than 46
According to this idea, if you continue to sort [40 38], 46, [56 79 80], you will get an ordered array 38, 40, 46, 56, 79, 80
-
Hehe, you're playing strongly, I don't understand it yet.
-
Quick and clever track speed sorting is the method of arranging filial piety and sequencing of ().
a.Stable. b.Instability.
c.Sometimes stable, sometimes unstable.
d.The first three book options are incorrect.
Correct answer: B
-
Quicksort, a computer science vocabulary that is applicable to languages such as Pascal and C++, is an improvement on the bubbling sort algorithm.
1. First, set a demarcation value, and divide the array into left and right parts through the demarcation value.
2. Concentrate the data greater than or equal to the cut-off value to the right side of the array, and the data less than the cut-off value to the left side of the array. In this case, all elements in the left section are less than the cut-off value, while all elements in the right part are greater than or equal to the cut-off value.
3. Then, the data on the left and right can be sorted independently. For the array data on the left, you can take a demarcation value and divide the part of the data into two parts, the left and right parts, and place the smaller value on the left and the larger value on the right. The array data on the right can be treated similarly.
4. Repeating the above process, it can be seen that this is a recursive definition. After recursively ordering the left part, recursively order the right part. When the data in the left and right parts is sorted, the entire array is sorted.
Sort demo
Suppose the initial sequence is: 5, 3, 7, 6, 4, 1, 0, 2, 9, 10, 8.
At this time, ref=5,i=1,j=11, look from back to front, the first number smaller than 5 is x8=2, so the sequence is: 2,3,7,6,4,1,0,5,9,10,8.
At this time, i=1, j=8, from front to back, the first number greater than 5 is x3=7, so the sequence is: 2, 3, 5, 6, 4, 1, 0, 7, 9, 10, 8.
In this case, i=3, j=8, looking forward from the 8th position, the first number smaller than 5 is x7=0, so it is broken: 2, 3, 0, 6, 4, 1, 5, 7, 9, 10, 8.
In this case, i=3, j=7, look backwards from the 3rd digit, the first number greater than 5 is x4=6, therefore: 2, 3, 0, 5, 4, 1, 6, 7, 9, 10, 8.
At this time, i=4,j=7, look forward from the 7th position, the first number smaller than 5 is x6=1, therefore: 2,3,0,1,4,5,6, manuscript with 7,9,10,8.
At this time, i=4, j=6, look backwards from the 4th position, until the 6th digit has a number larger than 5, at this time, i=j=6, ref becomes a dividing line, the number before it is smaller than it, and the number after it is larger than it, for the number of the two parts before and after, you can use the same method to sort.
-
Recursive formula. Since the design to recursion. There are two necessary conditions for subconsciously wanting to use recursion.
If we want to sort a set of data in an array with subscripts from startindex to endindex, we choose any data between startindex and endindex as the pivot. Typically, the last element of the array is selected.
We iterate through the data from startindex to endindex, putting the less pivot to the left, the larger pivot to the right, and the pivot to the middle.
After this step, the data between the startindex and the endindex of the array is divided into three parts, all of which are smaller than the pivot, the middle is the pivot, and the second is larger than the pivot.
According to the idea of divide and conquer and recursive, we can use recursively sort the data between startindex and pivot-1 and the data between pivot+1 and endindex until the interval is narrowed to 1, which means that all the data are ordered.
With the core idea, it is now givenRecursive formula. , andExit Conditions
t(n) =2*t(n/2) +n
The derivation logic and merge order are consistent. The time complexity is:o(nlogn)
Quick sort is an in-place sorting, a movement implemented in the form of swapping, without opening additional memory space, and the space complexity is:o(1)
The process of merging and sorting isFrom bottom to top, work on the sub-issues first, and then merge them.
Fast row is just the opposite, its processing process is:From top to bottom, partition first, and then deal with the subissue.
Merge sorting is not in-place sorting and requires additional memory space.
Quick Sort is an in-place sort.
-
Common quicksort methods include bubble sorting, selecting medium potato sorting, inserting sorting, quicksorting, merging sorting, etc. The principles and implementations of these sorting methods vary, but the core idea is that sorting is achieved by comparing and exchanging the position of data.
Bubbling sort is a simple sorting method that achieves sorting by gradually "floating" larger elements to the end of a sequence by constantly swapping the positions of adjacent elements. Sort by choosing to sort by constantly selecting the smallest value in the sequence, placing it at the beginning of the sequence, and then doing the same for the remaining unsorted parts.
Insertion sorting is achieved by inserting unsorted elements one by one into the appropriate position in the sorted sequence. Quicksort is an efficient sorting method, and its core idea is to divide the sequence to be sorted into two subsequences through a divide and conquer strategy, and then sort the subsequences separately, and finally merge them into an ordered sequence. Merge sorting is also a commonly used sorting method, the idea is to divide the sequence to be sorted into several sub-sequences, sort them separately, and then merge the sorted sub-sequences into an ordered sequence.
In addition to the above sorting methods, there are some other sorting methods, such as Hill sorting, heap sorting, cardinality sorting, etc. These sorting methods have their own characteristics and are suitable for different sorting scenarios. In the actual programming, we need to choose the appropriate sorting method according to the specific needs to achieve the sorting operation.
-
The detailed process of quicksort is as follows::
Quick sort is to find a reference value, placing the number less than the reference value on the left side of the array, and the number greater than the reference value on the right side of the array. How to do it:
1. Randomly select an index in the array, and use its value as a reference value. The reference value is saved and exchanged with the value in the first position of the array; Start with the left and right sides of the array.
2. When the value on the right is greater than the reference value of the circle, it will go back one place; When the value on the right is not greater than the reference value, put the current value on the left to the position where it is currently pointing, and advance one place on the left; Then, to determine that the value on the left satisfies less than the reference value and advances one place backward, and the value on the left does not meet the value less than the reference value, put the current value into the current pointing position on the right, and advance one place on the right.
3. Until the position of the left and right points coincides, end the above judgment, put the reference value into the coincidence point, and return the index of the coincident point.
4. Take the coincident point as the dividing line, and divide it into two sub-arrays. The subarray repeats the above judgment.
5. Exit the recursive call until the array size of the incoming function is 1.
Quick sort refers to splitting the data to be sorted into two independent parts through a sort, where all the data in one part is smaller than all the data in the other part, and then the two parts of the data are quickly sorted separately according to this method.
-
Let's say you've logged the number of times you've listened to songs, and you want to sort them to see which band you like the most.
The way to choose a sort is to iterate through your list. Find the record with the most complaints and add it to the new list.
See how long it takes:o(n) time means to look at each element in the list once, for example, when doing a simple lookup on a list of bands, it means that each band is looking at it once.
Quick sort is a commonly used sorting algorithm that is much faster than selection sorting. For example, the function qsort in the C standard library implements quicksort. Fast sorting also uses d&c.
First, select an element from the array, which is called a pivot. Next, find out which elements are smaller than the baseline value and those that are larger than the baseline value.
So, we have it now.
Then quickly sort the subarray, and finally you can get the result.
Quick Sort is unique in that its speed depends on the benchmark value selected. (Different benchmark values will have different sorting processes, and it is important to choose the appropriate benchmark value).
Fast row (the most common and the easiest).
The idea of algorithms is divide and conquer. >>>More
Bubbling sort is the least efficient. Juvenile! You can quickly sort it, and you will understand it in minutes.
This one is in C.
#include >>>More
Gift-giving is simple, how can there be so many sayings, you think too much. >>>More
Subfunction pointer used incorrectly!