Venciity

[ Telerik C#] MultidimArrays 05-SortStrings

Feb 28th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Swap(string[] arr, int i, int j)
  6.     {
  7.         string t = arr[i];
  8.         arr[i] = arr[j];
  9.         arr[j] = t;
  10.     }
  11.  
  12.     static int Partition(string[] arr, int l, int r)
  13.     {
  14.         Swap(arr, new Random().Next(l, r + 1), r);
  15.         int pivot = arr[r].Length, i = l;
  16.  
  17.         for (int j = l; j < r; j++) if (arr[j].Length <= pivot) Swap(arr, i++, j);
  18.         Swap(arr, i, r);
  19.  
  20.         return i;
  21.     }
  22.  
  23.     static void QuickSort(string[] arr, int l, int r)
  24.     {
  25.         if (l >= r) return;
  26.  
  27.         int q = Partition(arr, l, r);
  28.  
  29.         QuickSort(arr, l, q - 1);
  30.         QuickSort(arr, q + 1, r);
  31.     }
  32.  
  33.     static void Main()
  34.     {
  35.         string[] arr = { "", "1", "22", "666666", "333", "55555", "4444" };
  36.  
  37.         QuickSort(arr, 0, arr.Length - 1);
  38.  
  39.         foreach (string item in arr) Console.WriteLine(item);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment