9.2.2 quick sorting

Source: Internet
Author: User

QuickSort is an optimization algorithm for Bubble sorting.

Basic Idea: sort the records to be sorted into two separate parts. the keywords of some records are smaller than those of other records, then, the two records can be sorted to achieve the purpose of sorting the entire sequence.

Pivot keyword): select a keyword in the column to be sorted, and try to place it in a position so that the value on the left is smaller than it, and the value on the right is larger than it, we will make this keyword pivot ).

Generally, the keyword at the leftmost end of the sorting is pivot.

# Include <stdio. h> # include <stdlib. h> void swap (int arr [], int I, int j) {int temp; temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} // divides the array to be sorted into two parts. The value on the left side of the pivot is smaller than the pivot value, and the value on the right side is greater than the pivot value, the Return Value of the function contains int Partition (int arr [], int low, int high) {int lower tkey; // The pivot value pivotkey = arr [low]; // take the first value to be sorted as the pivot value while (low 

Quick Sort Complexity Analysis

Optimal Condition: Each split into two parts is very uniform, that is, the data of the two parts is similar. Only recursive logn times are required. The algorithm time complexity is O (nlogn)

Worst case: to be sorted in positive or reverse order, each split is empty on one side. n-1 recursive calls are required, and the I-th Division must be compared by n-I, so the number of comparisons is n-1 + N-2 +... + 1 = n (n-1)/2. The time complexity is O (n2 ).

Average:

Complexity: O (nlogn );

Fast sorting is unstable.

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1282083

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.