Arrays, LL and recursion Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How does insertion and removal happen in Arrays?

A

To remove, you store the onject that you want to remove, start your for loop from the position of that object and start overwriting it’s position with elements towards its right, decrease the size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give the psuedo-code for the insertion sort

A
  1. loop from 0 to -1 the number of elements in the array
  2. while j >0 (to control the j– and to continue looping in case we missed some) && arr[j-1] >arr[j]
  3. have a local variable j be initialized to i.
  4. while j > 0 and arr[j-1] > arr[j] // while the one before is greater
  5. store that element
  6. set arr[j] = arr[j-1] // not the other way around because we do not want to overwrite the value of arr[j-1]
  7. set arr[j-1] = stored
  8. j–
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Position ADT?

A

A unified way of viewing a single element stored in a data structure like a node of a LL or a cell of an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What two parts do nodes store in a SLL?

A

An element and a link to the next node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

T/F most operations in a SLL run in O(1) time?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is it a hassle removing at the tail of a SLL?

A

> You have to iterate through all the nodes up until your next is the tail, set current.next = null, tail = current. Because we have to go through almost all the nodes, say n nodes till we reach the one before the tail. This makes its runtime complexity to be O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an algorithm for insertion at the tail as well as it’s runtime complexity

A
  1. Create a new node, set its next to null\
  2. Set the “tail’s” next to be the new node (IMPORTANT)
  3. make the new node the tail
  4. increment the size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

(T/F) The head of a SLL cannot store any elements. It is simply a reference to the beginning of the list and nothing else.

A

False, the head can store an element as well as the tail

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which operation of a SLL has O(n) complexity?

And which one of a DLL has O(n) complexity?

A

SLL : removal at the tail

DLL: none

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are experimental studies and what are 3 of their drawbacks?

A

Experimental studies are a method of measuring the efficiency of an algorithm by examining how much time it takes to execute that particular algorithm

  1. You have to execute the whole algorithm to find out how efficient it is but this is time consuming
  2. You have to test with all different types of input, but this is sometimes not possible
  3. You have to run the algorithm on the same hardware and software that it was originally tested on to get similar results
How well did you know this?
1
Not at all
2
3
4
5
Perfectly