Created
July 22, 2015 12:41
-
-
Save ajmyers01/d5f2924180966abe2db7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| namespace RosettaCode.BogoSort | |
| { | |
| public static class BogoSorter | |
| { | |
| public static void Sort<T>(List<T> list) where T:IComparable | |
| { | |
| while (!list.isSorted()) | |
| { | |
| list.Shuffle(); | |
| } | |
| } | |
| private static bool isSorted<T>(this IList<T> list) where T:IComparable | |
| { | |
| if(list.Count<=1) | |
| return true; | |
| for (int i = 1 ; i < list.Count; i++) | |
| if(list[i].CompareTo(list[i-1])<0) return false; | |
| return true; | |
| } | |
| private static void Shuffle<T>(this IList<T> list) | |
| { | |
| Random rand = new Random(); | |
| for (int i = 0; i < list.Count; i++) | |
| { | |
| int swapIndex = rand.Next(list.Count); | |
| T temp = list[swapIndex]; | |
| list[swapIndex] = list[i]; | |
| list[i] = temp; | |
| } | |
| } | |
| } | |
| class TestProgram | |
| { | |
| static void Main() | |
| { | |
| List<int> testList = new List<int> { 3, 4, 1, 8, 7, 4, -2 }; | |
| BogoSorter.Sort(testList); | |
| foreach (int i in testList) Console.Write(i + " "); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment