cs214 - Mid - ModelAnswer
cs214 - Mid - ModelAnswer
2. For the insertion sort algorithm, what is the best-case scenario for an array of size n?
a) The array is sorted
b) The array is reversely sorted
c) The array is not sorted
d) Both (a) and (b)
3. Which of the following algorithms runs in N.log N average time but quadratic worst-
case time?
a) Insertion sort b) Merge sort
c) Quick sort d) Shell sort
5. If a header node is used, which of the following indicates a list with one item?
a) Head != NULL b) Head != NULL && Head->next ==NULL
c) Head == NULL d) Head != NULL && Head->next !=NULL &&
Head->next->next ==NULL
6. Which of the following operations is performed more efficiently by doubly linked list
than by singly linked list?
7. Which of the following statements inserts a new node of value val, right before
current?
1/4
Question 2: Answer the following: [6 marks]
A. Apply the Bubble sort algorithm on the following array showing every swap: (5
marks)
20 10 90 7 50
10 20 90 7 50
10 20 7 90 50
10 20 7 50 90
10 7 20 50 90
7 10 20 50 90
B. If the array [3, 6, 9, 10] was given to be sorted by bubble sort algorithm, how many
swaps will be done? What about number of swaps if the same array was given to
selection sort? (1 mark)
Bubble sort: 0 swaps
Selection sort: 3 swaps
i. A->info 32
ii. list->next->info 32
2/4
B. Write one statement to do each of the following: (3 marks)
B= B->next;
A->info = 32;
list->next->info = 32;
template<class T>
C. Given the following class declaration of a single class SLL
linked list, implement the member function void {
Node<T> *head, *tail;
SLL::moveTailToFront(). The function moves public:
the tail node to the front of the list before the SLL(){head = tail = 0;}
head node. You may call other member void addToHead(T);
void addToTail(T);
functions in your code. (8 marks) void moveTailToFront();
};
Example: For the linked list L:
90 95 80 60
head tail
head tail
template<class T>
void SLL<T>::moveTailToFront()
{
//if list is emoty or has 1 node only, do nothing
if(head == tail) return;
//our target node, which is the tail node will point to head
tail->setNext(head);
3/4
//change the tail to be prev
tail = prevNode;
}
{
MyStack<int> s;
for(int i=0;i< _size;i++)
s.push(A[i]);
for(int i=0;i< _size;i++)
A[i] = s.pop();
}
4/4