Advertisement
16112

INSERTION SORT

Apr 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. package testing;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class TritochkaTri {
  6.     public static void insertionSort(int arr[]) {
  7.         int n = arr.length;
  8.         for (int j = 1; j < n; j++) {
  9.             int key = arr[j];
  10.             int i = j - 1;
  11.             while ((i > -1) && (arr[i] > key)) {
  12.                 arr[i + 1] = arr[i];
  13.                 i--;
  14.             }
  15.             arr[i + 1] = key;
  16.         }
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         int[] arr1 = {1, -4, 32, -5};
  21.         System.out.println("Before Insertion Sort:");
  22.         for (int i : arr1) {
  23.             System.out.print(i + " ");
  24.         }
  25.         System.out.println();
  26.  
  27.         insertionSort(arr1);
  28.  
  29.         System.out.println("After Insertion Sort:");
  30.         for (int i : arr1) {
  31.             System.out.print(i + " ");
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement