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

Java Insertion Sort

// 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 :: reverseString 
Java :: how to display the decimal value when 0 after point android studio 
Java :: change size of array java 
Java :: for java 
Java :: how to get 2 decimal places in java 
Java :: display two dimension array java 
Java :: android dialog box example 
Java :: nosuchelementexception 
Java :: how to change color of drawable in android 
Java :: set view size android 
Java :: java print default options 
Java :: runnable java 
Java :: how to make arraylist character 
Java :: how to get string from resource id android 
Java :: add custom font to android paint object 
Java :: do...while loop Java 
Java :: loop through number java 
Java :: Powermockito static method call 
Java :: sort a list with custom comparator java 
Java :: getsource java 
Java :: java how to create an array with existing objects 
Java :: check if a char is a space java 
Java :: -3 to string in java 
Java :: set jcombobox index java 
Java :: window in java swing 
Java :: java abstraction 
Java :: unchecked exception in java 
Java :: set text in edittext android 
Java :: android get sdk version 
Java :: Remove an element at a specific index from an array in Java 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =