private static void InsertionSortBucket(ref CustomLinkedList <int> linkedList) { CustomLinkedList <int> .Node current = linkedList.Root; while (current != null) { CustomLinkedList <int> .Node reverseCurrent = current; while (reverseCurrent.Prev != null && reverseCurrent.Data < reverseCurrent.Prev.Data) { int tempData = reverseCurrent.Data; reverseCurrent.Data = reverseCurrent.Prev.Data; reverseCurrent.Prev.Data = tempData; reverseCurrent = reverseCurrent.Prev; } current = current.Next; } }
private static int[] BucketSort(int[] arrayToSortRandom, int maxNumInArray) { CustomLinkedList <int>[] linkedListArray = new CustomLinkedList <int> [arrayToSortRandom.Length]; int[] targetArray = new int[arrayToSortRandom.Length]; for (int i = 0; i < arrayToSortRandom.Length; i++) { linkedListArray[i] = new CustomLinkedList <int>(); } //Putting in buckets for (int i = 0; i < arrayToSortRandom.Length; i++) { linkedListArray[arrayToSortRandom[i] / ((maxNumInArray + 1) / arrayToSortRandom.Length)].Insert(arrayToSortRandom[i]); } //Insertion sort each bucket for (int i = 0; i < linkedListArray.Length; i++) { InsertionSortBucket(ref linkedListArray[i]); } int runningIndexForTargetArray = 0; //Putting data from each bucket in order to the target array for (int i = 0; i < linkedListArray.Length; i++) { CustomLinkedList <int> .Node current = linkedListArray[i].Root; while (current != null) { targetArray[runningIndexForTargetArray] = current.Data; current = current.Next; runningIndexForTargetArray++; } } return(targetArray); }