Selection sort implementation

This commit is contained in:
Reborn 2025-03-19 12:07:54 +00:00
parent c250bdc218
commit 4ec587f5b7
9 changed files with 86 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>SelectionSort</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,15 @@
package SelectionSort;
import java.util.Arrays;
//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 class Main
{
public static void main(String[] args) {
int[] toSort = new int[] {1, 5, 9, 3, 54, 2, 34, 2134, 2, 443, 23, 12, 4, 663, 123, 42};
SelectionSort sorter = new SelectionSort();
int[] sorted = sorter.sort(toSort);
System.out.println(Arrays.toString(sorted));
}
}

View File

@ -0,0 +1,21 @@
package SelectionSort;
public class SelectionSort {
public int[] sort(int[] arr) {
for (int i=0; i<arr.length; i++) {
for (int j = i+1; j<arr.length;j++) {
boolean isCurrentSmallerThanSpot = arr[j] < arr[i];
if (isCurrentSmallerThanSpot) {
arr = swapPlaces(arr, i, j);
}
}
}
return arr;
}
private int[] swapPlaces(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
}