mirror of
https://github.com/ossu/computer-science.git
synced 2025-04-18 06:35:58 -04:00
Selection sort implementation
This commit is contained in:
parent
c250bdc218
commit
4ec587f5b7
@ -0,0 +1 @@
|
||||
/.metadata/
|
@ -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>
|
@ -0,0 +1 @@
|
||||
/bin/
|
@ -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>
|
@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
@ -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
|
@ -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));
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
module SelectionSort {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user