Question

In: Computer Science

DATA STRUCTURES CLASS CMPS 2720 clear answers ESSAY TYPE ANSWERS 1) A) Suppose an array contains...

DATA STRUCTURES CLASS CMPS 2720

clear answers

ESSAY TYPE ANSWERS

1) A) Suppose an array contains N unsorted keys. What is the minimum number of comparisons necessary to find a specific key value? What is the maximum number? What is the average number?

b)   Suppose an array contains 2000 sorted keys. What is the maximum number of comparisons necessary to find a given key value if a binary search is used?

c) Suppose a large number of records need to be stored into and retrieved out of an array. What are the advantages and disadvantages of the keeping the keys in the array sorted? Be sure to address time required to store, delete, retrieve, and find data. What effect would the size of individual records (in bytes) have?

d) What is the difference between a queue and a priority queue? Be sure to address not only the structure of the data structures, but also the time required to insert an object into the queue/priority queue, and the time to remove an item from the queue/priority queue.

e) One way to sort data would be to insert the values, one at a time, into a priority queue without removing any values. Once the entire list has been entered, the data could then be removed from the queue one at a time in order. Which of the sorts would this method be most like? How efficient would this method be compared with the other methods?

f)   Suppose that 12, 20, 14 are inserted onto an empty queue, then one number removed from the queue. Suppose then that 19, 26, 10, and 17 are inserted into the queue and two numbers removed. How many numbers are in the queue at the conclusion of the procedures, and what are the values in the queue, listing the items from the first that would be removed to the last that would be removed?

g)  Compare the three sorting techniques from chapter 3, namely the bubble sort, the select sort, and the insertions sort. What are the strengths of each, and what are the weaknesses of each? Is one of the sorts always better than another, or are there cases where one sort is faster and other cases where a different sort is faster?

Solutions

Expert Solution

a) Since the array contains N unsorted elements, binary search cannot be applicable. Hence we use linear search to find the key.

To find the key, the best case is to find the key in first comparison. Therefore, the minimum number of comparisons required is 1. In the worst case, we need to traverse the entire array and the key may be found in the last comparison or even may not be found. Therefore maximum of N comparisons are required. In an average case, the key can be found in the middle of the array which requires around N/2 comparisons.

b) The array contains 2000 sorted elements. The algorithm used is binary search. Hence in the worst case, we can have log2N number of comparisons. log2N = log22000 = 10.96 = 11 (approx).

c) Suppose we have N records.

If we want to keep the records in a sorted order, it takes O(N) time complexity to insert a new record. Otherwise, the insertion of a new record takes O(1) time complexity.

If the records are sorted then worst case search time complexity becomes O(log2N) which in turn reduces the time complexity for deleting a record (Since to delete a record, we first need to find the record). If the records are not sorted, the worst case search time complexity would be O(N) which is also the time complexity for deleting the record.

The size of the individual records does not effect the time complexity, but effects the space complexity. But still we search the records based on the key values, and retrieve only the required fields from the record, the size of the record does not have much impact on the insertion, deletion and search operations.

d) A queue is a linear data structure in which the two ends are open and elements can be inserted from one end and deleted from another end. A priority queue is an extension of a queue in which every element is associated with a priority.

The queue operations insertion and deletion takes O(1) time complexity. While for the priority queue, the time complexities are O(1) and O(N) for each of the operations based on the type of the queue. If it is an ordered priority queue, the insertion takes O(N) time and deletion takes O(1) time whereas for an un-ordered queue, the insertion takes O(1) and deletion takes O(N) time complexity. The operation complexity can be reduced from O(N) to O(logN) if we use a heap to represent the priority queue.

e) The method is similar to insertion sort algorithm, In the insertion sort, the new element to be inserted is compared with the already sorted array and placed in its correct position. The number of swaps or comparisons of insertion sort are less then that of the bubble sort and the best case time complexity is O(N). Hence insertion sort suits this algorithm well.

f) The elements 12, 20 and 14 are inserted into an empty queue.

12 20 14 .................

One number is removed from the queue i.e, 12 will be removed. Then the queue becomes

20 14 ..............

Now, 19, 26, 10 and 17 are inserted into the queue.

20 14 19 26 10 17 ...........

Now, two numbers are removed i.e, 20 and 14 are removed.

19 26 10 17 ...........

At the conclusion, 4 items are left in the queue. they are 19, 26, 10 and 17.

Now, if another deletion operation is to be performed, then 19 will be the first item to be removed and then 26 and then 10 and at last 17 will be removed.

g) Bubble sort repeatedly compares and swaps (if needed) adjacent elements in every pass. Selection sort selects ith smallest element and places it in the ith position. In insertion sort, the (i-1) previous elements are already sorted and ith element is inserted into its proper place in the previous sorted sub-array.

Algorithm Time complexity
Best case Average case Worst case
bubble sort O(N) O(N2) O(N2)
selection sort O(N2) O(N2) O(N2)
insertion sort O(N) O(N2) O(N2)

The space complexity of the three algorithms is O(1).

Strengths:

Bubble sort:

  • simplest sorting technique.
  • The algorithm can be optimized by terminating it when the array is sorted.

Selection sort:

  • The number of swaps are reduced to O(N).
  • can be used in list data structures such as linked lists.

Insertion sort:

  • Number of swaps are reduced when compared to bubble sort.
  • works efficiently for smaller values of N.

Weaknesses:

Bubble sort:

  • comparatively slower algorithm
  • Requires more number of swaps.

Selection sort:

  • Time complexity is O(N2) even for the best case.

Insertion sort:

  • Inefficient for large values of N.

In terms of number of comparisons, bubble sort always gives worst case performance for random data, reversely sorted data, highly repetitive data and almost sorted data.

Insertion and selection sorts give the same performance for reversely sorted data, whereas for other types of data, selection sort gives better performance than insertion sort.


Related Solutions

Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Name the data type,value type and array size / name of the followings. 1) 20.4 2)...
Name the data type,value type and array size / name of the followings. 1) 20.4 2) a=5 3) [2 3 8] 4) ' 7 ' 5) ' abc' 6) pi 7) b=true 8) 20+2i 9) [ 'a' 'b' 'c' ] 10) [ 'a' ; 'b' ] 11) [1 2 ;4 5;7 8] 12) [20,21,22,23] 13) c= [a;b] 14) c= [b:a] 15) d=[b:a ; b:a] 16) magic(3) 17 uint8(20) 18) int8(d)
LAB2 PART 1 FOR THE DATA TYPE CLASS: If you do not have UML of class...
LAB2 PART 1 FOR THE DATA TYPE CLASS: If you do not have UML of class Account_yourLastName, you should do that first Basesd on the UML, provide the code of data type class Account_yourLastName to hold the information of an account with account number (String), name (String), balance (double), with no-argument constructor, parameter constructor Also, define some methods to handle the tasks: open account, check current balance, deposit, withdraw, and print monthly statement. At the end of each task we...
Write a class DataSet that stores a number of values of type double in an array...
Write a class DataSet that stores a number of values of type double in an array list. Provide methods to insert a value to the array list and to compute the sum, average, and maximum value. Please put a note on what you did is for what. So I can understand this question better. Code provided: import java.util.*; public class DataSet {    private ArrayList<Double> data; /** Constructs an empty data set. @param maximumNumberOfValues the maximum this data set can...
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
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
You need a data structure that contains fields for name (char array), color (char array), age...
You need a data structure that contains fields for name (char array), color (char array), age (int), and height (int). Name the struct Info when you define it. Create a function named getUserInfo() that asks the user for name, color, age, and height and then returns a data structure containing that information. It should do the following: What is your name? George What is your favorite color? Green What is your age? 111 What is your height in inches? 72...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list) The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after...
In Java Define the EvenNumber class for representing an even number. The class contains: A data...
In Java Define the EvenNumber class for representing an even number. The class contains: A data field value of the int type that represents the integer value stored in the object. A no-arg constructor that creates an EvenNumber object for the value 0. A constructor that constructs an EvenNumber object with the specified value. A function named getValue() to return an int value for this object. A function named getNext() to return an EvenNumber object that represents the next even...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT