Skip to main content

Posts

Showing posts from April, 2020

Quicksort in C#

using System; public class Test { public static void Main() { var unsorted = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(unsorted,x=>int.Parse(x)); int n= a.Length; Quicksort(a,0,n-1); for(int i=0;i<n;i++) { Console.WriteLine(a[i]); } } public static void Quicksort(int[] a,int low,int high) { if(low<high) { int pi= Partition(a,low,high); Quicksort(a,low,pi-1); Quicksort(a,pi+1,high); } } public static int Partition(int[]a,int low,int high) { int pivot = a[high]; int i = low-1; for(int j=low;j<=high-1;j++) { if(a[j]<pivot) { i++; int temp=a[i]; a[i]=a[j]; a[j]=temp; } } int temp2 = a[high]; a[high]=a[i+1]; a[i+1]=temp2; return i+1; } }