Advertisement
gride29

quicksort

Nov 19th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using System;
  2.  
  3. namespace zadanie2
  4. {
  5.     class Program
  6.     {
  7.         static void quickSort(string[] a, int start, int end)
  8.         {
  9.             // index for the "left-to-right scan"
  10.             int i = start;
  11.             // index for the "right-to-left scan"
  12.             int j = end;
  13.  
  14.             // only examine arrays of 2 or more elements.
  15.             if (j - i >= 1)
  16.             {
  17.                 // The pivot point of the sort method is arbitrarily set to the first element int the array.
  18.                 string pivot = a[i];
  19.                 // only scan between the two indexes, until they meet.
  20.                 while (j > i)
  21.                 {
  22.                     // from the left, if the current element is lexicographically less than the (original)
  23.                     // first element in the String array, move on. Stop advancing the counter when we reach
  24.                     // the right or an element that is lexicographically greater than the pivot String.
  25.                     while (a[i].CompareTo(pivot) <= 0 && i < end && j > i)
  26.                     {
  27.                         i++;
  28.                     }
  29.                     // from the right, if t he current element is lexicographically greater than the (original)
  30.                     // first element in the String array, move on. Stop advancing the counter when we reach
  31.                     // the left or an element that is lexicographically less than the pivot String.
  32.                     while (a[j].CompareTo(pivot) >= 0 && j > start && j >= i)
  33.                     {
  34.                         j--;
  35.                     }
  36.                     // check the two elements in the center, the last comparison before the scans cross.
  37.                     if (j > i)
  38.                         swap(a, i, j);
  39.                 }
  40.                 // At this point, the two scans have crossed each other in the center of the array and stop.
  41.                 // The left partition and right partition contain the right groups of numbers but are not
  42.                 // sorted themselves. The following recursive code sorts the left and right partitions.
  43.  
  44.                 // Swap the pivot point with the last element of the left partition.
  45.                 swap(a, start, j);
  46.                 // sort left partition
  47.                 quickSort(a, start, j - 1);
  48.                 // sort right partition
  49.                 quickSort(a, j + 1, end);
  50.             }
  51.         }
  52.  
  53.         static void swap(string[] a, int i, int j)
  54.         {
  55.             string temp = a[i];
  56.             a[i] = a[j];
  57.             a[j] = temp;
  58.         }
  59.  
  60.  
  61.  
  62.         static void Main(string[] args)
  63.         {
  64.             string[] tab = { "Adam", "Zenek", "Barbara" };
  65.             quickSort(tab, 0, tab.Length - 1);
  66.             foreach (string slowo in tab)
  67.             {
  68.                 Console.WriteLine(slowo);
  69.             }
  70.             Console.ReadKey();
  71.         }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement