0% found this document useful (0 votes)
8 views

Insertion Sort

The document describes an insertion sort algorithm implemented in Java. It defines an array of unsorted integers, passes the array to a doInsertionSort method, and iterates through the array comparing and swapping adjacent elements into sorted order. The sorted array is then printed to output.

Uploaded by

Lam Sin Wing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Insertion Sort

The document describes an insertion sort algorithm implemented in Java. It defines an array of unsorted integers, passes the array to a doInsertionSort method, and iterates through the array comparing and swapping adjacent elements into sorted order. The sorted array is then printed to output.

Uploaded by

Lam Sin Wing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

package com.java2novice.

algos;
public class MyInsertionSort {
public static void main(String a[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doInsertionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
public static int[] doInsertionSort(int[] input){
int temp;
for (int i = 1; i < input.length; i++) {
for(int j = i ; j > 0 ; j--){
if(input[j] < input[j-1]){
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}

- See more at: http://www.java2novice.com/java-sortingalgorithms/insertion-sort/#sthash.SWqr8gEP.dpuf

You might also like