commit 8b0269445f77b0f29a2a941f54c93962b92e8d84 Author: Beppe Date: Fri Jan 17 19:49:05 2020 +0100 initial commit. Insertion sort has to become more optimised diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a5fa63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,56 @@ +#GRADLE +.gradle +/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + +# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 +# gradle/wrapper/gradle-wrapper.properties + +#JAVA + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +#MAVEN + +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5217e29 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5be828b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/javaSorts.iml b/javaSorts.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/javaSorts.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/com/beppe/sorts/bubbleSort.java b/src/com/beppe/sorts/bubbleSort.java new file mode 100644 index 0000000..f8ea538 --- /dev/null +++ b/src/com/beppe/sorts/bubbleSort.java @@ -0,0 +1,20 @@ +package com.beppe.sorts; + +public class bubbleSort { + + public static void Sort(int[] arrayToSort, int index, int maxIndex) { + if(maxIndex == 0) return; + else if (index == maxIndex) Sort(arrayToSort, 0, maxIndex-1); + else if (arrayToSort[index] > arrayToSort[index+1]) { + int first = arrayToSort[index]; + int last = arrayToSort[index+1]; + arrayToSort[index+1] = first; + arrayToSort[index] = last; + Sort(arrayToSort, index+1, maxIndex); + } + } + + public static void Sort(int[] arrayToSort) { + Sort(arrayToSort, 0, arrayToSort.length-1); + } +} diff --git a/src/com/beppe/sorts/insertionSort.java b/src/com/beppe/sorts/insertionSort.java new file mode 100644 index 0000000..589fef1 --- /dev/null +++ b/src/com/beppe/sorts/insertionSort.java @@ -0,0 +1,22 @@ +package com.beppe.sorts; + +public class insertionSort { + + public static void Sort(int[] arrayToSort, int accumulator) { + if (accumulator == arrayToSort.length-1) return; + if (arrayToSort[accumulator] <= arrayToSort[accumulator+1]) Sort(arrayToSort, accumulator+1); + else { + int curr = arrayToSort[accumulator]; + int next = arrayToSort[accumulator+1]; + arrayToSort[accumulator+1] = curr; + arrayToSort[accumulator] = next; + if (accumulator != 0) Sort(arrayToSort, accumulator-1); + else Sort(arrayToSort, accumulator); + } + } + + public static void Sort(int[] arrayToSort) { + Sort(arrayToSort, 0); + } + +} diff --git a/src/com/beppe/sorts/sortingAlgorithms.java b/src/com/beppe/sorts/sortingAlgorithms.java new file mode 100644 index 0000000..6e3e379 --- /dev/null +++ b/src/com/beppe/sorts/sortingAlgorithms.java @@ -0,0 +1,21 @@ +package com.beppe.sorts; +import com.beppe.utils.*; + +public class sortingAlgorithms { + public static int arrSize = 100; + public static void main(String[] args) { + int[] intArray = new int[arrSize]; + for (int i = 0; i < arrSize; i++) { + intArray[i] = (int) (Math.random() * 10000); + } + System.out.println("Generated the list to sort"); + System.out.println(Static.timeFunction(() -> insertionSort.Sort(intArray.clone())).toString()); + /*for (int i = 0; i < arrSize; i++) { + intArray[i] = (int) Math.random(); + } + System.out.println("Generated list to sort again");*/ + System.out.println(Static.timeFunction(() -> bubbleSort.Sort(intArray.clone())).toString()); + + } + +} diff --git a/src/com/beppe/utils/Static.java b/src/com/beppe/utils/Static.java new file mode 100644 index 0000000..9afbf79 --- /dev/null +++ b/src/com/beppe/utils/Static.java @@ -0,0 +1,13 @@ +package com.beppe.utils; + +public class Static { + + public static TimedResult timeFunction(Runnable toRun) { + TimedResult timer = new TimedResult(); + timer.start(); + toRun.run(); + timer.end(); + return timer; + + } +} diff --git a/src/com/beppe/utils/TimedResult.java b/src/com/beppe/utils/TimedResult.java new file mode 100644 index 0000000..8c01c39 --- /dev/null +++ b/src/com/beppe/utils/TimedResult.java @@ -0,0 +1,46 @@ +package com.beppe.utils; + +public class TimedResult { + + public long startTime; + public long endTime; + + public long getTime() throws Exception { + if (startTime == 0) throw new Exception("Timer has not started"); + if (endTime == 0) throw new Exception("Timer has not ended"); + + return endTime - startTime; + + } + + public long getMilliseconds() throws Exception { + return getTime() / 1000; + } + + public long getSeconds() throws Exception { + return getMilliseconds() / 1000; + } + + public void start() { + startTime = System.nanoTime(); + } + + public void end() { + endTime = System.nanoTime(); + } + + public void Reset() { + startTime = 0; + endTime = 0; + } + + + @Override + public String toString() { + try { + return String.format("Took %d nanoseconds, %d milliseconds, %d seconds to run", getTime(), getMilliseconds(), getSeconds()); + } catch (Exception e) { + return e.getMessage(); + } + } +} diff --git a/src/com/beppe/utils/Tuple.java b/src/com/beppe/utils/Tuple.java new file mode 100644 index 0000000..fa951e2 --- /dev/null +++ b/src/com/beppe/utils/Tuple.java @@ -0,0 +1,13 @@ +package com.beppe.utils; + +public class Tuple { + public K key; + public V value; + public Tuple(K key, V value) { + this.key = key; + this.value = value; + } + + + +}