The idea of fast (Binary) sorting is to divide the array into two sides and use a node v as the border (set the value of this node to V). All the elements on the left of node v are smaller than v, all elements on the Right of node v are greater than v.
In this way, the entire array is ordered at the end.
The idea of dividing the subscripts into two sides is as follows (start is the initial subscript and end is the end subscript ):
Select v = A [end] As the mediation point
Start scanning from start, stop when a [++ I] <v, and then start scanning a [-- J]> V from end, exchange a [I] And a [J]
Scanning starts from I + 1, and scanning starts from the J-1 on the other side. Note the boundary condition if (I> = J) Break. After scanning is complete, the position where V should be placed is found, and swap (Ref A [I], Ref A [end]) is switched. this completes a division.
The average time complexity of this algorithm is nlgn, and the worst case is the square of N. This algorithm is suitable for sorting large data volumes:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace QuickSort{ class Program { static void Main(string[] args) { int[] a ={1,6,4,3,8,5,21,9,31,81,101,55,62, 151, 7,2,10}; quicksort(a, 0, a.Length-1); for (int i = 0; i < a.Length; i++) { Console.WriteLine(a[i]); } Console.Read(); } static void quicksort(int[] a, int start, int end) { int i; if (end - start == 1) { if (a[start] > a[end]) { Swap(ref a[start], ref a[end]); } return; } if (end - start == 0) { return; } i = partition(a, start, end); if (i > start) { quicksort(a, start, i - 1); } if (i < end) { quicksort(a, i + 1, end); } } static int partition(int[] a, int start, int end) { int i = start-1; int j = end; int v = a[end]; for (; ; ) { while (i < end && a[++i] < v) ; while (j > start && a[--j] > v) ; if (i > = j) break; Swap(ref a[i], ref a[j]); } Swap(ref a[i], ref a[end]); return i; } static void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; } }}