Question

In: Computer Science

1a .Write a program that perform insertion sort (ascending order) on an input array and print...

1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30).

1b. Write a program that perform insertion sort while printing the value of input array after each step of insertion sort from the leftmost element to the rightmost element. You can implement by using any programming languages such as C/C++, Java, Python, etc. After that, perform test on the given array A.

A= [7,6, 12, 9, 3, 4, 5, 11, 9, 14, 2, 8]

Solutions

Expert Solution

Answer 1:

public class InsertionSort {
   /* Function to sort array using insertion sort */
   static int sort(int values[], int numValues) {
       int n = numValues;
       int count=0;
       for (int i = 1; i < n; ++i) {
           int key = values[i];
           int j = i - 1;

           while (j >= 0 && values[j] > (key)) {
               values[j + 1] = values[j];
               j = j - 1;
               count++;
           }
           values[j + 1] = key;
           //printArray(values);
       }
       return count+2;
   }

   /* A utility function to print array of size n */
   static void printArray(int arr[]) {
       int n = arr.length;
       for (int i = 0; i < n; ++i)
           System.out.print(arr[i] + ",");

       System.out.println();
       System.out.println();
   }

   // Driver method
   public static void main(String args[]) {
       int arr[] = {30 , 10 , 20 , 40};
       int c=sort(arr, arr.length);
       printArray(arr);
       System.out.println("Number of comparions : "+c);
   }
}

Answer 2:

public class InsertionSort {
   /* Function to sort array using insertion sort */
   static int sort(int values[], int numValues) {
       int n = numValues;
       int count=0;
       for (int i = 1; i < n; ++i) {
           int key = values[i];
           int j = i - 1;

           while (j >= 0 && values[j] > (key)) {
               values[j + 1] = values[j];
               j = j - 1;
               count++;
           }
           values[j + 1] = key;
           printArray(values);
       }
       return count+2;
   }

   /* A utility function to print array of size n */
   static void printArray(int arr[]) {
       int n = arr.length;
       for (int i = 0; i < n; ++i)
           System.out.print(arr[i] + ",");

       System.out.println();
       System.out.println();
   }

   // Driver method
   public static void main(String args[]) {
       int arr[] = {7,6, 12, 9, 3, 4, 5, 11, 9, 14, 2, 8};
       int c=sort(arr, arr.length);
       System.out.println("Number of comparions : "+c);
   }
}


Related Solutions

Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show...
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show all steps. 7          11        2          9          5          14 Q4. State the number of comparisons for each pass. Pass # comparisons 1st 2nd 3rd 4th 5th
write a program in C language Create a function to perform the insertion sort. Now the...
write a program in C language Create a function to perform the insertion sort. Now the program will perform the following steps: Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Use dynamic memory allocation to allocate an array of N single precision floating-point elements (C type float). Read the N single precision floating-points elements to the allocated array. Invoke a function to sort the array using insertion sort (the insertion...
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
What is the running time of Quicksort when the input array is sorted in ascending order?...
What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT