Advertisement
soxa

QuickSort

Dec 19th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. //Write a program that sorts an array of strings using the quick sort algorithm (find it in Wikipedia).
  4.  
  5. class Program
  6. {
  7.     static int Part(int[] arrays, int start, int end)
  8.     {
  9.         int pivot = arrays[end]; // Basic element
  10.         int index = start;
  11.  
  12.         for (int i = start; i < end; i++)
  13.         {
  14.             if (arrays[i] <= pivot)
  15.             {
  16.                 if (i == index) // Prevent array
  17.                 {
  18.                     index++;
  19.                     continue;
  20.                 }
  21.                 arrays[i] ^= arrays[index]; //exchange of values
  22.                 arrays[index] ^= arrays[i];
  23.                 arrays[i] ^= arrays[index];
  24.                 index++;
  25.             }
  26.         }
  27.         if (end == index) // Prevent array
  28.         {
  29.             return index;
  30.         }
  31.         else
  32.         {
  33.  
  34.             arrays[end] ^= arrays[index]; // exchange of values
  35.             arrays[index] ^= arrays[end];
  36.             arrays[end] ^= arrays[index];
  37.             return index;
  38.         }
  39.     }
  40.  
  41.     static void QuickSort(int[] array, int start, int end)
  42.     {
  43.         if (start < end)
  44.         {
  45.             int partitionIndex = Part(array, start, end);
  46.             QuickSort(array, start, partitionIndex - 1);
  47.             QuickSort(array, partitionIndex + 1, end);
  48.         }
  49.     }
  50.  
  51.     static void Main()
  52.     {
  53.         string text = Console.ReadLine();
  54.         int[] arrays = new int[text.Length];
  55.         for (int i = 0; i < text.Length; i++)
  56.         {
  57.             arrays[i] = Convert.ToInt32(text[i]);
  58.         }
  59.  
  60.         QuickSort(arrays, 0, (arrays.Length - 1));
  61.  
  62.         for (int i = 0; i < arrays.Length; i++)
  63.         {
  64.             Console.Write(Convert.ToChar(arrays[i]));
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement