Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program that sorts an array of strings using the quick sort algorithm (find it in Wikipedia).
- using System;
- class QuickSortAlg
- {
- static void Main()
- {
- Console.Write("Enter the array length: ");
- int length = int.Parse(Console.ReadLine());
- string[] myArray = new string[length];
- for (int i = 0; i < length; i++)
- {
- Console.Write("Enter the string[{0}]: ", i + 1);
- myArray[i] = Console.ReadLine();
- }
- QuickSort(myArray, 0, length-1);
- for (int i = 0; i < length; i++)
- {
- Console.WriteLine(myArray[i]);
- }
- }
- static void QuickSort (string[] myArray, int left, int right)
- {
- int midIndex = (left + right) / 2;
- int i = left, j = right;
- while (i < j)
- {
- while (myArray[i].CompareTo(myArray[midIndex]) < 0) i++;
- while (myArray[j].CompareTo(myArray[midIndex]) > 0) j--;
- string changeString = myArray[i];
- if (i <= j)
- {
- myArray[i] = myArray[j];
- myArray[j] = changeString;
- i++;
- j--;
- }
- }
- if (left < j)
- {
- QuickSort(myArray, left, j);
- }
- if (right > i)
- {
- QuickSort(myArray, i, right);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement