-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
29 lines (26 loc) · 825 Bytes
/
Copy pathInsertionSort.java
File metadata and controls
29 lines (26 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// time complexity: O(n^2)
// space complexity: O(1)
import java.util.*;
import java.io.*;
public class InsertionSort {
// Implementation of insertion sort
public static void insertionSort(int[] arr){
for(int i=1; i<arr.length; i++){
int j = i;
while(j > 0 && arr[j] < arr[j-1]){
// swap the elements between arr[j] and arr[j-1]
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
}
public static void main(String[] args){
int[] arr = {20, 50, 25, 67, 79, 12, 15};
//function calling
insertionSort(arr);
System.out.println("Sorted array is: ");
System.out.println(Arrays.toString(arr));
}
}