C # Quick Sort class

Source: Internet
Author: User
Tags sort

The basic idea of fast ordering is based on the divide-and-conquer strategy. For the input sequence AP. AR, if the scale is small enough to sort directly, otherwise three steps to deal with:

Decomposition (Divide): The sequence AP that will be entered. AR is divided into two non-null sequence AP. AQ and aq+1..ar to make AP. The value of any element in the AQ is less than the value of any element in the aq+1..ar.

Recursive Solution (Conquer): by recursion to P.. AQ and aq+1..ar are sorted.

Merge: Because the sort of the decomposed two subsequence is done in place, the AP ... AQ and aq+1..ar are sorted and do not need to perform any compute AP. AR has been sorted out.

This process of settlement is a basic step in accordance with the rule of partition. Therefore, the fast sorting method is one of the classical application examples of the partition method.

using System;

Namespace Vcquicksort
{
///<summary>
///classquicksort Quick Sort.
///Van Vicho
///</summary>
public class QuickSort
{
Public QuickSort ()
{
}

Private void Swap (ref int i,ref int J)
//swap two integer
{
int t;
t=i;
I=j;
j=t;
}

public void Sort (int [] list,int low,int High)
{
if (High<=low)
{
//only one element in array List
//so It do not need sort
return;
}
Else if (high==low+1)
{
//means two elements in array list
//so We just compare them
if (list[low]> List[high])
{
//exchange them
Swap (ref list[low],ref List[high);
return;
}
}
//more than 3 elements in the Arrary list
//begin QuickSort
Myquicksort (List,low,high);
}

public void myquicksort (int [] list,int low,int High)
{
if (Low {
int pivot=partition (li St,low,high);
Myquicksort (List,low, pivot-1);
Myquicksort (List,pivot+1,high);
}
}

Private int Partition (int [] list,int low,int High)
{
//get the pivot of the Arrary list
int pivot;
Pivot=list[low];
while (Low {
while (Low {
high--;
}
if (Low!=high)
{
Swap (ref list[low],ref List[high]);
low++;
}
while (Low {
low++;
}
if (Low!=high)
{
Swap (ref list[low],ref List[high]);
high--;
}
}
return to low;
}

}
}

Source: http://www.cnblogs.com/tanghuawei/archive/2006/10/19/533711.html

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.