Advertisement
stanevplamen

02.1.14.2.QuickSortStrings

May 22nd, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. // sort by string lenght
  5.  
  6. class QuickSort
  7. {
  8.     static List<int> QuickSortEmpement(List<int> unsortedList)
  9.     {
  10.         if (unsortedList.Count <= 1)
  11.         {
  12.             return unsortedList;
  13.         }
  14.         int pivot = unsortedList.Count / 2;
  15.         int pivotValue = unsortedList[pivot];
  16.         unsortedList.RemoveAt(pivot);
  17.         List<int> lesser = new List<int>();
  18.         List<int> greater = new List<int>();
  19.         foreach (int element in unsortedList)
  20.         {
  21.             if (element <= pivotValue)
  22.             {
  23.                 lesser.Add(element);
  24.             }
  25.             else
  26.             {
  27.                 greater.Add(element);
  28.             }
  29.         }
  30.         List<int> result = new List<int>();
  31.         result.AddRange(QuickSortEmpement(lesser));
  32.         result.Add(pivotValue);
  33.         result.AddRange(QuickSortEmpement(greater));
  34.         return result;
  35.  
  36.     }
  37.     static void Main()
  38.     {
  39.  
  40.         List<string> arrayList = new List<string> { "aaaaa5", "ppppppp7", "xxxxxx6", "zzzz4", "cc2", "s1", "hhh3"};      
  41.         List<int> array = new List<int>();
  42.         //List<int> index = new List<int>();
  43.        
  44.         int counterChar = 0;
  45.         int counterString = 0;
  46.         foreach (string str in arrayList)
  47.         {
  48.             foreach (char c in str)
  49.             {
  50.                 counterChar++;
  51.             }
  52.             array.Add(counterChar);
  53.             //index.Add(counterString);
  54.             counterString++;
  55.             counterChar = 0;
  56.         }
  57.  
  58.         List<int> sortedArray = QuickSortEmpement(array);
  59.  
  60.         counterChar = 0;
  61.  
  62.         foreach (var item in sortedArray)
  63.         {
  64.             foreach (string str in arrayList)
  65.             {
  66.                 counterChar = 0;
  67.                 foreach (char c in str)
  68.                 {
  69.                     counterChar++;
  70.                 }
  71.                 array.Add(counterChar);
  72.                 if (counterChar == item)
  73.                 {
  74.                     Console.WriteLine(str);
  75.                 }
  76.             }
  77.         }
  78.         Console.WriteLine();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement