Simplified and fast code sorting in C language with comments

Source: Internet
Author: User

I haven't written a blog for a long time. Today I have learned to arrange for myself. I think I only know my thoughts and have never really written them. I found an ACM trainer Hdu1106, mainly because ACM has data to help you know if you are right.
Although I have been writing for a long time, I still have a sense of accomplishment. I did not read a book or anything. I only wrote it in the way I thought about the guiding ideology. I prefer this Coding method. Okay, let's talk about food.
/**
@ R: array to be sorted
@ S: the starting point of the row, starting from 0.
@ E: the end of the row, starting from n-1.
*/
Void quicksort (int r [1001], int s, int e)
{
Int t = r [s]; // Sentinel, starting
Int f = s + 1;
Int B = e; // f is the forward pointer, starting from s + 1, B is the reverse pointer, starting from e
Int m = 0;
If (s> = e) return; // exit condition

While (f <= B)
{
While (f <= B & r [f] <= t) f ++; // search for elements larger than the Sentinel
While (f <= B & r [B]> = t) B --; // find elements smaller than the sentry
// Exchange the two elements
If (f <B ){
M = r [f];
R [f] = r [B];
R [B] = m;
F ++; B --;
}
}
// Exchange sentinel and r [B], r [B] Must be smaller than Sentinel
R [s] = r [B];
R [B] = t;
// Arranged on both sides
Quicksort (r, s, B-1 );
Quicksort (r, B + 1, e );
}
Note that duplicate keys exist. For example, the data to be sorted is. This bug has been tuned for a long time.
When writing, it is best to draw images on paper and combine debugging (I am a little too dependent on debugging, so it takes time). Best practices ~~
Author: "Qinghe"

Related Article

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.