Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program
- {
- public static void Main()
- {
- int[] arr = {67, 12, 95, 56, 85, 1, 100, 23, 60, 9};
- int n = 10, i;
- Console.WriteLine("Quick Sort");
- Console.Write("Initial array is: ");
- for (i = 0; i < n; i++) {
- Console.Write(arr[i] + " ");
- }
- quickSort(arr, 0, 9);
- Console.Write("\nSorted Array is: ");
- for (i = 0; i < n; i++) {
- Console.Write(arr[i] + " ");
- }
- }
- static int Partition(int[] arr, int left, int right) {
- int pivot;
- pivot = arr[left];
- while (true) {
- while (arr[left] < pivot) {
- left++;
- }
- while (arr[right] > pivot) {
- right--;
- }
- if (left < right) {
- int temp = arr[right];
- arr[right] = arr[left];
- arr[left] = temp;
- } else {
- return right;
- }
- }
- }
- static void quickSort(int[] arr, int left, int right) {
- int pivot;
- if (left < right) {
- pivot = Partition(arr, left, right);
- if (pivot > 1) {
- quickSort(arr, left, pivot - 1);
- }
- if (pivot + 1 < right) {
- quickSort(arr, pivot + 1, right);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment