Przeglądaj źródła

Created Project 5 and ran preliminary tests.

Needs comments and more testing.
Tom Flucke 10 lat temu
rodzic
commit
7cca3e983e

+ 2 - 1
.gitignore

@@ -1,3 +1,4 @@
 .metadata
 .recommenders
-bin/
+bin/
+*.txt

+ 6 - 0
Project5/.classpath

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Project5/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Project5</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 11 - 0
Project5/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,11 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7

+ 38 - 0
Project5/src/SortTimes1.java

@@ -0,0 +1,38 @@
+public class SortTimes1 {
+
+	public static void main(String[] args)
+	{
+		Integer[] baseListSelect = new Integer[80000], baseListBubble = new Integer[80000], baseListInsert = new Integer[80000], baseListMerge = new Integer[80000], baseListQuick = new Integer[80000];
+		System.out.println("TEST1: presorted list");
+		for (int N = 5000; N <= 80000; N *= 2)
+		{
+			for (int i = 0; i < N; i++)
+			{
+				baseListSelect[i] = i;
+				baseListBubble[i] = i;
+				baseListInsert[i] = i;
+				baseListMerge[i] = i;
+				baseListQuick[i] = i;
+			}
+			long start = System.currentTimeMillis();
+			Sorts.selectionSort(baseListSelect, N);
+			long selectSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.bubbleSort(baseListBubble, N);
+			long bubbleSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.insertionSort(baseListInsert, N);
+			long insertSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.mergeSort(baseListMerge, N);
+			long mergeSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.quickSort(baseListQuick, N);
+			long quickSortTime = System.currentTimeMillis() - start;
+			System.out.println(String.format("N=%d: T_ss=%d, T_bs=%d, T_is=%d, T_ms=%d, T_qs=%d",
+					N, selectSortTime, bubbleSortTime, insertSortTime, mergeSortTime, quickSortTime));
+		}
+		System.out.println("End TEST1");
+	}
+
+}

+ 38 - 0
Project5/src/SortTimes2.java

@@ -0,0 +1,38 @@
+public class SortTimes2 {
+
+	public static void main(String[] args)
+	{
+		Integer[] baseListSelect = new Integer[80000], baseListBubble = new Integer[80000], baseListInsert = new Integer[80000], baseListMerge = new Integer[80000], baseListQuick = new Integer[80000];
+		System.out.println("TEST2: reverse sorted list");
+		for (int N = 5000; N <= 80000; N *= 2)
+		{
+			for (int i = 0; i < N; i++)
+			{
+				baseListSelect[i] = N - i;
+				baseListBubble[i] = N - i;
+				baseListInsert[i] = N - i;
+				baseListMerge[i] = N - i;
+				baseListQuick[i] = N - i;
+			}
+			long start = System.currentTimeMillis();
+			Sorts.selectionSort(baseListSelect, N);
+			long selectSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.bubbleSort(baseListBubble, N);
+			long bubbleSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.insertionSort(baseListInsert, N);
+			long insertSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.mergeSort(baseListMerge, N);
+			long mergeSortTime = System.currentTimeMillis() - start;
+			start = System.currentTimeMillis();
+			Sorts.quickSort(baseListQuick, N);
+			long quickSortTime = System.currentTimeMillis() - start;
+			System.out.println(String.format("N=%d: T_ss=%d, T_bs=%d, T_is=%d, T_ms=%d, T_qs=%d",
+					N, selectSortTime, bubbleSortTime, insertSortTime, mergeSortTime, quickSortTime));
+		}
+		System.out.println("End TEST2");
+	}
+
+}

+ 43 - 0
Project5/src/SortTimes3.java

@@ -0,0 +1,43 @@
+public class SortTimes3 {
+
+	public static void main(String[] args)
+	{
+		Integer[] baseListSelect = new Integer[80000], baseListBubble = new Integer[80000], baseListInsert = new Integer[80000], baseListMerge = new Integer[80000], baseListQuick = new Integer[80000];
+		System.out.println("TEST3: unsorted list");
+		for (int N = 5000; N <= 80000; N *= 2)
+		{
+			for (int x = 1; x <= 3; x++)
+			{
+				for (int i = 0; i < N; i++)
+				{
+					int num = (int) Math.random()*N;
+					baseListSelect[i] = num;
+					baseListBubble[i] = num;
+					baseListInsert[i] = num;
+					baseListMerge[i] = num;
+					baseListQuick[i] = num;
+				}
+				long start = System.currentTimeMillis();
+				Sorts.selectionSort(baseListSelect, N);
+				long selectSortTime = System.currentTimeMillis() - start;
+				start = System.currentTimeMillis();
+				Sorts.bubbleSort(baseListBubble, N);
+				long bubbleSortTime = System.currentTimeMillis() - start;
+				start = System.currentTimeMillis();
+				Sorts.insertionSort(baseListInsert, N);
+				long insertSortTime = System.currentTimeMillis() - start;
+				start = System.currentTimeMillis();
+				Sorts.mergeSort(baseListMerge, N);
+				long mergeSortTime = System.currentTimeMillis() - start;
+				start = System.currentTimeMillis();
+				Sorts.quickSort(baseListQuick, N);
+				long quickSortTime = System.currentTimeMillis() - start;
+				System.out.println(String.format("N=%d: T_ss=%d, T_bs=%d, T_is=%d, T_ms=%d, T_qs=%d",
+						N, selectSortTime, bubbleSortTime, insertSortTime, mergeSortTime, quickSortTime));
+			}
+			System.out.println();
+		}
+		System.out.println("End TEST3");
+	}
+
+}

+ 184 - 0
Project5/src/Sorts.java

