Question

In: Computer Science

Data Structures and Algorithms CMPS 2720 PLEASE ANSWER CLEARLY 10. Suppose a dataset has N items....

Data Structures and Algorithms CMPS 2720

PLEASE ANSWER CLEARLY

10. Suppose a dataset has N items. On average, how many comparisons and how many saves (not swaps) are needed to sort the dataset using the bubble sort?

11. Suppose a dataset has N items. On average, how many comparisons and how many saves (not swaps) are needed to sort the dataset using the select sort?

12. Suppose a dataset has N items. On average, how many comparisons and how many saves (not swaps) are needed to sort the dataset using the insertion sort?

13. Suppose that an array contains the eight number listed below. How many saves (not swaps) are needed to perform the first pass of the bubble sort? 18 20 12 14 10 16 32 26

14. Suppose that an array contains the eight number listed below. How many saves are needed to perform the first pass of the select sort? 18 20 12 14 10 16 32 26

5. What effect, if any, does the consideration of duplicates in an array have in searching for all records with a specific key?

Solutions

Expert Solution

ANSWERS:

10) Suppose for n-elements, the bubble-sort algo is written as :

// A function to implement bubble sort

void bubbleSort(int arr[], int n)

{

    int i, j;

    for (i = 0; i < n-1; i++)    

    // Last i elements are already in place

    for (j = 0; j < n-i-1; j++)

        if (arr[j] > arr[j+1])

            swap(&arr[j], &arr[j+1]);

}

So, comparison happens in every loop. Please find the illustration of bubble-sort of 8 elements:

So, for n-elements, the total number of comparisons will be: (n-1)*(n-2)/2

Total number of saves can be equal to number of comparisons if the array is already sorted. So, in that case, no swap will happen.

11) Please find below algo for selection sort. The best part is, it first finds the smallest element, and then swaps with the ith index.

void selectionSort(int arr[], int n)

{

    int i, j, min_idx;

    // One by one move boundary of unsorted subarray

    for (i = 0; i < n-1; i++)

    {

        // Find the minimum element in unsorted array

        min_idx = i;

        for (j = i+1; j < n; j++)

        if (arr[j] < arr[min_idx])

            min_idx = j;

        // Swap the found minimum element with the first element

        swap(&arr[min_idx], &arr[i]);

    }

}

So, for n-elements, the total number of comparisons will be: (n-1)*(n-2)/2

And, number of swaps will be equal to N only.

12) Please find below algo for insertion sort:

/* Function to sort an array using insertion sort*/

void insertionSort(int arr[], int n)

{

    int i, key, j;

    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)

        {

            arr[j + 1] = arr[j];

            j = j - 1;

        }

        arr[j + 1] = key;

    }

}

So, for n-elements, the total number of comparisons will be: (n-1)*(n-2)/2

And, number of swaps will be maximum equal to N only.

13) Suppose the elements to be sorted by bubble-sort are:

18 20 12 14 10 16 32 26

So, in 1st pass:

18 20 12 14 10 16 32 26 -> 18 20 12 14 10 16 32 26 (18 and 20 are compared and no swap is performed as 18<20)

18 20 12 14 10 16 32 26 -> 18 12 20 14 10 16 32 26 (20 and 12 are compared and swap is performed)

18 12 20 14 10 16 32 26 -> 18 12 14 20 10 16 32 26  (20 and 14 are compared and swap is performed)

18 12 14 20 10 16 32 26 -> 18 12 14 10 20 16 32 26 (20 and 10 are compared and swap is performed)

18 12 14 10 20 16 32 26 -> 18 12 14 10 16 20 32 26 (20 and 16 are compared and swap is performed)

18 12 14 10 16 20 32 26 -> 18 12 14 10 16 20 32 26 (20 and 32 are compared and no swap is performed as 20<32)

18 12 14 10 16 20 32 26 -> 18 12 14 10 16 20 26 32 (32 and 26 are compared and swap is performed)

Hence, total number of saves in the first pass of this bubble sort is 2.

14) In the selection sort of given array,

18 20 12 14 10 16 32 26
In the first pass, the minimum element is discovered from the remaining array, and swapped with 0th element.

So, after first pass the array will look like:

18 20 12 14 10 16 32 26 ->  10 20 12 14 18 16 32 26

So, single swap happened in first pass and no save happened.

15) To search all records with specific key, duplicates needs to be considered as duplicate values of given key increases the resultant count that needs to be returned. If the array is already sorted, then duplicates make it even easier to find the first occurrence and last occurrence of the specified key and find the total elements that exists. So, duplicates should always be considered while searching foe a key.

PS. I hope the answers are complete and upto the expectations. Please comment if anything else is required in the answer.


Related Solutions

(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and...
(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and $n-1$ keys. We would like to merge those two sorted sequences by performing $o(n)$ comparisons. (Note we are interested in comparisons, not running time.) Show how this can be done or argue that it cannot be done. Note: In class we showed that ordinary merging would require no more than $\lg{n}+n-1+1= n + \lg{n}$ comparisons.
Please respond to the short answer items using the below dataset. For any values with long...
Please respond to the short answer items using the below dataset. For any values with long decimals, please round to two decimal places. X Y 7    16 5 2 6 1 3 2 4    9 What is the value of SP, SSX, SSY, and Pearson correlation r? For the regression equation Y = bX + a, what is the value of b? For the regression equation Y = bX + a, what is the value of a? For the regression...
This question is in reference to BFS and DFS for data structures and algorithms Consider a...
This question is in reference to BFS and DFS for data structures and algorithms Consider a graph algorithm with a growth function on V and E: f(V, E). How would you convert f(V,E) to f'(V) such that f(V,E)=O(g(n))=f(V)? (That is, convert a growth function of two variables to be of one variable in such a way that the Big-Oh bound for the one variable function will hold for the two variable function.) Explain the steps in creating f', and explain...
Thoroughly explain the impacts of data structures and algorithms in the development and implementation of computer...
Thoroughly explain the impacts of data structures and algorithms in the development and implementation of computer programs using modern computer technologies.
What are the data structures and algorithms? Why are they important to software developers? Give an...
What are the data structures and algorithms? Why are they important to software developers? Give an example of using a data structure. Give an example of using algorithm. Use Google to search for your answers. Minimum 400 words and a maximum of 1000 words.
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data...
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data structure. Your queue should fully implemnted the following methods: first, push_back (enqueue), pop_front (dequeue), size, and isEmpty. Make sure to include a driver to test your newly implemented queue
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List...
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List of Bus Transit and Passengers You are to implement a C++ program for City Bus Transit using linked list data structure to maintain record of passengers. Specifically, you are to implement the following methods/functions: For Passenger: o A function which can create a new node of the linked list using new for each newpassenger o A function that prints the time of single passenger...
This all has to be in javascript Use the following variables and data structures to answer...
This all has to be in javascript Use the following variables and data structures to answer this question. var queue = new Queue(); var stack = new Stack(); 1. Enqueue the numbers 1, 2, 3, 4, and 5 in the queue. Then print the contents of the queue to the console. Note: queue and stack both have a print() method. 2. Dequeue each number from the queue and push it onto the stack. Then print the contents of the stack...
Individuals and organizations build various data structures and algorithms to solve real-life problems. Furthermore, many data...
Individuals and organizations build various data structures and algorithms to solve real-life problems. Furthermore, many data analysts tend to use Big-O notation for analyzing algorithms. In your own words, explain ways by which people can specify (i). data structures and (ii) algorithms, that they build and use.
PLEASE CLEARLY STATE AND ANSWER EACH QUESTION ( 1THROUGH 6) The following data gives the selling...
PLEASE CLEARLY STATE AND ANSWER EACH QUESTION ( 1THROUGH 6) The following data gives the selling price, square footage, number of bedrooms, and age (in years) of condominiums that were sold in a neighborhood in the Bronx in the past six months Selling Price Square Footage No. of Bedrooms Age of Condo 64000 1670 2 30 59000 1339 2 25 61500 1712 3 30 79000 1840 3 40 87500 2300 3 18 92500 2234 3 30 95000 2311 3 19...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT