insertion sort wrong

This commit is contained in:
Reborn 2025-03-20 16:06:07 +00:00
parent 4ec587f5b7
commit 6c24908481
9 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>InsertionSort</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>

View File

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
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.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17

View File

@ -0,0 +1,19 @@
package InsertionSort;
public class InsertionSort {
public int[] sort(int[] arr) {
for (int limiter=1; limiter < arr.length; limiter++) {
int i = 1;
int j = limiter;
int border = arr[limiter];
while (limiter>=i && border < arr[limiter-i]) {
arr[j] = arr[j-1];
arr[j-1] = border;
i++;
j--;
}
}
return arr;
}
}

View File

@ -0,0 +1,16 @@
package InsertionSort;
import java.util.Arrays;
public class Main {
//Selection sort is the algorithm to sort one spot at a time
//Comb the entire array, select the smallest and place it at the beginning
public static void main(String[] args) {
int[] toSort = new int[] {4, 5, 9, 3, 54, 2, 34, 2134, 2, 443, 23, 12, 4, 663, 123, 42};
InsertionSort sorter = new InsertionSort();
int[] sorted = sorter.sort(toSort);
System.out.println(Arrays.toString(sorted));
}
}