Advertisement
Guest User

ShuffleSort

a guest
Dec 19th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. /* ShuffleSort, as seen on xkcd #1185 "Ineffective Sorts"
  2.  * Author: Alex Burgess
  3.  */
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8.  
  9. namespace ShuffleSort
  10. {
  11.     static class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             List<String> theList;
  16.             if (args.Length > 0)
  17.             {
  18.                 theList = args.ToList<String>();
  19.             }
  20.             else
  21.             {
  22.                 Console.WriteLine("Please enter some arguments next time.");
  23.                 theList = new List<string>();
  24.                 theList.Add("e");
  25.                 theList.Add("c");
  26.                 theList.Add("a");
  27.                 theList.Add("b");
  28.                 theList.Add("d");
  29.             }
  30.             List<String> theUnsortedList = new List<string>();
  31.             theList.ForEach((item) =>
  32.             {
  33.                 theUnsortedList.Add((string)item.Clone());
  34.             });
  35.  
  36.             Console.Write("Your original list: ");
  37.             theList.PrintList();
  38.  
  39.             DateTime normalSortStart = DateTime.Now;
  40.             theUnsortedList.Sort();
  41.             DateTime normalSortFinish = DateTime.Now;
  42.             TimeSpan normalSortTimeTaken = normalSortFinish.Subtract(normalSortStart);
  43.             Console.WriteLine("It took {0} seconds and {1} milliseconds to sort using List.Sort() (i.e. normally)", normalSortTimeTaken.Seconds, normalSortTimeTaken.Milliseconds);
  44.  
  45.             Console.WriteLine("Sorting the list by shuffling...");
  46.             Console.WriteLine();
  47.  
  48.             DateTime startTime = DateTime.Now;
  49.             UInt64 count = 0;
  50.  
  51.             while (!IsSorted(theList))
  52.             {
  53.                 theList.Shuffle();
  54.                 count++;
  55.             }
  56.  
  57.             DateTime finishTime = DateTime.Now;
  58.             Console.Write("Your sorted list:   ");
  59.             theList.PrintList();
  60.             TimeSpan timeTaken = finishTime.Subtract(startTime);
  61.             Console.WriteLine("It took {0} hours, {1} minutes, {2} seconds and {3} milliseconds to sort.", timeTaken.Hours, timeTaken.Minutes, timeTaken.Seconds, timeTaken.Milliseconds);
  62.             Console.WriteLine("There were {0:n0} lists generated before the list was sorted.", count);
  63.  
  64.            
  65.  
  66. #if DEBUG
  67.             Console.WriteLine("Press any key to exit.");
  68.             Console.ReadKey();      //prevent console from closing in Visual Studio after debugging
  69. #endif
  70.             }
  71.  
  72.         static void PrintList<String>(this List<String> list)
  73.         {
  74.             for (int i = 0; i < list.Count - 1; i++)
  75.             {
  76.                 Console.Write(list[i] + ",");
  77.             }
  78.             Console.WriteLine(list[list.Count - 1]);
  79.         }
  80.  
  81.  
  82.         static void Shuffle<String>(this List<String> list)
  83.         {
  84.             Random rand = new Random();
  85.             int n = list.Count;
  86.             while (n > 1)
  87.             {
  88.                 n--;
  89.                 int k = rand.Next(n + 1);
  90.                 String value = list[k];
  91.                 list[k] = list[n];
  92.                 list[n] = value;
  93.             }
  94.         }
  95.  
  96.         static bool IsSorted(List<String> list)
  97.         {
  98.             for (int i = 1; i < list.Count; i++)
  99.             {
  100.                 String current = list[i];
  101.                 String previous = list[i-1];
  102.                 if (String.Compare(current, previous, true) < 0)        //is value greater than the previous one?
  103.                 {
  104.                     return false;           //no - list is not sorted
  105.                 }
  106.             }
  107.             return true;                    //could not find a counterexample so list must be sorted
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement