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;
- using System.Collections.Generic;
- class QuickSort
- {
- static List<int> QuickSortEmpement(List<int> unsortedList)
- {
- if (unsortedList.Count <= 1)
- {
- return unsortedList;
- }
- int pivot = unsortedList.Count / 2;
- int pivotValue = unsortedList[pivot];
- unsortedList.RemoveAt(pivot);
- List<int> lesser = new List<int>();
- List<int> greater = new List<int>();
- foreach (int element in unsortedList)
- {
- if (element <= pivotValue)
- {
- lesser.Add(element);
- }
- else
- {
- greater.Add(element);
- }
- }
- List<int> result = new List<int>();
- result.AddRange(QuickSortEmpement(lesser));
- result.Add(pivotValue);
- result.AddRange(QuickSortEmpement(greater));
- return result;
- }
- static void Main()
- {
- Console.Write("Enter array length: ");
- int n = int.Parse(Console.ReadLine());
- List<int> array = new List<int>(n);
- for (int i = 0; i < n; i++)
- {
- Console.Write("Insert element {0} = ", i + 1);
- int g = int.Parse(Console.ReadLine());
- array.Add(g);
- }
- List<int> sortedArray = QuickSortEmpement(array);
- foreach (var item in sortedArray)
- {
- Console.Write("{0} ", item);
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment