Two years of working with. net, I learned the Data StructureAlgorithmIt has never been used in actual work. I have nothing to worry about recently. I want to learn more about simple data structure algorithms. today, it took me one afternoon to complete the "quick sorting" in the sorting process. This is the first article to settle in the blog Park! After thinking, do you want to put it on the homepage? Finally, I decided to put it with a thick face and courage. however, it is hard to feel uneasy. although the content is very simple, I did write it with my heart, hoping it could be seen. now, let's talk a little bit about it. You are ready to scold me! If the administrator Thinks this article is inappropriate, please handle it at will.
Quick sortingThis operation performs several operations on the sorted series in a recursive manner. Each operation divides the operated series into two parts based on a certain element, and the part is smaller than the demarcation value, the other part is greater than the demarcation value. this demarcation value is generally called "pivot ".
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 # implementationCode:
1 Using System;
2
3 Public Class Sort
4 {
5 Public Class Quick_sort
6 {
7 Private Static Int Quicksort_once ( Int [] _ Pnarray, Int _ Pnlow, Int _ Pnhigh)
8 {
9 Int Nqueue = _ Pnarray [_ pnlow]; // Pivot the first element
10 Int I = _ Pnlow, J = _ Pnhigh;
11
12 While (I < J)
13 {
14 // From right to left, find the first element less than nqueue
15 While (_ Pnarray [J] > = Nqueue && I < J) j -- ;
16 // At this point, J has pointed to the first element with a value smaller than nqueue on the right.
17 // Execute replacement
18 _ Pnarray [I] = _ Pnarray [J];
19 // From left to right, find the first element greater than nqueue
20 While (_ Pnarray [I] <= Nqueue && I < J) I ++ ;
21 // At this point, I has pointed to the first nsung element from the left end.
22 // Execute replacement
23 _ Pnarray [J] = _ Pnarray [I];
24 }
25
26 // The while loop is launched. At this point, it must be I = J.
27 // I (or J) points to the Pivot Position, locates the pivot of the sort and returns the position
28 _ Pnarray [I] = Npivot;
29 Return I;
30 }
31
32 Private Static Void Quicksort ( Int [] _ Pnarray, Int _ Pnlow, Int _ Pnhigh)
33 {
34 If (_ Pnlow > = _ Pnhigh) Return ;
35
36 Int _ N1_tindex = Quicksort_once (_ pnarray, _ pnlow, _ pnhigh );
37 // Sort the left end of the Pivot
38 Quicksort (_ pnarray, _ pnlow, _ n1_tindex - 1 );
39 // Sort the right end of the Pivot
40 Quicksort (_ pnarray, _ n1_tindex + 1 , _ Pnhigh );
41 }
42
43 Public Static Void Main ()
44 {
45 Console. writeline ( " Enter the series to be sorted (\ " ,\ " Split ): " );
46 String _ S = Console. Readline ();
47 String [] _ Sarray = _ S. Split ( " , " . Tochararray ());
48 Int _ Nlength = _ Sarray. length;
49 Int [] _ Narray = New Int [_ Nlength];
50 For ( Int I = 0 ; I < _ Nlength; I ++ )
51 {
52_ Narray [I]=Convert. toint32 (_ sarray [I]);
53}
54 Quicksort (_ narray, 0 , _ Nlength - 1 );
55 Console. writeline ( " The sorted sequence is: " );
56 Foreach ( Int _ N In _ Narray)
57 {
58Console. writeline (_ n. tostring ());
59}
60 }
61 }
62 }
63