-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha35insertionSort.java
More file actions
30 lines (28 loc) · 1.21 KB
/
a35insertionSort.java
File metadata and controls
30 lines (28 loc) · 1.21 KB
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
30
public class a35insertionSort {
// Method to perform insertion sort on an array
static void insertionSort(int arr[]){
// Iterate through each element in the array starting from the second element
for(int i=1; i<arr.length; i++){
int key = arr[i]; // The current element to be positioned
int j = i-1; // Index of the last sorted element
// Move elements that are greater than 'key' to one position ahead
while(j>=0 && key<arr[j]){
arr[j+1] = arr[j]; // Shift element to the right
j--; // Move to the previous element
}
arr[j+1] = key; // Place 'key' in its correct position
}
}
// Method to print the elements of the array
static void printArray(int arr[]){
for(int i=0; i<arr.length; i++){
System.out.print(arr[i] + " "); // Print each element followed by a space
}
}
// Main method to execute the sorting and printing
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 9, 7, 11}; // Initial array
insertionSort(arr); // Sort the array
printArray(arr); // Print the sorted array
}
}