java selection sort

Selection sort

This time I will write the code for selection sort.

SelectionSort.java


public class SelectionSort {
	public static void main(String args[]) {
		int[] array = {2,3,5,4,1};
		sort(array);
		for(int i=0;i<array.length;i++) {
			System.out.print(array[i]);
		}
	}

	public static void sort(int[] array) {
		int n = array.length;
		for (int i =0;i<n-1;i++) {
			int lowest = i;
			int lowkey = array[i];
			for(int j=i+1;j<n;j++) {
				if(array[j]<lowkey) {
					lowest = j;
					lowkey=array[j];
				}
			}
			int temp = array[i];
			array[i]=array[lowest];
			array[lowest] = temp;
		}
	}
}


Next time I'll try insertion sort.

Recommended Posts

java selection sort
java bubble sort
java insertion sort
[java] sort in list
Java Japanese (Kanji) Sort
[Neta] Sleep Sort in Java
Sort strings functionally with java
Multi-stage selection (Java / Groovy / Scala)
Java
Java
Java List Group, Sort, etc.
Bubble sort using ArrayList (JAVA)
Sort a List of Java objects
Java learning (0)
[Java] array
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Java Day 2018
Java string
java (array)
Make bubble sort and selection sort in Ruby
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
[Java] Write a sort faster than Arrays.sort
java reflexes
java (interface)
☾ Java / Collection
Sort by multiple conditions using Java Stream
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics