public int[] insertionSort(int[] arr)
      for (j = 1; j < arr.length; j++) {
         int key = arr[j]
         int i = j - 1
         while (i > 0 and arr[i] > key) {
            arr[i+1] = arr[i]
            i -= 1
         }
         arr[i+1] = key
      }
      return arr;# Insertion sort in Python


def insertionSort(array):

    for step in range(1, len(array)):
        key = array[step]
        j = step - 1
        
        # Compare key with each element on the left of it until an element smaller than it is found
        # For descending order, change key<array[j] to key>array[j].        
        while j >= 0 and key < array[j]:
            array[j + 1] = array[j]
            j = j - 1
        
        # Place key at after the element just smaller than it.
        array[j + 1] = key


data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class InsertionSorting {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String[] argh){
    int[] arrNotSorted = newArrInitilizer();
    enterValues(arrNotSorted);
    sortArray(arrNotSorted);
    print(arrNotSorted);

    }
  	//Print Array
    public static void print(int[] arr){
            System.out.print(Arrays.toString(arr));
    }
  
	/* looping from "i"(the incremented index in) ==> function
    public static int[] sortArray(int [] unsortedArr)
   	first we initilize an integer "value"= Array[from])
    this will be assigned later to the Array in the minmum value index 
    
    and while (from > 0) && (Array[from-1] > value) 
    we assign every next value to the previous one 
    
     eventually we decrement ("from")
   */
    public static void insertionSorting(int [] toBesorted, int from){
        int value = toBesorted[from];
        while(from > 0 && toBesorted[from-1] > value){
            toBesorted[from] = toBesorted[from-1];
         --from;
        }

        toBesorted[from] = value;

    }
 	
	/* Looping from index = 1, array with size one concidered sorted) 
    later "From" will be assigned to i in the function above */
    public static int[] sortArray(int [] unsortedArr){
        for(int i = 1 ; i < unsortedArr.length ; ++i){
            insertionSorting(unsortedArr,i);
        }

        return unsortedArr;
    }

  
  
    public static int[] newArrInitilizer() {
        System.out.println("Enter Array Size .");
        int arrSize = scanner.nextInt();
        int[] arr = new int[arrSize];
        return arr;
    }
		
  
  
    public static int [] enterValues(int[] arr){
        System.out.println("Array being initlized randomly with "+arr.length+" values.");
        for(int i = 0 ; i< arr.length ; ++i){
            arr[i] = (int) (Math.random()*10);
        }
        return  arr;
    }
}
5 4 7 9INSERTION-SORT(A)
   for i = 1 to n
   	key ← A [i]
    	j ← i – 1
  	 while j > = 0 and A[j] > key
   		A[j+1] ← A[j]
   		j ← j – 1
   	End while 
   	A[j+1] ← key
  End for

Java相关代码片段

types of typecasting in java

types of assignment statement in java

Thread mutex

env files in spring boot

java over loading

logging in spring boot

how to split a list in multiple lists java

can two servlet have same urlpattern

spring debug

jsp spring

hanoi tower recursion java

java equals

age difference java

sed cheat sheet

java compare method

how to make a activity default in manifest

java max memory size

yum uninstall java

timestamp to long java

how to set background image in android studio

simple calculator in android

When to use HashMap vs Map

spring boot jpa

spring data jpa

spring jdbc

jpa vs hibernate

java with checking the password matching

profile in spring boot

string templates java

spring rest controller

java arraylist get item

com.oracle.jdbc

check if map is not empty java

how to install java 8 on debian 12

regex caractères spéciaux java

java 11 yum install

manage session in redis spring boot

java: error: release version 19 not supported

bean scope in spring

xml configuration in spring

spring cdi annotations

postconstruct and predestroy in spring

java decode_message

spring lazy initialization

java decorator design pattern

dependency injection in spring

check back pressed in fragment andorid

send email to any domain using java

record java

vs code setup for input/output in java

substring java

receive second word in string java

loose coupling in java

default value of char in java

spring boot repository pattern

spring boot h2 database

add security to spring admin

java islessthan method

Java implementation of recursive Binary Search

java round double

java downcasting

range of fibonacci series in java using recursion

java by anyone

solid java

equals() and hashcode() methods in java

android java map to bundle

android start activity with data

medium java 17

java import util list

bean life cycle in spring

spring boot aop

spring aspect

spring logging

simple java jsp project example

spring boot file watcher implementation example

kotlin loop list

autowired spring

java generics example

spring bean scope

appconfig spring