Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class QuickSort
- {
- static Random randomGenerator = new Random();
- static void Main()
- {
- int[] myArray = { 1, -10, -8, 7, -6, 3, 4, 5, 100, -100, 1000, 9999, 111111, -90, -60, 55, 54};
- Console.WriteLine("The original array is: ");
- Console.WriteLine(String.Join(", ", myArray));
- //in this list will be transfered the original array
- List<int> myList = new List<int>();
- //put all the values from the array into the list
- FillOriginalList(myArray, myList);
- int pivot = myList[myList.Count / 2];
- //process the result string to remove the exesssice spaces
- string newString = Sort(myList);
- while (newString.IndexOf(" ") != -1)
- {
- newString = newString.Replace(" ", " ");
- }
- Console.WriteLine("The arranged array is: ");
- Console.WriteLine(newString);
- }
- static string Sort(List<int> myList)
- {
- int lenList = myList.Count;
- if (lenList <= 1)
- {
- return String.Join(", ", myList) + " ";
- }
- else
- {
- //Random randomGenerator = new Random();
- int newPivotIndex = randomGenerator.Next(0, lenList);
- int pivot = myList[newPivotIndex];
- //here we put all elements less and equal to the pivot
- List<int> less = new List<int>();
- //here we put all elements bigger than the pivot
- List<int> bigger = new List<int>();
- //divide the elements into the 'less' and 'bigger' lists
- for (int i = 0; i < lenList; i++)
- {
- if ( myList[i] < pivot)
- {
- less.Add(myList[i]);
- }
- else
- {
- bigger.Add(myList[i]);
- }
- }
- return (Sort(less) + Sort(bigger));
- }
- }
- private static void FillOriginalList(int[] myArray, List<int> myList)
- {
- for (int i = 0; i < myArray.Length; i++)
- {
- myList.Add(myArray[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement