Viacheslav

Untitled

Jan 1st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. namespace HelloWorld
  4. {
  5.     class MainClass
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("Hello world");
  10.             int[] array = new int[]{3, 2, 4, 1, 0};
  11.  
  12.             Console.WriteLine(String.Join(", ", QuickSort2(array)));
  13.  
  14.  
  15.         }
  16.         public static int[] QuickSort(int[] arr)
  17.         {
  18.             if (arr.Length < 2)
  19.             {
  20.                 return arr;
  21.             }
  22.             List<int> left = new List<int>();
  23.             List<int> right = new List<int>();
  24.             int pivotindex = arr.Length / 2;
  25.             for (int i = 0; i < arr.Length; i++)
  26.             {
  27.                 if (i == pivotindex)
  28.                 {
  29.                     continue;
  30.                 }
  31.                 if (arr[i] > arr[pivotindex])
  32.                 {
  33.                     right.Add(arr[i]);
  34.                 }
  35.                 else
  36.                 {
  37.                     left.Add(arr[i]);
  38.                 }
  39.             }
  40.             List<int> result = new List<int>();
  41.             result.AddRange(QuickSort(left.ToArray()));
  42.             result.Add(arr[pivotindex]);
  43.             result.AddRange(QuickSort(right.ToArray()));
  44.  
  45.             return result.ToArray();
  46.  
  47.         }
  48.         public static int[] QuickSort2(int[] arr)
  49.         {
  50.             if (arr.Length < 2)
  51.             {
  52.                 return arr;
  53.             }
  54.  
  55.             var left = new int[arr.Length / 2 - 1];
  56.             int pivotindex = arr.Length / 2;
  57.             var right = new int[arr.Length - left.Length - 1];
  58.             for (int i = 0; i < arr.Length; i++)
  59.             {
  60.                 if (i == pivotindex)
  61.                 {
  62.                     continue;
  63.                 }
  64.                 if (arr[i] > arr[pivotindex])
  65.                 {
  66.                     right[i] = arr[i];
  67.                 }
  68.                 else
  69.                 {
  70.                     left[i] = arr[i];
  71.                 }
  72.             }
  73.             var result = new int[arr.Length];
  74.             QuickSort2(left);
  75.             QuickSort2(right);
  76.             int filling = 0;
  77.             for (int i = 0; i < left.Length + 1; i++)
  78.             {
  79.                 if (i == pivotindex)
  80.                 {
  81.                     result[i] = arr[pivotindex];
  82.                 }
  83.                 else
  84.                 {
  85.                     result[i] = left[i];
  86.  
  87.                 }
  88.  
  89.             }
  90.             for (int i = pivotindex + 1; i < arr.Length; i++)
  91.             {
  92.                 result[i] = right[i];
  93.             }
  94.  
  95.  
  96.             return result;
  97.  
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment