Algorithms start from zero - sorting

Algorithms start from zero - sorting

1. Quick Sort

Quick sort is almost a must-have question for fresh graduates interviewing for various algorithms. It is not asking you how to implement it, or you are asked to write the implementation code on paper.

The idea adopted by quicksort is the idea of ​​divide and conquer, for example, as this blog said https://www.cnblogs.com/codeskiller/p/6360870.html

1. Algorithm overview/idea

Quicksort is generally implemented based on recursion. The idea is this:

1. Select an appropriate value (ideally the best value, but the first value of the array is generally used in the implementation), which is called "pivot".

2. Based on this value, divide the array into two parts, the smaller part is on the left and the larger part is on the right.

3. It is certain that after such a round, the position of the pivot must be in the final position.

4. Repeat the above process (recursively) for each of the two subarrays until each array has only one element.

5. Sorting is complete.

Example:

an array to be sorted

2  0  3  6  4  5  1

Then we have 2 pointers, one for first to point 0 and one for last to point to last.

We choose the first one as the "pivot", called the key. So key=2 .

Then start from the last pointer (from the far right), select (1) less than the key value, copy it to the position pointed to by first, which is the 0th position, the first and last pointers are not shifted, and now the result becomes

1  0  3  6  4  5  1

Then from the first pointer (from the far left), select (3) greater than the key value, and copy it to the position pointed to by last, which is the sixth position. Because the 1 in the 0th position does not match the 0 in the first position, the first pointer is moved to the right, the first pointer is 2, and the last is 6. The result becomes

1  0  3  6  4  5  3

Next, continue, starting from the last pointer ( 3 ), select (3) less than the key value. Since it does not match, the last pointer moves to the left until it overlaps with first, and the result becomes

1  0  3(3)  6  4  5  3

When the first and last coincide, a group of recursive loops is completed, and the coincident bit is assigned as the key. it becomes

1  0  2  6  4  5  3

At this point, recursion can be performed, and the above steps can be continued with 2 as the center and divided into left and right groups.

Recursive use only needs to have an exit condition, which is naturally "first and last coincide".

 

Second, the code example

#include<stdio.h>
void Qsort(int a[], int low, int high)//The incoming is the array to be sorted
{
    if(low >= high)/*recursive exit condition*/
    {
        return;
    }
    int first = low;
    int last = high;
    int key = a[first];/*Use the first record of the word table as the pivot*/

    while(first < last)
    {
        while(first < last && a[last] >= key)
        {
            --last;
        }

        a[first] = a[last];/*Move smaller than key to low end*/

        while(first < last && a[first] <= key)
        {
            ++first;
        }


        a[last] = a[first]; /* move the key larger than the key to the high end */
    }
    a[first] = key; /*Pivot record in place*/
    Qsort(a, low, first-1);/*recursive left array*/
    Qsort(a, first+1, high);/*recursive right array*/
}
intmain()
{
	int a[10]={2,0,3,7,6,4,5,9,8,1};
	QSort(a,0,9);
	int n;
	for(n=0;n<10;n++)
	{
		printf("%d %d\n",n,a[n]);
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337978&siteId=291194637