## Infosys Interview Question

Java Developers**Country:**United States

In C# :

public int Partition(int[] a,int left,int right)

{

int pivot=a[left];

while(true)

{

while(a[left]<pivot)

left++;

while(a[right]>pivot)

right--;

if(left<right)

{

int temp=a[right];

a[right]=a[left];

a[left]=temp;

}

else

return right;

}

{

public int[] QuickSort(int[] a, int left,int right)

{

int pivot=Partition(a,left,right);

if(pivot>left)

QuickSort(a,left,pivot-1);

if(pivot<right)

QuickSort(a,pivot+1,right);

return a;

}

In C# :

public static int Partition(int[] a ,int left,int right)

{

int pivot = a[left];

while(true)

{

while (a[left] < pivot)

left++;

while (a[right] > pivot)

right--;

if(left<right)

{

int temp = a[right];

a[right] = a[left];

a[left] = temp;

}

else

{

return right;

}

}

}

public static void QuickSortRecur(int[] a ,int left, int right)

{

if(left<right)

{

int pivot = Partition(a, left, right);

//Console.Write(pivot);

if (pivot >left)

QuickSortRecur(a, left, pivot-1);

if (pivot < right)

QuickSortRecur(a, pivot+1, right);

}

}

- tarek.salah@badrit.com November 18, 2013