C # Fast (Binary) sorting

Source: Internet
Author: User

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;        }    }}

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.