Astonishing Insertion sort in an array
For other sorting programs clickhttps://wordpress-343193-1101484.cloudwaysapps.com/array-programs-part-1-icse-computer-applications
Insertion sort is a sorting technique used to sort array in ascending and descending order.
For other sorting programs click
Array in JAVA
Write a program to accept an array from the user and sort the array in ascending order using the insertion sort method.
import java.io.*;
class insertion_sort {
public static void main()throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the length of the Array.");
int n=Integer.parseInt(br.readLine());
int Arr[]=new int[n];
int sum=0;
System.out.println("Enter the values");
for(int i=0;i<n;i++)
Arr[i]=Integer.parseInt(br.readLine());
System.out.println("\f");
for(int i=0;i<n;i++)
System.out.println(Arr[i]);
int flag, j;
for(int i=1;i<n;i++) {
flag=Arr[i];
j=i-1;
while(j>=0 && Arr[j]>flag)
{
Arr[j+1]=Arr[j];
j=j-1;
}
Arr[j+1]=flag;
} System.out.println("Sorted Array is ");
for(int i=0;i<n;i++)
System.out.println(Arr[i]);
} }Output:
Enter the length of the Array.
4
Enter the values
23
17
2
4
Sorted Array is
2
4
17
23
For more java videos click
https://www.youtube.com/user/trushnatej
Related Posts
Continue learning with these related articles

ISC Practical 2027: College Enrolment Code Validator and Stream Report – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 question: validate college enrolment codes made of a stream, year and serial number, give the exact reason for each rejection, auto-correct lowercase streams and prepare a stream-wise report.

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

Boolean Algebra for ISC Class 12: Laws, Canonical SOP and POS, and Simplification
ISC Class 12 Computer Science notes on Boolean algebra: all the laws and theorems, duality, De Morgan's laws, minterms and maxterms, converting to canonical SOP and POS, and step-by-step algebraic simplification with solved examples.