public static List <SortableObject> BucketSort(ListObject x, int n) { List <SortableObject> result = new List <SortableObject>(); int numOfBuckets = 10; List <SortableObject>[] buckets = new List <SortableObject> [numOfBuckets]; for (int i = 0; i < numOfBuckets; i++) { buckets[i] = new List <SortableObject>(); } for (int i = 0; i < n; i++) { int buckitChoice = (x.GetIndex(i).Number / numOfBuckets); buckets[buckitChoice].Add(x.GetIndex(i)); } for (int i = 0; i < numOfBuckets; i++) { SortableObject[] temp = BubbleSortList(buckets[i], x); result.AddRange(temp); } return(result); }
public static SortableObject[] BubbleSortList(List <SortableObject> input, ListObject x) { for (int i = 0; i < input.Count; i++) { for (int j = i; j < input.Count; j++) { if (input[i] > input[j]) { SortableObject temp = input[i]; input[i] = input[j]; input[j] = temp; } } } return(input.ToArray()); }
public static void Test_Array_List(int seed, int n) { string filename = @"mydataarray.dat"; ListObject data = new ListObject(filename, n, seed); Stopwatch sw = new Stopwatch(); sw.Start(); List <SortableObject> SortableList = BucketSortList.BucketSort(data, n); sw.Stop(); /* * foreach(SortableObject a in SortableList) * { * Console.WriteLine(a.ToString()); * }*/ Console.WriteLine("{1,9} took => {0}", sw.Elapsed, n); }