A brief talk on PHP source code 26: Php Quick Sort source code implementation simplification

Source: Internet
Author: User
This article mainly introduces about the PHP source code 26: Php Quick Sort Source code to achieve the simplification, has a certain reference value, now share to everyone, the need for friends can refer to

A brief talk on PHP source code 26: Php Quick Sort source code implementation simplification

This time in reviewing the data structure, there is a quick sort of seeing sort and classic
So there is a way to see the implementation of the Order in PHP, in the Zend directory we can see the zend_qsort.c file and the Zend_qsort.h file
This is where PHP implements a quick sort of file
From the code we can see, perhaps in order to be compatible with a variety of data types, so it is more complex in the exchange and comparison position, it seems more tangled, so they will
All of the types are converted to int type, which gets the quick sort in the simplified version of PHP source code.
The code is as follows:

#include <stdio.h> static qsort_swap (int *a, int *b) {int tmp;tmp = *a;*a = *b;*b = tmp;} void qsort (int *base, int n MEMB) {int *begin_stack[10];int *end_stack[10];int *begin;int *end;int *seg1;int *seg2;int *seg2p;int loop;unsigned int Offset /* Use stack instead of common recursive implementation */begin_stack[0] = base;//start element position stack, stack end_stack[0] = base + (nmemb-1);//End position stack, enter stack for (loop = 0; Loop & gt;= 0;  --loop) {begin = begin_stack[loop];//start position out of stack end = end_stack[loop];//End position out of stack while (begin < end) {offset = (end-begin) >> 1;//takes the middle position qsort_swap (begin, begin + offset);//exchange start and middle position seg1 = BEGIN;SEG2 = end; while (1) {for (; SEG1 < SEG2 && *begin < *seg1; SEG1 + = 1); for (; SEG2 >= seg1 && *seg2 > * Begin SEG2-= 1); if (seg1 >= seg2) break; Qsort_swap (SEG1, SEG2);} Qsort_swap (begin, SEG2); seg2p = SEG2; if ((Seg2p-begin) <= (end-seg2p)) {if (seg2p < end) {//right into stack begin_stack[loop] = seg2p + 1;end_stack[loop++] = end ;} end = seg2p;} else {if (seg2p > Begin) {//left into stack bEgin_stack[loop] = begin;end_stack[loop++] = seg2p-1;} End ifbegin = seg2p;} End If}//end While}//end for}int main (int argc, char *argv[]) {int a[10] = {5, 7, 8, 2, 4,, 3};int I;qsort (A, 8); for (i = 0; i < 8;i++) {printf ("%d", A[i]);} return 0;}

After reading, there is a feeling: powerful pointers, the benefits are not shallow!

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.