func TestMergeSort(t *testing.T) { arr := array.NewWithElements(3, 6, 9, 1, 0, 11, 8) fmt.Printf("before mergesort: ") arr.Print() MergeSort(arr, 0, arr.Size()-1) fmt.Printf("after mergesort: ") arr.Print() expected := array.NewWithElements(0, 1, 3, 6, 8, 9, 11) if !arr.IsMemeberWiseEqual(expected) { t.Errorf("Sorting error") } }
func TestHeapSort(t *testing.T) { arr := array.NewWithElements(4, 6, 3, 22, 34, 5, 9, 20) expected := array.NewWithElements(3, 4, 5, 6, 9, 20, 22, 34) HeapSort(arr) for i := 0; i < expected.Size(); i++ { item1, _ := arr.Get(i) item2, _ := expected.Get(i) if item1 != item2 { t.Errorf("Item[%d] = %d is not equal to the sorted array Item[%d] = %d", i, item1, i, item2) } } }
func TestQuickSort(t *testing.T) { arr := array.NewWithElements(3, 6, 9, 1, 0, 11, 8) fmt.Printf("before quick sort: ") arr.Print() // choose how to select pivot element fn := SelectRandomPivot() //fn := SelectRightAsPivot() QuickSort(arr, 0, arr.Size()-1, fn) fmt.Printf("after quick sort: ") arr.Print() expected := array.NewWithElements(0, 1, 3, 6, 8, 9, 11) if !arr.IsMemeberWiseEqual(expected) { t.Errorf("Sorting error") } }
func TestHeap(t *testing.T) { arr := arraylist.NewWithElements(4, 6, 3, 22, 34, 5, 9, 20) heap := New(arr) heap.data.Print() if head, _ := heap.data.Get(0); head != 34 { t.Errorf("heapfiy failed") } }