Data Structures Exam 3
Consider the following code segment. count = 0; for(i = 1; i < n; ++i) for(j = 0; j < n/2; ++j) ++count; cout << count << endl; What is the output of the code segment if n = 10? What is the output value of the code segment in terms of n? (NOT in Big-Oh.)
Consider the following code segment. count = 0; for(i = 1; i < n; ++i) for(j = 0; j < n/2; ++j) ++count; cout << count << endl; What is the output of the code segment if n = 10? ******45******* What is the output value of the code segment in terms of n? (NOT in Big-Oh.) *******(n-1)n/2
A binary search is faster on large lists, but a sequential search is faster on small lists.
False
A binary search is faster on ordered lists and slower on unordered lists.
False
A sequential search of a list assumes that the list is in ascending order.
False
If two sorting algorithms have the same running-time in Big-Oh, say, O(n²), then both algorithms take exactly the same amount of time to sort the same array.
False
The level of the root node of a binary tree is 1.
False
Fill in blanks and complete the following function, which so algorithm. void selectionSort(int a[], int n) { for(int i = 0; i < n - 1; ++i) { int k = i; for(int j = i+1; j < n; ++j) if(a[j] < a[k]) _______________________; int temp = a[k]; a[k] = a[i]; a[i] = temp; } }
Fill in blanks and complete the following function, which so algorithm. void selectionSort(int a[], int n) { for(int i = 0; i < n - 1; ++i) { int k = i; for(int j = i+1; j < n; ++j) if(a[j] < a[k]) ___k=j__________; int temp = a[k]; a[k] = a[i]; a[i] = temp; } }
Fill in blanks and complete the following function, which sorts an array using the bubble sort algorithm. void bubbleSort(int a[], int n) { for(int i = 1; i < n; ++i; for(int j = _____; j < _____; ++j) if(a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j +1]; a[j+1] = temp; } }
Fill in blanks and complete the following function, which sorts an array using the bubble sort algorithm. void bubbleSort(int a[], int n) { for(int i = 1; i < n; ++i; for(int j = __0__; j < _n-i_; ++j) if(a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j +1]; a[j+1] = temp; } }
Fill in the blanks and complete the following function, which sorts an array using the insertion sort algorithm. void insertionSort(int a[], int n) { int i, j; int temp; for(i = 1; i < n; ++i) { temp = a[i]; for(j = ______; j >= 0 && a[j] > temp; --j) a[j+1] = a[j]; a[j+1] = _____; } }
Fill in the blanks and complete the following function, which sorts an array using the insertion sort algorithm. void insertionSort(int a[], int n) { int i, j; int temp; for(i = 1; i < n; ++i) { temp = a[i]; for(j = __i-1__; j >= 0 && a[j] > temp; --j) a[j+1] = a[j]; a[j+1] = _temp__; } }
Fill in the blanks and coplete the merge function shown below for merging two linked-lists: while(p1 !=NULL && p2 != NULL) { ________________; ________________; ________________; } else{ ________________; ________________; ________________; } } if(p1 != NULL) ________________; else if(p2 != NULL) ________________; return head; }
Fill in the blanks and coplete the merge function shown below for merging two linked-lists: while(p1 !=NULL && p2 != NULL) { __tail->next=p1_____; __tail=p1__________; __p1=p1->next______; } else{ __tail->next=p2_____; __tail=p2__________; __p2=p2->next______; } } if(p1 != NULL) __tail->next=p1_____; else if(p2 != NULL) __tail->next=p2_____; return head; }
A binary search tree T is either empty or:
T has a special node called the root node: T has two sets of nodes, Lt and Rt, called left subtree and the right subtree of T, respectively: The key in the root node is larger than every key in the left subtree and smaller than every key in the right subtree: Lt and Rt are binary search trees:
Postorder traversal
Traverse left subtree Traverse right subtree Visit Node
Inorder traversal:
Traverse left subtree Visit Node Traverse the right subtree
A binary search of a list assumes that the list is sorted.
True
The inorder traversal of a binary search tree outputs the keys in ascending order.
True
The running-time of Mergesort is O(n log n), where n is the input size.
True
The running-time of bubble sort is 0(n²), where n is the input (i.e., array) size.
True
Preorder traversal
Visit Node Traverse left subtree Traverse right subtree
Sort the following list of integers sorted in an array to the first partition using quick sort. 20, 59, 83, 36, 28, 13, 23, 14, 98, 17.
20, 17, 14, 23, 13, 28, 36, 83, 98, 59