INSERTION-SORT(A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1.. j - 1]. 4 i = j - 1 5 while i > 0 and A[i] > key 6 A[i + 1] = A[i] 7 i = i - 1 8 A[i + 1] = key
Added by Anthony M.
Close
Step 1
Here's the code for the insertionSort method: ```java public static void insertionSort(int[] arr) { int n = arr.length; for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { Show more…
Show all steps
Your feedback will help us improve your experience
Shashank Pandey and 74 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Please do in Java!!!!!!! Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of list values for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the data into an array. Then, based on the array's size, find the middle item.
Akash M.
Design a sorted ArrayList in java.
Muhammad J.
IN JAVA. 1) Write a program named KeyboardToFile that reads all the data in standard input and makes an exact, perfect copy of it in a file named input.copy. 2) Write a program named KeyboardTo2Files that reads all the integers in standard input and writes the even ones to a file named evens.txt and the odd ones to a file named odds.txt. Each integer written out should be on a line by itself, with nothing else on the line. 3) The file numbers.text consists of integers. Write a program named ParityCounter that reads the data and then prints (to standard output) the number of even integers and number of odd integers read (excluding the header of course). For example, if the file numbers.text contains: 3 8 27 5 44 67, the program should produce the following output: 3 even integers 1 odd integer.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD