import java.util.Vector;
import java.util.Date;

public class SortsTest {
	public static void main(String[] args) {
		ConsoleReader lettore = new ConsoleReader(System.in);
		Vector arr = new Vector();
		System.out.println("Lettura array di int (terminare con EOF)...");
		String inString = lettore.readLine();
		while(inString != null) {
			arr.add(new Integer(inString.trim()));
			inString = lettore.readLine();
		}
		System.out.println(" fatto.");
		
		Comparable[] carr = (Comparable[])arr.toArray(new Comparable[0]);
		System.out.println(isSorted(carr));
		System.out.print("Starting InsertionSort...");
		Date start = new Date();
		Sorts.insertionsort(carr);
		Date end = new Date();
		System.out.println(isSorted(carr));
		System.out.println(" fatto. " + (end.getTime() - start.getTime()) + "ms");

		carr = (Comparable[])arr.toArray(new Comparable[0]);
		System.out.println(isSorted(carr));
		System.out.print("Starting QuickSort...");
		start = new Date();
		Sorts.quicksort(carr);
		end = new Date();
		System.out.println(" fatto. " + (end.getTime() - start.getTime()) + "ms");

		carr = (Comparable[])arr.toArray(new Comparable[0]);
		System.out.println(isSorted(carr));
		System.out.print("Starting QuickSort2...");
		start = new Date();
		Sorts.quicksort2(carr);
		end = new Date();
		System.out.println(" fatto. " + (end.getTime() - start.getTime()) + "ms");

		carr = (Comparable[])arr.toArray(new Comparable[0]);
		System.out.println(isSorted(carr));
		System.out.print("Starting MergeSort...");
		start = new Date();
		Sorts.mergesort(carr);
		end = new Date();
		System.out.println(" fatto. " + (end.getTime() - start.getTime()) + "ms");

		carr = (Comparable[])arr.toArray(new Comparable[0]);
		System.out.println(isSorted(carr));
		System.out.print("Starting SelectionSort...");
		start = new Date();
		Sorts.selectionsort(carr);
		end = new Date();
		System.out.println(" fatto. " + (end.getTime() - start.getTime()) + "ms");

		carr = (Comparable[])arr.toArray(new Comparable[0]);
		System.out.println(isSorted(carr));
		System.out.print("Starting HeapSort...");
		start = new Date();
		Heap.heapSort(carr);
		end = new Date();
		System.out.println(" fatto. " + (end.getTime() - start.getTime()) + "ms");
	}	
	
	private static boolean isSorted(Comparable[] a) {
		for(int i = 0; i < a.length - 1; i ++)
			if(a[i].compareTo(a[i+1]) > 0)
				return false;
		return true;
	}
}

 


Bacheca di Algoritmi e Strutture Dati a.a. 2007-08 - canale A - L

forum del corso

ultima modifica: 03/04/2008 23.35
by FdA