Quicksort in C# -
class Programsaddaa{
static int[] quickSort(int[] arr) {
int pivot = arr[0];
arr[0] = arr[arr.Length-1];
arr[arr.Length-1] = pivot;
int i = 0;
for(int j=0; j<arr.Length-1; j++)
{
if(arr[j] <= pivot)
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
}
}
arr[arr.Length-1] = arr[i];
arr[i] = pivot;
return arr;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp));
int[] result = quickSort(arr);
textWriter.WriteLine(string.Join(" ", result));
textWriter.Flush();
textWriter.Close();
}
}
Post a Comment