C # Quick sorting algorithm,

Source: Internet
Author: User

C # Quick sorting algorithm,

Today, I reviewed the following sorting algorithms, including the Bubble sorting method and direct sorting method, which are relatively simple, but it is difficult to quickly sort the algorithm. So I will focus on it.

Let's talk about the principle: the quick sorting method uses recursion to perform several operations on the sorted series. Each operation divides the operated series into two parts based on an element, part is smaller than the demarcation value, and the other part is greater than the demarcation value. this demarcation value is generally called "pivot ". generally, the first number on the left is used as the boundary value. The sequence is divided into two parts based on the boundary value. The left part is smaller than the boundary value, and the right part is greater than the boundary value, repeat the left and right parts until the sorting is completed.

Take the series, as an example to describe how to execute a fast Sorting Algorithm:

1. Select the pivot of the series to be sorted. Generally, the first element of the series is used as the pivot. In this series, we select the first element 14 as the pivot, n133 = 14.

2. Set two pointers, I and j, respectively pointing to the first element and the end element of the series. I pointing to the first element 14, and j pointing to the end element 28:

3. Move forward the tail pointer j to point to the first element less than pivot (14) starting from the end of the series, and replace the element with the position pointed to by the head pointer I. _ nArray [I] = _ nArray [j]. as follows:

When this operation is performed for the first time, the value pointed to by the I pointer is actually the pivot value. the operation here can be understood as that the value pointed to by the I pointer has been replaced to the pivot. At this time, the I point is already a blank space. Fill in the space with the element found less than the pivot.

4. Move the header pointer I backward to point to the first element greater than pivot (14) from the header of the series, and replace the element with the position pointed by the end pointer j. _ nArray [j] = _ nArray [I]. as follows:

It can also be understood that the value pointed to by the j pointer has been replaced by a vacant position at. j in the previous operation.

5. Repeat Steps 3 and 4 until I = j ends.

6. After exiting the loop, I and j must point to the same position. the value of the element at the front of the position is smaller than the pivot. the value of the rear element at this position is greater than the pivot. obviously, the position I and j point at the same time should be the pivot "New Home ". _ nArray [I] = native. for example:

At this point, the sorting ends. The first element of the series to be sorted divides the series into two smaller parts than its size. For example:

Next, we will perform the same sorting operation on the two small and small molecular sequences.

So "recursion" until the entire sequence is sorted.

 

Below is the c # implementation code

  

Static void Main (string [] args) {Console. writeLine ("Enter the series to be sorted (separated by \", \ "):"); string _ s = Console. readLine (); string [] _ sArray = _ s. split (",". toCharArray (); int _ nLength = _ sArray. length; int [] _ nArray = new int [_ nLength]; for (int I = 0; I <_ nLength; I ++) {_ nArray [I] = Convert. toInt32 (_ sArray [I]);} var list = _ nArray. toList (); QuickSort (list, 0, _ nLength-1); foreach (var I in list) {Console. writeLine (I. toString ();} while (true) {Thread. sleep (10000) ;}}// obtain the position of the pivot after the pivot value is diverted between the left and right sides. private static int Division (List <int> list, int left, int right) {while (left <right) {int num = list [left]; // use the first element as the pivot if (num> list [left + 1]) {list [left] = list [left + 1]; list [left + 1] = num; left ++;} else {int temp = list [right]; list [right] = list [left + 1]; list [left + 1] = temp; right --;} Console. writeLine (string. join (",", list);} Console. writeLine ("-------------- \ n"); return left; // point to the position of the pivot at this time} private static void QuickSort (List <int> list, int left, int right) {if (left <right) {int I = Division (list, left, right); // sort the left part of the pivot in QuickSort (list, I + 1, right ); // sort the right part of the pivot. QuickSort (list, left, I-1 );}}

  


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

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.