0% found this document useful (0 votes)
5 views1 page

Insertion Sort Java

The document presents a Java program that implements the Insertion Sort algorithm. It defines a method to sort an array of integers and demonstrates its functionality with a sample array. The sorted array is then printed to the console.

Uploaded by

123456bca987
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Insertion Sort Java

The document presents a Java program that implements the Insertion Sort algorithm. It defines a method to sort an array of integers and demonstrates its functionality with a sample array. The sorted array is then printed to the console.

Uploaded by

123456bca987
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Sorting Program

public class InsertionSort {


public static void insertionSort(int[] arr) {
int n = [Link];
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

public static void main(String[] args) {


int[] arr = {12, 11, 13, 5, 6};
insertionSort(arr);
for (int num : arr) {
[Link](num + " ");
}
}
}

You might also like