SlideShare a Scribd company logo
write a program that given a list of 20 integers, sorts them according to insert sort. java
Solution
public class InsertionSort {
/**
* method to sort the array elements using insertion sorts *
*
* @param array
* @return
*/
public static int[] 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;
}
return array;
}
/**
* method to print the array elements
*
* @param input
*/
private static void printNumbers(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println(" ");
}
/**
* @param args
*/
public static void main(String[] args) {
int[] input = { 3, 26, 67, 35, 9, -6, 43, 82, 10, 54, 14, 22, 21, 32,
121, 231, 12, 98, 77, 65 };
System.out.println("Before Sorting");
printNumbers(input);
input = insertionSort(input);
System.out.println("Before Sorting");
printNumbers(input);
}
}
OUTPUT:
Before Sorting
3, 26, 67, 35, 9, -6, 43, 82, 10, 54, 14, 22, 21, 32, 121, 231, 12, 98, 77, 65,
Before Sorting
-6, 3, 9, 10, 12, 14, 21, 22, 26, 32, 35, 43, 54, 65, 67, 77, 82, 98, 121, 231,

Recommended

Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
aloeplusint
 
import java-lang-Comparable- -- new Main-String-() public class Main-.pdf
import java-lang-Comparable- -- new Main-String-() public class Main-.pdf
GordonF2XPatersonh
 
Insertion sort
Insertion sort
Dorina Isaj
 
D&AA Lecture 2 insertion sort.ppt
D&AA Lecture 2 insertion sort.ppt
HaniaKhan75
 
Insertion Sort Code
Insertion Sort Code
Nanthachai Poolketgij
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
rajeshjangid1865
 
Write a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdf
archanaemporium
 
Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
ajoy21
 
Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
sorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
Lecture12,13,14.pdf
Lecture12,13,14.pdf
zainab278016
 
insertion sort.ppt
insertion sort.ppt
JawadHaider36
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
Write a Java application that performs the following taskCrea.docx
Write a Java application that performs the following taskCrea.docx
edgar6wallace88877
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
dorisc7
 
Insertion sort
Insertion sort
Rajendran
 
The program below belongs to the topic ARRAYLIST. Modify the program.pdf
The program below belongs to the topic ARRAYLIST. Modify the program.pdf
alankareyecare
 
InsertionSorting.javaimport java.util.Arrays; import java.util.S.pdf
InsertionSorting.javaimport java.util.Arrays; import java.util.S.pdf
info54093
 
Ds program-print
Ds program-print
Chaitanya Kn
 
Sorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan Satish
Dipayan Sarkar
 
Sorting programs
Sorting programs
Varun Garg
 
Objectives In this lab you will review passing arrays to methods and.pdf
Objectives In this lab you will review passing arrays to methods and.pdf
f3apparelsonline
 
Elementary Sort
Elementary Sort
Sri Prasanna
 
Insert element position
Insert element position
Kavya Shree
 
Lecture 2
Lecture 2
sajinsc
 
Computer java programs
Computer java programs
ADITYA BHARTI
 
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
Homework 3 P11Is this relation a function    (Write either Yes.pdf
Homework 3 P11Is this relation a function    (Write either Yes.pdf
armcomputers
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
armcomputers
 

More Related Content

Similar to write a program that given a list of 20 integers, sorts them accordi.pdf (20)

Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
sorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
Lecture12,13,14.pdf
Lecture12,13,14.pdf
zainab278016
 
insertion sort.ppt
insertion sort.ppt
JawadHaider36
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
Write a Java application that performs the following taskCrea.docx
Write a Java application that performs the following taskCrea.docx
edgar6wallace88877
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
dorisc7
 
Insertion sort
Insertion sort
Rajendran
 
The program below belongs to the topic ARRAYLIST. Modify the program.pdf
The program below belongs to the topic ARRAYLIST. Modify the program.pdf
alankareyecare
 
InsertionSorting.javaimport java.util.Arrays; import java.util.S.pdf
InsertionSorting.javaimport java.util.Arrays; import java.util.S.pdf
info54093
 
Ds program-print
Ds program-print
Chaitanya Kn
 
Sorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan Satish
Dipayan Sarkar
 
Sorting programs
Sorting programs
Varun Garg
 
Objectives In this lab you will review passing arrays to methods and.pdf
Objectives In this lab you will review passing arrays to methods and.pdf
f3apparelsonline
 
Elementary Sort
Elementary Sort
Sri Prasanna
 
Insert element position
Insert element position
Kavya Shree
 
Lecture 2
Lecture 2
sajinsc
 
Computer java programs
Computer java programs
ADITYA BHARTI
 
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
Lecture12,13,14.pdf
Lecture12,13,14.pdf
zainab278016
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
Write a Java application that performs the following taskCrea.docx
Write a Java application that performs the following taskCrea.docx
edgar6wallace88877
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
dorisc7
 
Insertion sort
Insertion sort
Rajendran
 
The program below belongs to the topic ARRAYLIST. Modify the program.pdf
The program below belongs to the topic ARRAYLIST. Modify the program.pdf
alankareyecare
 
InsertionSorting.javaimport java.util.Arrays; import java.util.S.pdf
InsertionSorting.javaimport java.util.Arrays; import java.util.S.pdf
info54093
 
Sorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan Satish
Dipayan Sarkar
 
Sorting programs
Sorting programs
Varun Garg
 
Objectives In this lab you will review passing arrays to methods and.pdf
Objectives In this lab you will review passing arrays to methods and.pdf
f3apparelsonline
 
Insert element position
Insert element position
Kavya Shree
 
Lecture 2
Lecture 2
sajinsc
 
Computer java programs
Computer java programs
ADITYA BHARTI
 
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 

More from armcomputers (20)

Homework 3 P11Is this relation a function    (Write either Yes.pdf
Homework 3 P11Is this relation a function    (Write either Yes.pdf
armcomputers
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
armcomputers
 
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdf
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdf
armcomputers
 
Female copperhead snakes have the ability to reproduce both sexually.pdf
Female copperhead snakes have the ability to reproduce both sexually.pdf
armcomputers
 
Explain some of the commonly held misconceptions about cloning. Why .pdf
Explain some of the commonly held misconceptions about cloning. Why .pdf
armcomputers
 
Describe nN when the number of positive divisors of n is i) p2 for .pdf
Describe nN when the number of positive divisors of n is i) p2 for .pdf
armcomputers
 
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdf
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdf
armcomputers
 
Consider the table below How many featuresattributes are in this s.pdf
Consider the table below How many featuresattributes are in this s.pdf
armcomputers
 
Chickens vary in color. Black and white color variation can be traced.pdf
Chickens vary in color. Black and white color variation can be traced.pdf
armcomputers
 
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
armcomputers
 
Why is the 16S rRNA gene a preferred marker for the identification, .pdf
Why is the 16S rRNA gene a preferred marker for the identification, .pdf
armcomputers
 
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdf
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdf
armcomputers
 
which of the following types of orgnisms possess the enzyme catalase.pdf
which of the following types of orgnisms possess the enzyme catalase.pdf
armcomputers
 
Which of the five factors of production is most important for creati.pdf
Which of the five factors of production is most important for creati.pdf
armcomputers
 
What is the good producing business What is the good producing.pdf
What is the good producing business What is the good producing.pdf
armcomputers
 
What is the future of PKI Acceptance of PKI solutions—and product s.pdf
What is the future of PKI Acceptance of PKI solutions—and product s.pdf
armcomputers
 
An easy temperament is best characterized asoptionshigh so.pdf
An easy temperament is best characterized asoptionshigh so.pdf
armcomputers
 
A protein that was isolated by a team of university scien4sts is sus.pdf
A protein that was isolated by a team of university scien4sts is sus.pdf
armcomputers
 
A laser beam strikes one end of a slab of material of length L = 42..pdf
A laser beam strikes one end of a slab of material of length L = 42..pdf
armcomputers
 
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
armcomputers
 
Homework 3 P11Is this relation a function    (Write either Yes.pdf
Homework 3 P11Is this relation a function    (Write either Yes.pdf
armcomputers
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
armcomputers
 
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdf
Find Tukey-Kramer resultsa. Group 1 to Group 2 Different Not D.pdf
armcomputers
 
Female copperhead snakes have the ability to reproduce both sexually.pdf
Female copperhead snakes have the ability to reproduce both sexually.pdf
armcomputers
 
Explain some of the commonly held misconceptions about cloning. Why .pdf
Explain some of the commonly held misconceptions about cloning. Why .pdf
armcomputers
 
Describe nN when the number of positive divisors of n is i) p2 for .pdf
Describe nN when the number of positive divisors of n is i) p2 for .pdf
armcomputers
 
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdf
Describe one derived trait in humans (Homo sapiens).SolutionThe.pdf
armcomputers
 
Consider the table below How many featuresattributes are in this s.pdf
Consider the table below How many featuresattributes are in this s.pdf
armcomputers
 
Chickens vary in color. Black and white color variation can be traced.pdf
Chickens vary in color. Black and white color variation can be traced.pdf
armcomputers
 
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
{c}Problem Run For Charity (charityrun.c)Charity Runs are a pop.pdf
armcomputers
 
Why is the 16S rRNA gene a preferred marker for the identification, .pdf
Why is the 16S rRNA gene a preferred marker for the identification, .pdf
armcomputers
 
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdf
Why do SSDs benefit from a scheduler like noopSolutionThe def.pdf
armcomputers
 
which of the following types of orgnisms possess the enzyme catalase.pdf
which of the following types of orgnisms possess the enzyme catalase.pdf
armcomputers
 
Which of the five factors of production is most important for creati.pdf
Which of the five factors of production is most important for creati.pdf
armcomputers
 
What is the good producing business What is the good producing.pdf
What is the good producing business What is the good producing.pdf
armcomputers
 
What is the future of PKI Acceptance of PKI solutions—and product s.pdf
What is the future of PKI Acceptance of PKI solutions—and product s.pdf
armcomputers
 
An easy temperament is best characterized asoptionshigh so.pdf
An easy temperament is best characterized asoptionshigh so.pdf
armcomputers
 
A protein that was isolated by a team of university scien4sts is sus.pdf
A protein that was isolated by a team of university scien4sts is sus.pdf
armcomputers
 
A laser beam strikes one end of a slab of material of length L = 42..pdf
A laser beam strikes one end of a slab of material of length L = 42..pdf
armcomputers
 
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
4. Communicate Carla gathered 328 sea-shells. Daniel gathered 176 .pdf
armcomputers
 

Recently uploaded (20)

Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 

write a program that given a list of 20 integers, sorts them accordi.pdf

  • 1. write a program that given a list of 20 integers, sorts them according to insert sort. java Solution public class InsertionSort { /** * method to sort the array elements using insertion sorts * * * @param array * @return */ public static int[] 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; } return array; } /** * method to print the array elements * * @param input */ private static void printNumbers(int[] input) { for (int i = 0; i < input.length; i++) { System.out.print(input[i] + ", "); } System.out.println(" "); }
  • 2. /** * @param args */ public static void main(String[] args) { int[] input = { 3, 26, 67, 35, 9, -6, 43, 82, 10, 54, 14, 22, 21, 32, 121, 231, 12, 98, 77, 65 }; System.out.println("Before Sorting"); printNumbers(input); input = insertionSort(input); System.out.println("Before Sorting"); printNumbers(input); } } OUTPUT: Before Sorting 3, 26, 67, 35, 9, -6, 43, 82, 10, 54, 14, 22, 21, 32, 121, 231, 12, 98, 77, 65, Before Sorting -6, 3, 9, 10, 12, 14, 21, 22, 26, 32, 35, 43, 54, 65, 67, 77, 82, 98, 121, 231,