Insertion-sort Program in Java

  1. public class InsertionSortExample {
  2. public static void insertionSort(int array[]) {
  3. int n = array.length;
  4. for (int j = 1; j < n; j++) {
  5. int key = array[j];
  6. int i = j-1;
  7. while ( (i > -1) && ( array [i] > key ) ) {
  8. array [i+1] = array [i];
  9. i — ;
  10. }
  11. array[i+1] = key;
  12. }
  13. }
  14. public static void main(String a[]){
  15. int[] arr1 = {9,14,3,2,43,11,58,22};
  16. System.out.println(“Before Insertion Sort”);
  17. for(int i:arr1){
  18. System.out.print(i+” “);
  19. }
  20. System.out.println();
  21. insertionSort(arr1);//sorting array using insertion sort
  22. System.out.println(“After Insertion Sort”);
  23. for(int i:arr1){
  24. System.out.print(i+” “);
  25. }
  26. }
  27. }

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Kubernetes — the 2020 Buzz

THM Sub Voucher writeup

DOT Claim in 2 Steps

Finding Complications of Real Life Examples

Java IO Tutorial — Java Jar Manifest

Exploration beyond local whiteboards — our remote collaboration tool belt

How to insert an image into a Word DOCX document in Python

2021 ManoMano IT Odyssey

Bassin d’Arcachon — France — 2019 — Laurent

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amansingh Javatpoint

Amansingh Javatpoint

More from Medium

Time Conversion | HackerRank Problem | Java Solution

Return nothing, with Java Optionals

Java 14 Features

Basics of Java