1 3 4 4 4 5 5 6 7 8 26 32 187
This is a C# Program to implement quick sort.
This C# Program Implements Quick Sort.
Quicksort is a divide and conquer algorithm. Here Quicksort first divides a large array into two smaller sub-array: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays
Here is source code of the C# Program to Implement Quick Sort. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Implement Quick Sort */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace sortQuick { class quickSort { private int[] array = new int[20]; private int len; public void QuickSort() { sort(0, len - 1); } public void sort(int left, int right) { int pivot, leftend, rightend; leftend = left; rightend = right; pivot = array[left]; while (left < right) { while ((array[right] >= pivot) && (left < right)) { right--; } if (left != right) { array[left] = array[right]; left++; } while ((array[left] <= pivot) && (left < right)) { left++; } if (left != right) { array[right] = array[left]; right--; } } array[left] = pivot; pivot = left; left = leftend; right = rightend; if (left < pivot) { sort(left, pivot - 1); } if (right > pivot) { sort(pivot + 1, right); } } public static void Main() { quickSort q_Sort = new quickSort(); int[] array = { 4, 3, 1, 4, 6, 7, 5, 4, 32, 5, 26, 187, 8 }; q_Sort.array = array; q_Sort.len = q_Sort.array.Length; q_Sort.QuickSort(); for (int j = 0; j < q_Sort.len; j++) { Console.WriteLine(q_Sort.array[j]); } Console.ReadKey(); } } }
This C# program is used to implement quick sort. We are creating an object ‘q_sort’ variable for quickSort() function. Quick sort is a divide and conquer algorithm. Here Quicksort first divides a large array into two smaller sub-array the low elements and the high elements. Quicksort() procedure can then recursively sort the sub-arrays.
In Quicksort() procedure, the ‘Partition()‘ function which divides array into two part (not necessarily equal) and then recursively calling the quicksort() procedure on divided arrays.
The QuickSort() procedure chooses a pivot element and rearranges the array such that all elements smaller than pivot are put before the pivot and all bigger elements are put at the end. We have actually found the correct position for pivot element and return the index of pivot element after rearranging the array.
1 3 4 4 4 5 5 6 7 8 26 32 187