# C# - QuickSort

**URL:** https://forum.kirupa.com/t/c-quicksort/207163
**Category:** open source
**Created:** [November 15, 2006, 5:36am UTC](https://forum.kirupa.com/t/c-quicksort/207163 "2006-11-15T05:36:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [November 15, 2006, 5:36am UTC](https://forum.kirupa.com/t/c-quicksort/207163/1 "2006-11-15T05:36:32Z")

</div>

The following is an almost direct translation of the Flash version of Quicksort I created earlier: [http://www.kirupa.com/developer/actionscript/quickSort.htm](http://www.kirupa.com/developer/actionscript/quickSort.htm)

```auto
private static List<int> QuickSort(List<int> a, int left, int right)
{
    int i = left;
    int j = right;
    double pivotValue = ((left + right) / 2);
    int x = a[Convert.ToInt32(pivotValue)];
    int w = 0;
    while (i <= j)
    {
        while (a* < x)
        {
            i++;
        }
        while (x < a[j])
        {
            j--;
        }
        if (i <= j)
        {
            w = a*;
            a[i++] = a[j];
            a[j--] = w;
        }
    }
    if (left < j)
    {
        QuickSort(a, left, j);
    }
    if (i < right)
    {
        QuickSort(a, i, right);
    }
    return a;
}
// Quicksort in C#
static void Main(string[] args)
{
    List<int> tempData = new List<int>();
    tempData.Add(4);
    tempData.Add(24);
    tempData.Add(1);
    tempData.Add(3);
    tempData.Add(5);
    List<int> newList = QuickSort(tempData, 0, tempData.Count - 1);
    for (int b = 0; b < newList.Count; b++)
    {
        Console.WriteLine(newList**);
    }
}

```

:disco:
