int [] list = {18,57,8,89,7} and the length = 5
Sort the array using insertion sort
public class InsertionSort {
/** The method for sorting the numbers */
public static void insertionSort(int[] list) {
for (int i = 1; i < list.length; i++) {
/** insert list[i] into a sorted sublist list[0..i-1] so that list[0..i] is sorted. */
int currentElement = list[i];
int k; for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
list[k + 1] = list[k];
}
// Insert the current element into list[k+1]
list[k + 1] = currentElement;
}
}
/** A test method */
public static void main(String[] args) {
int[] list = {18,57,8,89,7};
insertionSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " "); } }
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images
- // pre: list != null, list.length > 0 // post: return index of minimum element of array public static int findMin(int[] list) { assert list != null && list.length > 0 : "failed precondition"; int indexOfMin = 0; for(int i = 1; i < list.length; i++) { if(list[i] < list[indexOfMin]) { indexOfMin = i; } } return indexOfMin; } Question: draw DFG from the code above find the all-def/c/p use paths. Generate test cases to test this function using JUnit! (to test all-def/c/p use path)arrow_forwardWrite a loop that counts how many elements in an array are equal to zero. arrays.cpp 1 #include // sizet 2 int countZeros (const int values[], size_t size) { int count = 0; 3 4 for (int i; i using namespace std; 3 2 4 int countZeros(const int values[], size_t size); 5 int main() { int a[] = {1, 2, 0, 3}; cout <« countZeros (a, 4) <« endl; cout « "Expected: 1" « endl; 6 7 8 9 10 11 int b[] = {0, 2, 0, 3}; cout <« countZeros (b, 4) <« endl; cout « "Expected: 2" « endl; 12 13 14 15 int cl] -{1, 0, θ, 0, 0 ; cout <« countZeros (c, 5) <« endl; cout « "Expected: 4" « endl; 16 17 18 19 } CodeCheck Reset Testers Running Tester.cpp pass fail fail 1 Expected: 1 Expected: 2 Expected: 4 Score 1/3arrow_forwardstruct nodeType { int infoData; nodeType * next; }; nodeType *first; … and containing the values(see image) Using a loop to reach the end of the list, write a code segment that deletes all the nodes in the list. Ensure the code performs all memory ‘cleanup’ functions.arrow_forward
- X40: sum3 Given an array containing three ints, return the sum of all the elements. Examples: sum3({1, 2, 3}) -> 6 sum3({5, 11, 2}) -> 18 Your Answer: 1 public int sum3(int[] nums) 2{ 3 4} Check my answer! Reset Next exercisearrow_forwardHeapsort has heapified an array to: 91 41 13 34 63 55 and is about to start the second for loop. What is the array after the first iteration of the second for loop? Ex: 98, 36, 41arrow_forwardJava coding Can you create a remove method that removes and returns an element from an index in an array? Thanks.arrow_forward
- Computer Sciencearrow_forwardLab 16 Implementing bubble sort In this lab, you will implement the bubble sort algorithm. The bubble sort is so called because it compares adjacent items, "bubbling" the smaller one up toward the beginning of the array. By comparing all pairs of adjacent items starting at the end of the array, the smallest item is guaranteed to reach the beginning of the array at the end of the first pass.The second pass begins again at the end of the array, ultimately placing the second smallest item in the second position. During the second pass, there is no need to compare the first and second items, because the smallest element is guaranteed to be in the first position.Bubble sort takes at most n - 1 passes for an array of n items. During the first pass, n - 1 pairs need to be compared. During the second pass, n - 2 pairs need to be compared. During the ith pass, n - i pairs need to be compared. During the last pass, n - (n - 1) or one pair needs to be compared. If, during any pass, no two…arrow_forwardJava, Insertion Sort Table. Thank youarrow_forward
- Correct errorsarrow_forwardMake List Items Uppercase 2 in python Define the function make_uppercase(mylist), which takes a list parameter mylist (a list of strings), mutates the list by uppercasing each string in mylist. The function returns None. For example, if mylist is ['cat', 'Dog', 'frOG'], then the mutated list should be ['CAT', 'DOG', 'FROG']. For example: Test Result mylist = ['cat', 'Dog', 'frOG'] result = make_uppercase(mylist) print(mylist) if result != None: print("Error, return value should be None") ['CAT', 'DOG', 'FROG'] mylist = ['baNanas', 'appLes', 'pEAches', 'PEArs'] result = make_uppercase(mylist) print(mylist) if result != None: print("Error, return value should be None") ['BANANAS', 'APPLES', 'PEACHES', 'PEARS']arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY