Instance 365 (14) --------- classic array sorting method ------ quick sorting

Source: Internet
Author: User
Tags index sort

I:

Ii. Quick sorting

Quick sorting is a very fast comparison sorting method. It is also one of the implementations of Divide-And-Conquer ideas. Since its generation, the fast sorting theory has been greatly improved, but in reality it is very difficult to program correct and robust code. This article will give a comprehensive explanation of the basic theory and programming practices of the Quick Sort Algorithm. In this article, we will ignore many details and try to create a very specific sorting image for readers.

Quick sorting-Basic Theory

This algorithm is an implementation of Divide-And-Conquer, so this article will analyze it with Divide-And-Conquer. First, if the number to be sorted is stored in array S, the operation of this algorithm can be split into two parts:

  • Select an element v from S;
  • Divides the S array into three sub-arrays. Here, the element "v" forms a sub-array 1 separately, and the element smaller than "v" forms sub-array 2, and the element larger than "v" forms the self-array 3.
  • Perform the first two steps for sub-array 2 and sub-array 3 to implement recursive sorting;
  • Returns S1, V, and S2 in sequence;

The program has the average running time T (n) = O (nlgn), the worst running time T (n) = O (n ^ 2 );

The following provides a simple sorting example to describe the above algorithms:

The initial array is --------------> S :,

Assign the first element to v -----> v = 6;

Split S based on v ---> [2, 5, 3], [6], [8, 13, 10, 11] <---------- name the obtained array S1, S2;

Split the child array S1-> [], [2], [5, 3] <------------------ after splitting, the first child array is empty. Name the obtained array S12;

Split the child array S2 -----> [], [8], [13, 10, 11] <------------- name the obtained array S22;

At this time, the array S is ---------->

Split the child array S12 ----> [3], [5], [];

Split the self-array S22 ----> [10, 11], [13], [] <------------------ name the obtained array S221

At this time, the array S is -----------> 2, 3, 5, 6, 8, 10, 11, 13

Split the child array S221 ---> [], [11], [13]

The resulting array is --------> 2, 3, 5, 6, 8, 10, 11, 13;

Iii. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace FastSort {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private int [] G_int_value; // define the array FIELD private Random G_Random = new Random (); // create a Random number object // exchange data private void swap (ref int l, r Ef int r) {int temp; // temporary value temp = l; // record the previous value l = r; // record the next value r = temp; // pre-and post-exchange data} // <summary> /// fast sort /// </summary> /// <param name = "list"> array to be sorted </param> /// <param name = "low"> Start position of the array </param> /// <param name = "high"> end position of the array </param> private void Sort (int [] list, int low, int high) {int middle; // temporary variable, used to store the maximum int l, r; // respectively used to record the retrieved index and the maximum index int mid; // intermediate index if (high <= low) // judge whether the input value is valid return; else if (hi Gh = low + 1) // determine whether two indexes are adjacent {if (list [low]> list [high]) // determine whether the previous value is greater than the subsequent value swap (ref list [low], ref list [high]); // exchange the return of the pre-and post-index value ;} mid = (low + high)> 1; // The middle index of the record array, equivalent to (low + high) divided by 2 rows = list [mid]; // initialize the value of the Temporary Variable swap (ref list [low], ref list [mid]); // swap the index order of the first and middle values l = low + 1; // record the retrieved index value r = high; // record the maximum index try {// use do... while looping through the array, and comparing the size of the front and back values do {while (l <= r & list [l] <distinct) // determine whether the retrieved index is smaller than or equal to the maximum Large index l ++; // index value plus 1 while (list [r]> = distinct) // determines whether the maximum value is greater than or equal to the record's pivot r --; // increase the index value by 1 if (l <r) // if the retrieved value is smaller than the maximum value swap (ref list [l], ref list [r]); // switching sequence} while (l <r); list [low] = list [r]; // records the minimum value list [r] = comment at the minimum index; // record the maximum value if (low + 1 <r) at the maximum index // determine whether the minimum index is smaller than the maximum index Sort (list, low, r-1 ); // call itself to perform quick sorting if (r + 1 

 

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.