java insertion sort

java insertion sort

This time I will write the code for insertion sort.

InsetionSort.java


//It feels like sorting cards in your hand.
public class InsertionSort {
	public static void sort(int[] array) {
		for(int i=1;i<array.length;i++) {
			int j=i;
			while(j>=1 && array[j-1]>array[j]) {
				int temp = array[j];
				array[j] = array[j-1];
				array[j-1] = temp;
				j--;
			}
		}
	}

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

Next time I'll try shellsort.

Recommended Posts

java insertion sort
java bubble sort
java selection sort
[java] sort in list
Java Japanese (Kanji) Sort
[Neta] Sleep Sort in Java
Sort strings functionally with java
Java
Java
Java List Group, Sort, etc.
Bubble sort using ArrayList (JAVA)
Java Excel Insertion and Image Extraction
Sort a List of Java objects
Java learning (0)
Studying Java ―― 3
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
Java Day 2018
Java string
java (array)
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 (interface)
Java memorandum
☾ Java / Collection
Sort by multiple conditions using Java Stream
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
Java basics
Decompile Java