-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from frigid-lynx/patch-2
Create InsertionSort.java
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//insertion sort implementation in java | ||
public class InsertionSortExample { | ||
public static void insertionSort(int array[]) { | ||
int n = array.length; | ||
for (int j = 1; j < n; j++) { | ||
int key = array[j]; | ||
int i = j-1; | ||
while ( (i > -1) && ( array [i] > key ) ) { | ||
array [i+1] = array [i]; | ||
i--; | ||
} | ||
array[i+1] = key; | ||
} | ||
} | ||
|
||
public static void main(String a[]){ | ||
int[] arr1 = {9,14,3,2,43,11,58,22}; | ||
System.out.println("Before Insertion Sort"); | ||
for(int i:arr1){ | ||
System.out.print(i+" "); | ||
} | ||
System.out.println(); | ||
|
||
insertionSort(arr1);//sorting array using insertion sort | ||
|
||
System.out.println("After Insertion Sort"); | ||
for(int i:arr1){ | ||
System.out.print(i+" "); | ||
} | ||
} | ||
} |