Skip to content
This repository was archived by the owner on Dec 29, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions QuickSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// C# program for implementation of QuickSort
using System;

class GFG {

static int partition(int []arr, int low,
int high)
{
int pivot = arr[high];

int i = (low - 1);
for (int j = low; j < high; j++)
{
if (arr[j] <= pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

int temp1 = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp1;

return i+1;
}

static void quickSort(int []arr, int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);

quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}

static void printArray(int []arr, int n)
{
for (int i = 0; i < n; ++i)
Console.Write(arr[i] + " ");

Console.WriteLine();
}

public static void Main()
{
int []arr = {10, 7, 8, 9, 1, 5};
int n = arr.Length;
quickSort(arr, 0, n-1);
Console.WriteLine("sorted array ");
printArray(arr, n);
}
}