dsa-interview-Quick Sort Notes

Source: Internet
Author: User

http://blog.csdn.net/morewindows/article/details/6684558#

Dictation: (the error is marked with yellow)

int partition (int s[], int left, int. right) {int i = left;int J = Right;int x =S[0];--------should be s[left]----is not necessarily a non-zero start AH. while (I < j)---------------because I=j is the time to end this small process. So, at the moment of i=j, it's okay to stop. {while (i<=J&AMP;&AMP;S[J]<x)-----------While (i<j&&s[j]>=x)"Two-while requires a =x" J--;s[i] = s[j];-----------------------if (i<j)------because the while may be i=j, here is guaranteed to pits at unequal times, and move 
{S[I]=S[J];
                                 i++;}while (i <= j&&s[i] > x)-----------similarly,s[i]<x;I++;S[J] = s[i];} S[i] = x;return x;--------------------------return is the pit, not the thing in the pit return I}void quick_sort (int s[], int left,int right) {if (s = = null| | Left>=right)------Here about an empty check ... It's weird---- It's a problem to be solved. Returnint g = partition (S, left, right), Quick_sort (s, left, g-1),--------------------recursive Quick_sort (s, left + 1, right); }

When you integrate into a function,

Attention:

① to set the return value to void

② checks the size of left and right at the very beginning----if you do not check for a stack overflow----The reason is that the final recursion is to be g-1 and g+1, if not checked out of range.

dsa-interview-Quick Sort Notes

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.