Advertisement
Guest User

Untitled

a guest
Dec 19th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. /*
  2.  * Write a program that sorts an array of strings using the quick sort algorithm (find it in Wikipedia).
  3.  * this is the most useful explanation of QuickSort I found:
  4.  * https://d19vezwu8eufl6.cloudfront.net/algs4partI/recoded_videos%2Falgs4partI-35.mp4
  5.  */
  6. using System;
  7. class QuickSortAlg
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.  
  12.         string[] strArray =  { "This", "is", "Telerik", "academy", "C#", "2", "QuickSort", "exccercise" };
  13.                             // { "g", "f", "e", "d", "c", "b", "a" };
  14.                             // { "a", "b", "c", "d", "e", "f", "g" };
  15.                             // { "g", "gg", "a", "bb","c","ab","BB"};
  16.                             // { "g", "G", "a", "A", "a", "b", "B" };
  17.                            
  18.  
  19.         Console.WriteLine("Before sorting: ");
  20.         Console.WriteLine(string.Join(" ", strArray));
  21.  
  22.         QuickSort(strArray, 0, strArray.Length - 1);
  23.  
  24.  
  25.         Console.WriteLine("After sorting: ");
  26.         Console.WriteLine(string.Join(" ", strArray));
  27.  
  28.     }
  29.  
  30.     private static int PartitioningElements(string[] testStrArray, int lo, int hi)
  31.     {
  32.         int iKey = lo+1;
  33.         int jKey = hi;
  34.  
  35.         while (true)
  36.         {
  37.             // String.Compare(StrA,StrB, true) <0 when strA is less than strB
  38.             while (String.Compare(testStrArray[iKey], testStrArray[lo], true) < 0)
  39.             {
  40.                 if (iKey == hi)
  41.                 {
  42.                     break;
  43.                 }
  44.                 iKey++;
  45.             }
  46.             // String.Compare(StrA,StrB, true) >0 when strA is greater than strB
  47.             while (String.Compare(testStrArray[jKey], testStrArray[lo], true) > 0)
  48.             {
  49.                 if (jKey == lo)
  50.                 {
  51.                     break;
  52.                 }
  53.                 jKey--;
  54.             }
  55.             if (iKey >= jKey) { break; } //check if pointers cross
  56.                
  57.             //swap elements
  58.             var temp = testStrArray[iKey];
  59.             testStrArray[iKey] = testStrArray[jKey];
  60.             testStrArray[jKey] = temp;
  61.         }
  62.         //swap with jKey
  63.         var j = testStrArray[jKey];
  64.         testStrArray[jKey] = testStrArray[lo];
  65.         testStrArray[lo] = j;
  66.         return jKey;
  67.     }
  68.     private static void QuickSort(string[] testStrArray, int lo, int hi)
  69.     {
  70.         if (hi <= lo)
  71.         {
  72.             return;
  73.         }
  74.         int j = PartitioningElements(testStrArray, lo, hi);
  75.         QuickSort(testStrArray, lo, j - 1);
  76.         QuickSort(testStrArray, j + 1, hi);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement