Docsity

Data Structures and Algorithms Latest Midterm Exam, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures and
Algorithms Latest Midterm
Exam with Verified Answers
Abstract Data Types - ANSA class considered without regard to its
implementation
E.g. Stacks, queues, or lists
Arrays - ANSAdvantages
- Easy to access a particular item
- Insertion is fast in an unordered array
- Searching is fast in ordered array
- Data locality in memory
Disadvantages:
- Some algorithms require large number of shifting/moves
- The size of an array cannot be changed once created
- If the size is too large... waste of memory
- If the size is too small, it cannot be expanded dynamically
Binary Search - ANSFast, but the array must be sorted log2(N)
Drawbacks --> insertion takes longer
O(logN)
Bubble Sort - ANSComparisons: O(N^2)
pf3
pf4
pf5

Partial preview of the text

Download Data Structures and Algorithms Latest Midterm Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structures and

Algorithms Latest Midterm

Exam with Verified Answers

Abstract Data Types - ANSA class considered without regard to its implementation E.g. Stacks, queues, or lists Arrays - ANSAdvantages

  • Easy to access a particular item
  • Insertion is fast in an unordered array
  • Searching is fast in ordered array
  • Data locality in memory Disadvantages:
  • Some algorithms require large number of shifting/moves
  • The size of an array cannot be changed once created
  • If the size is too large... waste of memory
  • If the size is too small, it cannot be expanded dynamically Binary Search - ANSFast, but the array must be sorted log2(N) Drawbacks --> insertion takes longer O(logN) Bubble Sort - ANSComparisons: O(N^2)

Swaps: O(N^2) (less than comparisons) Big O: O(N^2) Double-ended linked-list - ANSContains an additional reference to the last link

  • Makes it possible to insert a new link directly at the end of the list without the need to iterate along the entire list O(1)
  • Suitable for situations like for implementing a queue Doubly Linked List - ANSAllows to traverse the list forward and backward There are now two references within each link instead of one Benefits:
  • Traverse backward
  • More flexibility in removing, inserting objects
  • Deletion from the end of the list is O(1) Drawback:
  • Need to manage multiple pointers Enhanced Insertion sort - ANSComparisons: O(NlogN) Shift: O(N^2) Big O: O(N^2) --> faster than bubble sort Insertion Sort - ANSComparisons: O(N^2) --> max is N(N-1)/2, average is N(N-1)/ Shifts (copies): O(N^2) --> shift is not as time consuming as a swap
  • Number of comparisons is O(N) like for arrays but items do not need to be shifted/movies --> expected to be faster than using arrays Linked-List: Implementation of stacks/queues - ANS- Does not require the knowledge of the maxSize
  • Only allocate the right amount of memory for stack items
  • Linked-list provides more flexibility than arrays if one cannot predict maxSize (speed/complexity is similar) Queues: Easy to manipulate using double-ended feature Linked-List: Sorted Linked List - ANSInsertion/removal likely to be faster than using an array, but still O(N)
  • much more difficult to implement than a sorted array
  • Efficient sorting mechanism
  • Is still O(N^2), but only 2N copies vs O(N^2) --> however it requires twice as much memory (array+linked-list) List Insertion Sort - ANSComparisons: O(N^2) Copies: O(N) O(N^2) Merge sort - ANSComparisons: O(N) Copies: O(N) Remarks: additional memory needed, but very efficient, can be done recursively O(NlogN)

Notion of Partitioning - ANSAction of dividing into groups depending on key value

  • The underlying mechanism of quicksort O(N) but fewer swaps than comparisons Ordered arrays - ANSfast binary search O(logN) slow insertion and deletion O(N) Priority Queue - ANSdequeue is O(1) but enqueue is O(N) --> slow insertion, so priority queue is often implemented using a heap to improve insertion time Quick Sort - ANSAverage is O(NlogN)
  • Worst case: O(N^2) when an array is inversely sorted Improvements
  • Median-three-partitioning
  • finding the median of the first, last and middle elements (or directly sorting the elements)
  • Using an insertion sort procedure for small partitions
  • Hybrid scheme - do quicksort )(NlogN) until the array is almost sorted and then use InsertionSort which is then O(N) Recursion - ANSProgramming technique in which a method calls itself Efficiency
  • calling a method involves some overhead