@@ -0,0 +1,184 @@
+
+public class Sorts {
+	 public static <T extends Comparable<? super T>> void selectionSort(T[] arr, int size)
+	 //Sorts the list of size elements contained in arr using the selection sort algorithm.
+	 //Swap one at a time "Shrinking LIst"
+	 {
+		 for (int i = 0; i < size; i++)
+		 {
+			 int minIndex = i;
+			 for(int j = i + 1; j < size; j++)
+			 {
+				 if(arr[minIndex].compareTo(arr[j]) > 0)
+				 {
+					 minIndex = j;
+				 }
+			 }
+			 
+			swap(arr, i, minIndex);
+	 	 }
+	 }
+	 
+	 private static <T extends Comparable<? super T>> void swap(T[] arr, int index1, int index2)
+	 {
+		 T tmp = arr[index1];
+		 arr[index1] = arr[index2];
+		 arr[index2] = tmp;
+	 }
+	 
+	 public static <T extends Comparable<? super T>> void bubbleSort(T[] arr, int size)
+	 //Sorts the list of size elements contained in arr using the bubble sort algorithm.
+	 // Swap as you go
+	 {
+		 boolean done;
+		 do
+		 {
+			 done = true;
+			 for(int i = 0; i < size-1; i++)
+			 {
+				 if(arr[i].compareTo(arr[i + 1]) > 0)
+				 {
+					 swap(arr, i, i + 1);
+					 done = false;
+				 }
+			 }
+		 }
+		 while (!done);
+	 }
+	 public static <T extends Comparable<? super T>> void insertionSort(T[] arr, int size)
+	 //Sorts the list of size elements contained in arr using the insertion sort algorithm.
+	 {
+		 for(int i = 1; i < size; i++)
+		 {
+			 T tmp = arr[i];
+			 int j = i;
+			 while (j > 0 && arr[j-1].compareTo(tmp) > 0)
+			 {
+				 arr[j] = arr[j - 1];
+				 j--;
+			 }
+			 arr[j] = tmp;
+		 }
+	 }
+	 public static <T extends Comparable<? super T>> void mergeSort(T[] arr, int size)
+	 //Sorts the list of size elements contained in arr using the merge sort algorithm.
+	 {
+		 mergeSort(arr, 0, size - 1);
+	 }
+	 
+	 private static <T extends Comparable<? super T>> void mergeSort(T[] arr, int first, int last)
+	 {
+		 int middle = (first + last) / 2;
+		 if(first < last)
+		 {
+			 mergeSort(arr, first, middle);
+			 mergeSort(arr, middle + 1, last);
+			 mergeSortedHalves(arr, first, middle, last);
+		 }
+	 }
+	 
+	 private static <T extends Comparable<? super T>> void mergeSortedHalves(T[] arr, int left, int middle, int right)
+	 {
+		 @SuppressWarnings("unchecked")
+		T[] tmp = (T[]) new Comparable[right - left + 1];
+		 int index1 = left;
+		 int index2 = middle + 1;
+		 int index = 0;
+		 
+		 while(index1 <= middle && index2 <= right)
+		 {
+			 if(arr[index1].compareTo(arr[index2]) < 0)
+			 {
+				 tmp[index] = arr[index1];
+				 index1 ++;
+			 }
+			 else
+			 {
+				 tmp[index] = arr[index2];
+				 index2++;
+			 }
+			 index++;
+		 }
+
+		while(index1 <= middle)
+		{
+			tmp[index] = arr[index1];
+			index1++;
+			index++;
+		}
+		while(index2 <= right)
+		{
+			tmp[index] = arr[index2];
+			index2++;
+			index++;
+		}
+		for (int i = 0; i < tmp.length; i++)
+		{
+			arr[i+left] = tmp[i];
+		}
+	 }
+	 
+	 public static <T extends Comparable<? super T>> void quickSort(T[] arr, int size)
+	 //Sorts the list of size elements contained in arr using the quick sort algorithm with
+	 //median-of-three pivot and rearrangement of the three elements (as done in class).
+	 {
+		 quickSort(arr, 0, size - 1);
+	 }
+	 
+	 private static <T extends Comparable<? super T>> void quickSort(T[] arr, int first, int last)
+	 {
+		 if (first < last)
+		 {
+			 setPivotToEnd(arr, first, last);
+			 int pivotIndex = splitList(arr, first, last);
+			 quickSort(arr, first, pivotIndex - 1);
+			 quickSort(arr, pivotIndex + 1, last);
+		 }
+		 
+	 }
+	 
+	 private static <T extends Comparable<? super T>> void setPivotToEnd(T[] arr, int left, int right)
+	 {
+		 int middle = (right + left) / 2;
+		 if(arr[middle].compareTo(arr[left]) < 0)
+		 {
+			 swap(arr, left, middle);
+		 }
+		 if(arr[right].compareTo(arr[left]) < 0)
+		 {
+			 swap(arr, left, right);
+		 }
+		 if(arr[middle].compareTo(arr[right]) < 0)
+		 {
+			 swap(arr, right, middle);
+		 }
+	 }
+	 
+	 private static <T extends Comparable<? super T>> int splitList(T[] arr, int left, int right)
+	 {
+		 int indexLeft = left;
+		 int indexRight = right - 1;
+		 
+		 T pivot = arr[right];
+		 
+		 while(indexRight >= indexLeft)
+		 {
+			 while(arr[indexLeft].compareTo(pivot) < 0)
+			 {
+				 indexLeft++;
+			 }
+			 while(indexRight >= indexLeft && arr[indexRight].compareTo(pivot) > 0)
+			 {
+				 indexRight--;
+			 }
+			 if(indexRight >= indexLeft)
+			 {
+				 swap(arr, indexLeft, indexRight);
+				 indexLeft++;
+				 indexRight--;
+			 }
+		 }
+		 swap(arr, indexLeft, right);
+		 return indexLeft;
+	 }
+}