Advertisement
stanevplamen

02.1.14.1.QuickSortStrings

May 22nd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. // sort by the first letter
  5.  
  6. class QuickSortStrings
  7. {
  8.     static void Main()
  9.     {
  10.         List<string> arrayList = new List<string> { "ppp7", "ttt8", "xxx9", "zzz10", "bbb2", "aaa1", "ccc3", "ddd4", "eee5", "fff6" };
  11.         Console.WriteLine("The current strings array is: ");
  12.         foreach (var item in arrayList)
  13.         {
  14.             Console.Write("{0} ", item);
  15.         }
  16.         Console.WriteLine();
  17.  
  18.         List<string> sortedArrayList = QuickSortFunction(arrayList);
  19.            
  20.         Console.WriteLine("The sorted strings array is: ");
  21.         foreach (var item in sortedArrayList)
  22.         {
  23.             Console.Write("{0} ", item);
  24.         }
  25.         Console.WriteLine();
  26.     }
  27.  
  28.     static List<string> QuickSortFunction(List<string> currentArrayList)
  29.     {
  30.         if (currentArrayList.Count <= 1)
  31.         {
  32.             return currentArrayList;
  33.         }
  34.  
  35.         int measuringSign = currentArrayList.Count / 2;
  36.         string valueSign = currentArrayList[measuringSign];
  37.         currentArrayList.RemoveAt(measuringSign);
  38.  
  39.         List<string> smaller = new List<string>();
  40.         List<string> bigger = new List<string>();
  41.  
  42.         int counter = 0;
  43.         foreach (var item in currentArrayList)
  44.         {
  45.             if (true)
  46.             {
  47.                 if (string.Compare(item, valueSign) < 0)
  48.                 {
  49.                     smaller.Add(item);
  50.                 }
  51.                 else
  52.                 {
  53.                     bigger.Add(item);
  54.                 }
  55.             }
  56.             counter++;
  57.         }
  58.         List<string> resultArrayList = new List<string>();
  59.         resultArrayList.AddRange(QuickSortFunction(smaller));
  60.         resultArrayList.Add(valueSign);
  61.         resultArrayList.AddRange(QuickSortFunction(bigger));
  62.         return resultArrayList;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement