Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

insertion sort java

//  Insertion Sort

class InsertionSort {
    /*Function to sort array using insertion sort*/
    void insertionsort(int arr[])
    {
        int n = arr.length;
        for (int i = 1; i < n; ++i) {
            int key = arr[i];
            int r = i - 1;
  
            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while (r >= 0 && arr[r] > key) {
                arr[r + 1] = arr[r];
                r = r - 1;
            }
            arr[r + 1] = key;
        }
    }
    /* A utility function to print array of size n*/
    static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
  
        System.out.println();
    }
  
  
    // Driver method
    public static void main(String args[])
    {
        int arr[] = { 12, 11, 13, 5, 6 , 7, 90, 60, 50, 30, 54, 2, 1, 6 };
        //creates new instance of insertin sort and runs it with a given arry
        InsertionSort ob = new InsertionSort();
        ob.insertionsort(arr);
        //prints the sorted array
        printArray(arr);
    }
} 
Comment

insertion sort java

// Java program for implementation of Insertion Sort
class InsertionSort {
    /*Function to sort array using insertion sort*/
    void sort(int arr[])
    {
        int n = arr.length;
        for (int i = 1; i < n; ++i) {
            int key = arr[i];
            int j = i - 1;
  
            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
    }
  
    /* A utility function to print array of size n*/
    static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
  
        System.out.println();
    }
  
    // Driver method
    public static void main(String args[])
    {
        int arr[] = { 12, 11, 13, 5, 6 };
  
        InsertionSort ob = new InsertionSort();
        ob.sort(arr);
  
        printArray(arr);
    }
} /* This code is contributed by Rajat Mishra. */
Comment

Insertion sort in Java

// Insertion sort in Java

import java.util.Arrays;

class InsertionSort {

  void insertionSort(int array[]) {
    int size = array.length;

    for (int step = 1; step < size; step++) {
      int key = array[step];
      int 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 && key < array[j]) {
        array[j + 1] = array[j];
        --j;
      }

      // Place key at after the element just smaller than it.
      array[j + 1] = key;
    }
  }

  // Driver code
  public static void main(String args[]) {
    int[] data = { 9, 5, 1, 4, 3 };
    InsertionSort is = new InsertionSort();
    is.insertionSort(data);
    System.out.println("Sorted Array in Ascending Order: ");
    System.out.println(Arrays.toString(data));
  }
}
Comment

Insertion Sort Java

// Java Insertion Sort
// -------------------

/* 
   Time Complexity
     Best Time Complexity:O(n)
	 Average Time Complexity:O(n^2)
	 Worst Time Complexity:O(n^2)
     
   Space Complexity
     No auxiliary space is required in Linear Search implementation.
	 Hence space complexity is:O(1)
*/

import java.util.Arrays;
import java.util.Scanner;

public class InsertionSort {
    public static int[] sort(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            int j = i - 1; // j will point to previous element
            int k = arr[i]; // k holds the value being sorted

            while (j >= 0 && k <= arr[j]) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }

            arr[j + 1] = k;

        }
        return arr; //returns the sorted array

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int arr[] = new int[10];

        System.out.println("Enter Array Values: ");
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }

        int a[] = new int[arr.length];

        a = Arrays.copyOf(sort(arr), arr.length); //copies the sorted array to another array

      	//sorted in ascending order
        System.out.println("Sorted Array:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(+a[i] + " ");
        }
    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Access Array Elements 
Java :: encapsulation java 
Java :: java boolean data type 
Java :: get value from Spring application.properties 
Java :: Java How to use Deque? 
Java :: java streams 
Java :: spring boot send api request 
Java :: Java Expression statements 
Java :: Adding Entire ArrayList to another ArrayList in Java 
Java :: java float data type 
Java :: compare 2 hashmap 
Java :: android studio convert java to kotlin 
Java :: array to array list java 
Java :: run specific test case junit 
Java :: java first index of an arraylist 
Java :: enum class in java 
Java :: extends class in java 
Java :: check if object is empty java 8 
Java :: java check if array element is null 
Java :: java to python 
Java :: BufferedReader x = new BufferedReader(new InputStreamReader(System.in)); 
Java :: edit text on 2sec change andropid 
Java :: show bottom sheet in adapter 
Java :: Android Studio First time run check 
Java :: how to stop extending jpa repository to every class in java 
Java :: internal hashcode 
Java :: Picked up _JAVA_OPTIONS: -Xmx256M intillij 
Java :: springboot request list 
Java :: data validation dialog box android 
Java :: java operations on classes 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =