Attributi degli algoritmi di ordinamento

Alcune helper functions

(* Linguaggio Pascal *)
const	MAX = 7;
type	myarray = array [0..MAX-1] of real;
var	nCompare, nMove;

function isSorted(n: word; var a: myarray): boolean;
var	i: word;
begin
	isSorted := true;
	for i := MIN to MAX do
		if (a[i-1]>a[i]) then
			isSorted := false;
end;

procedure analysis(n: word);
begin
  // ...
end;

var	a: myarray = (1.1, -0.7, -0.4, -0.1, 0.2, 0.5, 0.9);
begin
	nCompare := 0;
	nMove := 0;
	T0 := Time;
	case 1 of
	1:	insertionSort(length(a), a);
	2:	selectionSort(length(a), a);
	3:	swapSort(length(a), a);
	7:	mergeSort(length(a), a);
	end;
	T1 := Time;
	writeln('elapsed time: ', FormatDateTime('NN:SS.ZZZ', T1-T0));
	if not isSorted(length(a), a) then
		writeln('algoritmo ordinamento errato');
	for i := 0 to length(a)-1 do
		write(a[i]:5:1);
	writeln();
	writeln('nCompare/nMove: ', nCompare:9, nMove:9);

	analysis(max);
end.

void analysis(const unsigned n) {
	printf("Insertion (min): %5u %5u\n", (n-1),       2*(n-1));
	printf("Insertion (avg): %5u %5u\n", (n²+n-2)/4, (n*n+9*n-10)/4);
	printf("Insertion (max): %5u %5u\n", (n²+n)/2-1, (n*n+3*n-4)/2);

	printf("Selection (min): %5u %5u\n", (n²-n)/2,   3*(n-1));
	printf("Selection (avg): %5u %5u\n", (n²-n)/2,   (int)(n*(log(n)+0.577)));
	printf("Selection (max): %5u %5u\n", (n²-n)/2,   (n*n/4+3*(n-1)));

	printf("Bubble    (min): %5u %5u\n", (n²-n)/2,   0);
	printf("Bubble    (avg): %5u %5u\n", (n²-n)/2,   (n*n-n)*3/4/3);
	printf("Bubble    (max): %5u %5u\n", (n²-n)/2,   (n*n-n)*3/2);
}
/* Linguaggio C++ */
#include
#include
#include
#include
bool isSorted(std::vector vet[]) {
	return std::is_sorted(vet.begin(), vet.end());
}
void analysis(const unsigned n) {
	std::cout << "Insertion (min): " << (n-1)       << 2*(n-1)         << std::endl;
	std::cout << "Insertion (avg): " << (n*n+n-2)/4 << (n*n+9*n-10)/4) << std::endl;
	std::cout << "Insertion (max): " << (n*n+n)/2-1 << (n*n+3*n-4)/2   << std::endl;

	std::cout << "Selection (min): " << (n*n-n)/2   << 3*(n-1)                 << std::endl;
	std::cout << "Selection (avg): " << (n*n-n)/2   << (int)(n*(log(n)+0.577)) << std::endl;
	std::cout << "Selection (max): " << (n*n-n)/2   << (n*n/4+3*(n-1))         << std::endl;

	std::cout << "Bubble    (min): " << (n*n-n)/2   << 0               << std::endl;
	std::cout << "Bubble    (avg): " << (n*n-n)/2   << (n*n-n)*3/4/3   << std::endl;
	std::cout << "Bubble    (max): " << (n*n-n)/2   << (n*n-n)*3/2     << std::endl;
}
unsigned nCompare, nMove;
int main() {
	nCompare = 0;
	nMove = 0;
	std::vector a { 0.9, 0.5, 0.2, -0.1, -0.4, -0.7, -1.1, };
	switch (1) {
	case 1:	insertion_sort(a.begin(), a.end());
		break;
	case 2:	selection_sort(a.begin(), a.end());
		break;
	case 3:	swap_sort(a.begin(), a.end());
		break;
	case 5:	heap_sort(a.begin(), a.end());
		break;
	case 6:	quick_sort(a.begin(), a.end());
		break;
	case 7:	merge_sort(a.begin(), a.end());
		break;
	}
	if (!isSorted(a))
		std::cerr << "algoritmo ordinamento errato" << std::endl;

//	copy(std::begin(a), std::end(a), std::ostream_iterator(std::cout, " "));
//	std::cout << std::endl;

	for (auto i = a.begin(); i != a.end(); ++i)
		std::cout << std::setw(5) << *i;
	std::cout << std::endl;

	std::cout << "nCompare: " << nCompare << ", nMove: " << nMove << std::endl;

	analysis(max);

	return 0;
}

Algoritmi semplici

Sia dato un array a[MIN..MAX] per i cui elementi sia definita una relazione d'ordine parziale.

Algoritmi evoluti