sorts test Algorithm

The Sorts Test Algorithm is a procedure used to evaluate and compare the efficiency of various sorting algorithms in computer science. Sorting algorithms are essential in the organization and management of data, as they arrange elements in a specific order, either ascending or descending, based on the values of the data. The Sorts Test Algorithm involves subjecting these sorting algorithms to a series of tests using different data sets and conditions, such as random, partially sorted, or reversed data, in order to gauge their performance in terms of speed, memory usage, and stability. The results of these tests help in determining the most suitable sorting algorithm for a particular application, taking into account the specific characteristics of the data set and the desired sorting criteria. There are several popular sorting algorithms, such as Bubble Sort, Quick Sort, Merge Sort, and Insertion Sort, each with its own unique approach to organizing data. During the Sorts Test Algorithm, the performance of these algorithms is analyzed based on factors like time complexity, which indicates the number of operations required to complete the sorting process, and space complexity, which refers to the amount of memory needed for the algorithm to function. Furthermore, the stability of the algorithms is assessed, as it determines whether the relative order of elements with equal values is maintained after sorting. By comparing the different algorithms based on these factors, one can make an informed decision when choosing the optimal sorting technique for a specific task, ensuring efficient data organization and processing in various applications.
package sorts

import "testing"

//BEGIN TESTS

func TestBubble(t *testing.T) {
	for _, test := range sortTests {
		actual := bubbleSort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}

func TestSelection(t *testing.T) {
	for _, test := range sortTests {
		actual := selectionSort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}

func TestInsertion(t *testing.T) {
	for _, test := range sortTests {
		actual := insertionSort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}

func TestMerge(t *testing.T) {
	for _, test := range sortTests {
		actual := Mergesort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}

func TestHeap(t *testing.T) {
	for _, test := range sortTests {
		actual := heapSort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}

func TestQuick(t *testing.T) {
	for _, test := range sortTests {
		actual := quickSort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}

func TestShell(t *testing.T) {
	for _, test := range sortTests {
		actual := shellSort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}

/*func TestTopological(t *testing.T) {
	for _, test := range sortTests {
		actual := topologicalSort(test.input)
		pos, sorted := compareSlices(actual, test.expected)
		if !sorted {
			if pos == -1 {
				t.Errorf("test %s failed due to slice length changing", test.name)
			}
			t.Errorf("test %s failed at index %d", test.name, pos)
		}
	}
}*/

//END TESTS

//BEGIN BENCHMARKS
func BenchmarkBubble(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			bubbleSort(test.input)
		}
	}
}

func BenchmarkSelection(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			selectionSort(test.input)
		}
	}
}

func BenchmarkInsertion(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			insertionSort(test.input)
		}
	}
}

func BenchmarkMerge(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			Mergesort(test.input)
		}
	}
}

func BenchmarkHeap(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			heapSort(test.input)
		}
	}
}

func BenchmarkQuick(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			quickSort(test.input)
		}
	}
}

func BenchmarkShell(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			shellSort(test.input)
		}
	}
}

/*func BenchmarkTopological(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range sortTests {
			topologicalSort(test.input)
		}
	}
}*/

//END BENCHMARKS

func compareSlices(a []int, b []int) (int, bool) {
	if len(a) != len(b) {
		return -1, false
	}
	for pos := range a {
		if a[pos] != b[pos] {
			return pos, false
		}
	}
	return -1, true
}

LANGUAGE:

DARK MODE: