Click here to Skip to main content
Licence 
First Posted 26 Oct 2006
Views 51,867
Bookmarked 21 times

QuickSort Algorithm using Generics in C# 2.0

By | 26 Oct 2006 | Article
A QuickSort algorithm implementation using Generics in C# 2.0.

Screen Shot of test program

Introduction

Quick Sort is the fastest sorting algorithm. Quick sort uses the divide and conquer strategy. In this algorithm, I have implemented the Quick Sort algorithm using Generics in C# 2.0.

Background

The Quick Sort algorithm has following steps:

  1. If there are one or less elements in the array to be sorted, return immediately.
  2. Pick an element in the array to serve as a "pivot" point. (Usually the left-most element in the array is used.)
  3. Split the array into two parts - one with elements larger than the pivot and the other with elements smaller than the pivot.
  4. Recursively repeat the algorithm for both halves of the original array.

Using the code

Generics in C# are similar to templates in C++. Using Generics, I can use the same piece of code for sorting int, float, and double. The Generics class for a Quick Sort is as follows:

public class QuickSort <T> where T:IComparable
{
    T[] input;
    
    public QuickSort(T[] values)
    {
        input = new T[values.Length];
        for (int i = 0; i < values.Length; i++)
        {
            input[i] = values[i];
        }
        
    }

    public T[] Output
    {
        get
        {
            return input;
        }
    }
    public void Sort()
    {
        Sorting(0, input.Length-1);
    }
    public int getPivotPoint(int begPoint, int endPoint)
    {
        int pivot = begPoint;
        int m = begPoint+1;
        int n = endPoint;
        while ((m < endPoint) && 
               (input[pivot].CompareTo(input[m]) >= 0))
        {
            m++;
        }

        while ((n > begPoint) && 
               (input[pivot].CompareTo(input[n]) <= 0))
        {
            n--;
        }
        while (m < n)
        {
            T temp = input[m];
            input[m] = input[n];
            input[n] = temp;

            while ((m < endPoint) && 
                   (input[pivot].CompareTo(input[m]) >= 0))
            {
                m++;
            }

            while ((n > begPoint) && 
                   (input[pivot].CompareTo(input[n]) <= 0))
            {
                n--;
            }

        }
        if (pivot != n)
        {
            T temp2 = input[n];
            input[n] = input[pivot];
            input[pivot] = temp2;
            
        }
        return n;

    }
    public void Sorting(int beg,int end)
    {
        if (end==beg)
        {
            return;
        }
        else
        {
            int pivot = getPivotPoint(beg,end);
            if(pivot > beg)
            Sorting(beg, pivot-1);
            if(pivot < end)
            Sorting(pivot+1, end);
        }
    }
}

Points of Interest

The CompareTo method is used for comparing the generic type variables. ‘<’ and ‘>’ cannot be used for comparing generic variables because it works only with certain types. As generic parameter can used as any type of parameter, it will throw a compiler error:

Operator '>' cannot be applied to operands of type 'T' and 'T'.

Operator '<' cannot be applied to operands of type 'T' and 'T'.

Reference

For more information on the Quick Sort algorithm, see here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MaheshBabu.ap



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDo not use the first element for the pivot. PinmemberQwertie18:59 16 May '11  
GeneralMy vote of 5 PinmemberThe_Ant15:21 3 Aug '10  
GeneralMy vote of 1 PinmemberAkhil Gurua7:38 19 Feb '10  
GeneralQuestionable Performance PinmemberAtomicMonster5:07 5 Mar '08  
GeneralStatic it! Pinmemberk^s22:52 30 May '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Oct 2006
Article Copyright 2006 by MaheshBabu.ap
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid