Advertisement
BackoChan

14. Quicksort algo

Dec 11th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. //Write a program that sorts an array of strings
  2. //using the quick sort algorithm (find it in Wikipedia).
  3.  
  4. using System;
  5.  
  6.     class QuickSortAlgo
  7.     {
  8.         static void Main()
  9.         {
  10.             Console.Write("Number of elemetns of the array: ");
  11.             int n = int.Parse(Console.ReadLine());
  12.             int[] array = new int[n];
  13.  
  14.             //populating the array
  15.             for (int elements = 0; elements < array.Length; elements++)
  16.             {
  17.                 Console.Write("Array 1, Element [{0}]: ", elements);
  18.                 int inputNumber = int.Parse(Console.ReadLine());
  19.                 array[elements] = inputNumber;
  20.             }
  21.             int startElement = 0;
  22.             int endElement = n - 1;
  23.             int[] secondaryArray = new int[n*2];
  24.            
  25.             //solve the task by declaring another array with double size than the first one,
  26.             //and counting the positions on the left and on the right from the middle of the new array
  27.             while (true)
  28.             {
  29.                 int pivot = array[startElement];
  30.                 secondaryArray[secondaryArray.Length / 2] = array[startElement];
  31.                 int leftElementCounter = 0;
  32.                 int rightElementCounter = 0;
  33.                 for (int i = startElement + 1; i <= endElement; i++)
  34.                 {
  35.                     if (pivot < array[i])
  36.                     {
  37.                         leftElementCounter++;
  38.                         secondaryArray[n + leftElementCounter] = array[i];
  39.                     }
  40.                     else if (pivot > array[i])
  41.                     {
  42.                         rightElementCounter++;
  43.                         secondaryArray[n - rightElementCounter] = array[i];
  44.                     }
  45.                 }
  46.                
  47.                 //transfer data to 1st array;
  48.                 int helper = 0;
  49.                 for (int i = startElement; i <= array.Length - 1; i++)
  50.                 {
  51.                     array[i] = secondaryArray[n - rightElementCounter + helper];
  52.                     helper++;
  53.                 }
  54.                
  55.                 //check if the array is sorted.
  56.                 int checker = 0;
  57.                 for (int i = 0; i < array.Length - 1; i++)
  58.                 {
  59.                     if (array[i] > array[i + 1])
  60.                     {
  61.                         startElement = i;
  62.                         break;
  63.                     }
  64.                     else if (array[i] < array[i + 1])
  65.                     {
  66.                         checker++;
  67.                     }
  68.                 }
  69.                 if (checker == n - 1)
  70.                 {
  71.                     break;
  72.                 }
  73.                
  74.             }
  75.             for (int i = 0; i < array.Length; i++)
  76.             {
  77.                 Console.Write(array[i] + " ");
  78.             }
  79.             Console.WriteLine();
  80.            
  81.         }
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement