vojta249

david's quick

Mar 30th, 2022 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         int[] arr = {67, 12, 95, 56, 85, 1, 100, 23, 60, 9};
  8.          int n = 10, i;
  9.          Console.WriteLine("Quick Sort");
  10.          Console.Write("Initial array is: ");
  11.          for (i = 0; i < n; i++) {
  12.             Console.Write(arr[i] + " ");
  13.          }
  14.          quickSort(arr, 0, 9);
  15.          Console.Write("\nSorted Array is: ");
  16.          for (i = 0; i < n; i++) {
  17.             Console.Write(arr[i] + " ");
  18.          }
  19.     }
  20.     static  int Partition(int[] arr, int left, int right) {
  21.          int pivot;
  22.          pivot = arr[left];
  23.          while (true) {
  24.             while (arr[left] < pivot) {
  25.                left++;
  26.             }
  27.             while (arr[right] > pivot) {
  28.                right--;
  29.             }
  30.             if (left < right) {
  31.                int temp = arr[right];
  32.                arr[right] = arr[left];
  33.                arr[left] = temp;
  34.             } else {
  35.                return right;
  36.             }
  37.          }
  38.       }
  39.    
  40.     static  void quickSort(int[] arr, int left, int right) {
  41.          int pivot;
  42.          if (left < right) {
  43.             pivot = Partition(arr, left, right);
  44.             if (pivot > 1) {
  45.                quickSort(arr, left, pivot - 1);
  46.             }  
  47.             if (pivot + 1 < right) {
  48.                quickSort(arr, pivot + 1, right);
  49.             }
  50.          }
  51.       }
  52.    
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment