Question

Compare the worst case run time complexity of the below functions in Unsorted arrays, sorted arrays...

Compare the worst case run time complexity of the below functions in Unsorted arrays, sorted arrays and linked lists(unsorted) (15 points).

  • a) Find (given an element, return the index)
  • b) Insert (given an element, insert it)
  • c) Delete (given an element, find it then delete it)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) Find (given an element, return the index)

Unsorted Array: In unsorted array given an element, finding it's location takes O(n) in the worst case. This is because in the worst case the element we want to find may present at the end of an array.

Sorted array: It takes O(log n) time. Since the array is sorted, we can use binary search to find location of an element which takes O(log n) in worst case.

Linked list(Unsorted): It takes O(n) time. In the worst case the element we are looking art may present at the end of the linked list and hence we need to traverse entire linked list which takes O(n) time.

b) Insert (given an element, insert it)

Unsorted Array: It takes O(1) time. array is unsorted, so insertion can be done at end of array which takes O(1) time.

Sorted array: Worst case time complexity of it is O(n) time. Since the array is sorted, to find it's appropriate location to insert it takes O(log n) and to insert we may have to shift all other element to it's right in worst case, which takes O(n) time. so total time complexity is equal to O(n+log n) which is equal to O(n).

Unsorted Linked list:

Case(i): If reference to start of the linked list is given: Then it takes O(n) time to insert. This is because iterator has to traverse from head to the end of the list to insert an element

Case(ii): If reference to location of element we want insert is given: Then it takes O(1) time.

c) Delete (given an element, find it then delete it)

Unsorted Array: It takes O(n) time. To find an element it takes O(n) time for finding and O(n) for deletion. (i.e, deletion may happen in the beginning and all the elements have to shifted to their left by one place). So total time is O(n+n) = O(n).

Sorted array: Worst case time complexity of it is O(n) time. It takes O(log n) time for finding and O(n) for deletion. (i.e, deletion may happen in the beginning and all the elements have to shifted to their left by one place). So total time is O(log n+n) = O(n).

Unsorted Linked list:

If reference to start of the linked list is given: Then it takes O(n) time to delete. This is because iterator has to traverse from head to location of an element which takes O(n) in the worst case and for deletion it takes O(1) to delete it. Total time it takes is O(n+1) = O(n).

If the reference to the position of element we want delete is given it takes O(1) time.

Summary Table:

Find Insert Delete(finding and then delete)
Unsorted Array O(n) O(1) O(n)
Sorted Array O(log n) O(n) O(n)
Unsorted Linked list O(n) O(n) or O(1)* O(n)

* indicates that it takes O(1) time to insert if pointer to the location where new element needs to be inserted is given, else it has to traverse from head to the end of list which takes O(n) time.

Add a comment
Know the answer?
Add Answer to:
Compare the worst case run time complexity of the below functions in Unsorted arrays, sorted arrays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2.What is the order of complexity of the insert function in the worst case? You are...

    2.What is the order of complexity of the insert function in the worst case? You are required to provide a complexity function, and a proof that your complexity function belongs to a particular complexity class. In your complexity function you may specify the number of comparisons performed, as a function of input size. def insert(element, sorted): if len(sorted) == 0: return [element] else: if sorted[0] >= element: new_copy = sorted[:] new_copy.insert(0, element) return new_copy else: return [sorted[0]] + insert(element, sorted[1:])

  • 2.1 Given an unsorted std::vector<int> and a number n, what is the worst-case time complexity for...

    2.1 Given an unsorted std::vector<int> and a number n, what is the worst-case time complexity for finding the pair of integers whose sum is closest to n, using no additional memory? For example, given the vector {12, 3, 17, 5, 7} and n = 13, we would get the pair {5, 7}.

  • 1. For each of the following tasks find the complexity of the algorithm using big O...

    1. For each of the following tasks find the complexity of the algorithm using big O notation. You must justify your answer with 1-2 lines of explanation. a) Insert a new element into an unsorted linked list b) Insert a new element into a sorted linked list c) Remove the minimum element in an unsorted linked list d) Remove the minimum element in a sorted linked list

  • Given two arrays A and B of n integers both of which are sorted in ascending...

    Given two arrays A and B of n integers both of which are sorted in ascending order. Write an algorithm to check whether or not A and B have an element in common. Find the worst case number of array element comparisons done by this algorithm as a function of n and its Big-O complexity

  • 2. Here is a sorting algorithm that I like to use. Given an unsorted list of...

    2. Here is a sorting algorithm that I like to use. Given an unsorted list of size n, let Xx represent the data in location k of the unsorted list. Compare xi to X2 and switch if necessary so that they are in sorted order, smallest first. Compare Xn-1 and Xn and switch if necessary so that they are in sorted order, smallest first. • Compare x3 with its left neighbors, switching if necessary so that the 3 first entries...

  • 1. What is the worst case time complexity of insertion into a binary search tree with...

    1. What is the worst case time complexity of insertion into a binary search tree with n elements? You should use the most accurate asymptotic notation for your answer. 2. A binary search tree is given in the following. Draw the resulting binary search tree (to the right of the given tree) after deleting the node with key value 8. 10 3. You have a sorted array B with n elements, where n is very large. Array C is obtained...

  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • Given the following code find the worst case time complexity binary search (target: integer, a[1..n ]:...

    Given the following code find the worst case time complexity binary search (target: integer, a[1..n ]: ascending integers) k =1 j =n loop when (k is less than j) m =floor((k+j)/2) if (target is larger than the element at m) then k = m+1 else j = m endloop if (target equals element at k) then location=k else location =0

  • 1.If a list is implemented as a singly linked stack, give the big-O worst-case time complexity...

    1.If a list is implemented as a singly linked stack, give the big-O worst-case time complexity of the following operations (as usual use the smallest standard big-O category that works: a) push_front, b) push_back, c) lookup, d) read the i'th member 2.Repeat question 3 for a dynamic array (for example, as in the C++ vector class)

  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT