Insertion Sort in Java (original) (raw)
Published on 15 October 2019 (Updated: 10 October 2022)
Welcome to the Insertion Sort in Java page! Here, you'll find the source code for this program as well as a description of how the program works.
Current Solution
import java.util.ArrayList;
public class InsertionSort {
public static void main(String[] args) {
ArrayList<Integer> numList = new ArrayList<>();
if (args.length < 1) {
System.out.println(
"Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"");
}
else if (args[0].length() < 2) {
System.out.println(
"Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"");
} else {
String[] stringList = args[0].split(",");
if (stringList.length < 2) {
System.out.println(
"Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"");
} else {
for (int i = 0; i < stringList.length; i++) {
numList.add(Integer.parseInt(stringList[i].trim()));
}
if (numList.size() < 2) {
System.out.println(
"Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"");
} else {
insertionSort(numList);
}
}
}
}
public static void insertionSort(ArrayList<Integer> numList) {
for (int j = 1; j < numList.size(); j++) {
int k = j - 1, val = numList.get(j);
while (k >= 0 && numList.get(k) > val) {
numList.set(k + 1, numList.get(k));
k--;
}
numList.set(k + 1, val);
}
for (int i = 0; i < numList.size() - 1; i++) {
System.out.print(numList.get(i) + ", ");
}
System.out.print(numList.get(numList.size() - 1));
}
}
Insertion Sort in Java was written by:
- Ganesh Naik
- Jeremy Grifski
- Shubham Raj
If you see anything you'd like to change or update, please consider contributing.
How to Implement the Solution
No 'How to Implement the Solution' section available. Please consider contributing.
How to Run the Solution
No 'How to Run the Solution' section available. Please consider contributing.