Algorithm sorting (4): Analysis on the optimization of quick sorting and Analysis on Sorting

Source: Internet
Author: User

Algorithm sorting (4): Analysis on the optimization of quick sorting and Analysis on Sorting

The previous article introduced the fast sorting of unilateral and bilateral scans. However, we will give a brief analysis today.

I. disadvantages of unilateral Scanning

The biggest drawback of a single-side scan is that it needs to be exchanged every time. If an array is 5 4 3 2 1, it will start from 4 and 4 will be exchanged with 4, 3. Exchange with 3, and so on. Therefore, it would be better to use bilateral scanning. The first round only needs to be exchanged once to get an array like 1 4 3 2 5. However, bilateral scanning can be further optimized.

Ii. Optimization of bilateral Scanning

Optimization 1: the principle of random selection should be used for key-worthy selection, rather than the first number. Meaning everyone knows.

Optimization 2: the above method is the pitfall method. Find the first number smaller than the key on the right side and put it in the pitfall on the left side. A pitfall is added on the right side. Add the index of the Left pitfall to 1, then find the first number greater than the key from left to right, and then put it in the right pitfall. And so on. To speed up, find the first number smaller than the key on the right, record the index as j, and separate it from the first pitfall on the left.Exchange (note the exchange instead of simply put it .). Then, add 1 scan to the left-side Navigation Pane, find the first number greater than the key, and then exchange with j. Then scan from the J-1 to find the second number less than the key, and so on.

Optimization 3: This article describes insertion sorting, which is characterized by fast sorting of a basic ordered array, you can first determine the right-left value. If the value is small, return. The last insert order is OK! This is also an optimization idea.

The specific code is not included in the miscellaneous and can be found on the Internet.

References: Chapter 11 of programming Pearl River


Quick sorting algorithm problems, let's look at your ideas

/* I just read the introduction to algorithms and wrote one. I still feel the efficiency is acceptable */
# Include <stdio. h>

Static int a [8] = {3, 7, 2, 8, 4, 5, 3, 9 };

Void swap (int * m, int * n)
{
Int temp = * m;
* M = * n;
* N = temp;
}

Int partition (int p, int r)
{
Int j;
Int x = a [r];
Int I = p-1;

For (j = p; j <r; j ++)
{
If (a [j] <= x)
{
I ++;
Swap (& a [I], & a [j]);
}
}
Swap (& a [I + 1], & a [r]);

Return I + 1;
}

Void quicksort (int p, int r)
{
Int q;

If (p <r)
{
Q = partition (p, r );
Quicksort (p, q-1 );
Quicksort (q + 1, r );
}
}

Int main ()
{
Int I;
Int len = sizeof (a)/4;

For (I = 0; I <len; I ++)
Printf ("% d", a [I]);
Printf ("\ n ");

Quicksort (0, len-1 );

For (I = 0; I <len; I ++)
Printf ("% d", a [I]);
Printf ("\ n ");

Return 0;
}
/* Another article in my space is about qsort. I didn't understand the source code in glibc. If you are interested, please study it */
I have read the URL provided above. The article is well written. The analysis on qsort is also thorough. I feel that there are people outside of us who are still learning a long way.

Bubble sort method and Quick Sort comparison algorithm

Beat your ass, so you don't have to take a hard look at this simple problem.

Bubble Sorting is the slowest sorting, and the time complexity is O (n ^ 2 ).

Quick sorting is the fastest sorting. For quick sorting, I recommend you read chapter 2 of "the beauty of code": the most beautiful code I have written. The most beautiful thing the author says is the most efficient.

------------------------------ From the "Beauty of code" ---------------

When I wrote a thesis on divide-and-conquer algorithms, I found C. a.R. hoare's Quicksort algorithm ("Quicksort", Computer Journal 5) is undoubtedly the originator of various Quicksort algorithms. This is a beautiful algorithm for solving basic problems and can be implemented with elegant code. I like this algorithm very much, but I still cannot figure out the innermost loop of the algorithm. I spent two days debugging a complex program that uses this loop, and for a few years, when I need to complete similar tasks, I will be very careful to copy this code. Although this code can solve the problem I encountered, I did not really understand it.
I later learned a partitioning mode from Nico Lomuto, and finally compiled the Quicksort algorithm that I can understand and even prove. William Strunk Jr. the experience of "good writing style is concise" proposed for English is also suitable for coding, so I followed his suggestion, "omit unnecessary words" (from The book The Elements of Style ). I eventually reduced about 40 lines of code to a dozen lines of code. Therefore, if you want to answer "What is the most beautiful code you have ever written ?" In this case, my answer is the Quichsort Algorithm in my book Programming Pearls, Second Edition (Addison-Wesley. In Example 2-1, the Quicksort function written in C language is provided. We will further study and improve this function in the following sections.
[Example] 2-1 Quicksort Function
Void quicksort (int l, int u)
{Int I, m;
If (l> = u) return; 10
Swap (l, randint (l, u ));
M = l;
For (I = l + 1; I <= u; I ++)
If (x [I] <x [l])
Swap (++ m, I );
Swap (l, m );
Quicksort (m-1 );
Quicksort (m + 1, u );
}
If the function is called in the form of quicksort (0, n-1), this code sorts a Global Array x [n. The two parameters of the function are the subscript of the sub-array to be sorted: l is the lower subscript, and u is the higher subscript. Function call swap (I, j) will swap the two elements x [I] and x [j. During the first switching operation, a division element is randomly selected between l and u in a uniform distribution mode.
The book Programming Pearls contains detailed derivation of the Quicksort algorithm and proof of correctness. In the rest of this chapter, I will assume that the reader is familiar with the Quicksort algorithm provided in Programming Pearls and the Quicksort algorithm provided in most elementary algorithm textbooks.
If you change the question to "which code is the most beautiful in your code that is widely used ?" My answer is the Quicksort algorithm. An article ("Engineering a sort function," Software-Practice and Experience, Vo...) written by me and M. D. McIlroy>

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.