Step-by-step write algorithm (Quick sort)

Source: Internet
Author: User

Original: Step-by-step writing algorithm (Quick sort)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Fast sorting is a sort of method that is often used in programming. But a lot of friends have fear emotion to the quick sort, think that the quick sort uses recursion, is a very complicated procedure, actually not necessarily this. As long as we use the good method, we can achieve a quick sort by ourselves.

First, let's review what the basic steps of a quick sort are:

1. Judging the legitimacy of input parameters

2, the first data of the array as the origin of the comparison, the data is smaller than the data on the left, the data is larger than the data arranged on the right

3, according to the method (2) to the left of the array and the right side of the data and (2) the same data arrangement

So how do you actually write the code?

a) First, judge the legitimacy of the data?

void Quick_sort (int array[], int length) {int median = 0;if (NULL = = Array | | 0 = = length) return;_quick_sort (array, 0, Lengt H-1);}

B) Find the middle number, and sort the data on the left and right respectively

void _quick_sort (int array[], int start, int end) {int middle;if (start >= end) Return;middle = Get_middle (array, start, E nd); _quick_sort (array, start, middle-1), _quick_sort (array, middle + 1, end);} void Quick_sort (int array[], int length) {int median = 0;if (NULL = = Array | | 0 = = length) return;_quick_sort (array, 0, Lengt H-1);}

c) So how should the middle number be arranged here?

int get_middle (int array[], int start, int end) {int front = 0;int Tail = end-start;int Value = array[start];int length = End-start + 1;int loop = start + 1;while (loop <= end) {if (Array[loop] < value) {Gquicksort[front] = Array[loop];fro NT + +;} Else{gquicksort[tail] = Array[loop];tail--;} loop + +;} Gquicksort[front] = Value;memmove (&array[start], gquicksort, sizeof (int) * (length)); return start + front;}

Note: Here Gquicksort is a global array, primarily for use as a temporary array of sorts, where you can flexibly apply various methods in the real world.

D) The basic quick sort is done, so how do we test it? Can we write a few simple test cases?

static void Test1 () {int array[] = {1};quick_sort (array, sizeof (array)/sizeof (int));} static void Test2 () {int array[] = {2, 1};quick_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), assert (2 = = A RRAY[1]);} static void Test3 () {int array[] = {4, 3, 2,1};quick_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), ASSERT ( 2 = = Array[1]), assert (3 = = array[2]), assert (4 = = array[3]);} static void Test4 () {int array[] = {3, 2, 1};quick_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), assert (2 = = Array[1]); assert (3 = = array[2]);}


"Preview: The next blog mainly introduces the contents of the merged sort"

Step-by-step write algorithm (Quick sort)

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.