Question

In: Computer Science

a. Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112,...

a. Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1} using Insertion Sort and determine the total number of comparisons made (do not count swaps)

b. Sort the list stated in 5a) but using Merge Sort

Solutions

Expert Solution

Below is the c++ solution with output screenshot

Code (a) insertion sort :

// include libraries
#include <bits/stdc++.h>
using namespace std;

// function to print an array of size n
void printArray(int arr[], int n)
{
        int i;
        for (i = 0; i < n; i++)
                cout << arr[i] << " ";
        cout << endl;
}

/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
        int i, key, j, comparision=0;
        for (i = 1; i < n; i++)
        {
                key = arr[i];
                j = i - 1;

                /* Move elements of arr[0..i-1], that are
                greater than key, to one position ahead
                of their current position */
                while (j >= 0 && arr[j] > key)
                {
                    comparision++;
                        arr[j + 1] = arr[j];
                        j = j - 1;
                }
                arr[j + 1] = key;
        }
    cout<<"\nArray after insertion sort is:\n";
    printArray(arr, n);
    cout<<"total number of comparisons made = "<<comparision;
}

// main method
int main()
{
        int arr[] = { 20, 13, 4, 34, 5, 15, 90, 100, 75, 102, 112, 1};
        int n = sizeof(arr) / sizeof(arr[0]);
    cout<<"Original array:\n";
    printArray(arr,n);
        insertionSort(arr, n);
        return 0;
}

Output : insertion sort

Total number of comparisons made = 21

Code (b) Merge sort :

// C++ program for Merge Sort
#include<iostream>
using namespace std;

// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
    int n1 = m - l + 1;
    int n2 = r - m;

    // Create temp arrays
    int L[n1], R[n2];

    // Copy data to temp arrays L[] and R[]
    for(int i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for(int j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

    // Merge the temp arrays back into arr[l..r]

    // Initial index of first subarray
    int i = 0;

    // Initial index of second subarray
    int j = 0;

    // Initial index of merged subarray
    int k = l;

    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    // Copy the remaining elements of
    // L[], if there are any
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    // Copy the remaining elements of
    // R[], if there are any
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

// l is for left index and r is
// right index of the sub-array
// of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {

        // Same as (l+r)/2, but avoids
        // overflow for large l and h
        int m = l + (r - l) / 2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);

        merge(arr, l, m, r);
    }
}

// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
    for(int i = 0; i < size; i++)
        cout << A[i] << " ";
}

int main()
{
    int arr[] = { 20, 13, 4, 34, 5, 15, 90, 100, 75, 102, 112, 1 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);

    cout << "Given array is :\n";
    printArray(arr, arr_size);

    mergeSort(arr, 0, arr_size - 1);

    cout << "\n\nArray after merge sort is :\n";
    printArray(arr, arr_size);
    return 0;
}

Output (merge sort) :


Related Solutions

Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1}...
Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1} using Insertion Sort and determine the total number of comparisons made (do not count swaps) b.Sort the list stated in 5a) but using Merge Sort
Final_exam assignment_grade Tutorial_attend 100 90 5 100 75 5 90 75 5 85 85 5 85...
Final_exam assignment_grade Tutorial_attend 100 90 5 100 75 5 90 75 5 85 85 5 85 100 5 80 95 5 70 80 5 60 95 5 60 80 5 55 95 5 55 25 4 50 80 5 45 90 5 40 65 5 40 65 4 35 0 3 30 70 4 30 55 4 25 85 5 25 90 4 15 5 3 15 80 5 15 50 5 15 45 3 5 75 3 5 70...
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25?...
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25? = 200 25a − 15? − 5? = 300 10? + 20? − 30? + 100? = 400 how to do flowchart using gauss elimination and lu decomposition method
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25?...
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25? = 200 25a − 15? − 5? = 300 10? + 20? − 30? + 100? = 400 how to write coding in matlab using lu decomposition
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Selection sort and shell sort....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Selection sort and shell sort. Write the algorithms. show work. please
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write...
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write the algorithm.
investment poor average good A 50 75 20 B 80 15 40 C -100 300 50...
investment poor average good A 50 75 20 B 80 15 40 C -100 300 50 Maximax, Maximin, Minimax Regreat, Hurxicz 0.4, Equal liklihood
Given: Butter Guns MC 0 100 1 90 2 75 3 55 4 30 5 0...
Given: Butter Guns MC 0 100 1 90 2 75 3 55 4 30 5 0 Complete the marginal cost column (MC). Identify the opportunity cost increasing butter production from 1 to 2 units on the graph. Graphically illustrate the Keynesian versus classical position of the economy and their respective opportunity costs (two graphs). Explain the policy implications of these two alternative assumptions of the economy (Per lesson notes).
A list of 100 numbers has the largest value 90 and the smallest value 12.9, with...
A list of 100 numbers has the largest value 90 and the smallest value 12.9, with a mean 40, a median 50, and a standard deviation 20. However, you accidentally copied the smallest number “12.9” as “1.29”. Is it possible to determine by how much the mean, the median and the standard deviation change? For each quantity, if so, show your computation. Otherwise, explain why
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT