Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q4: Assume that the vector has been created with size 5 and all of its elements

ID: 3606170 • Letter: Q

Question

Q4: Assume that the vector has been created with size 5 and all of its elements have been previously initialized to-1 a) What will this vector look like before the first operation below? b) What is the output and content of a Vector after each operation in the table below? Operation set (0,10) insert (2, 15) at (1) set (4,19) insert (3,5) erase (2) erase (3) size () at (2) empty ) Output Vector content Q5: Given the following sequence, show the first pass of the bubble sort algorithm 10, 12, 1, 3,6, 3

Explanation / Answer

a) -1, -1, -1, -1, -1

b) Output Vector Content

After set(0,10) : iterator of Begining of the vector 10,-1,-1,-1,-1

After insert(2,15): An iterator that points to the first of the newly inserted elements. 10,-1,15,-1,-1,-1

After at(1): The element at the specified position in the container i.e. -1    10,-1,15,-1,-1,-1

After set(4,19): Iterator of Begining of the vector 10,-1,15,-1,19,-1

After insert(3,5): An iterator that points to the first of the newly inserted elements. 10,-1,15,5,-1,19,-1

After erase(2): An iterator pointing to the new location of the element that followed the last element eased by the function call. 10,-1,5,-1,19,-1

After erase(3):   An iterator pointing to the new location of the element that followed the last element eased by the function call. 10,-1,5,19,-1

After size() : 5 10,-1,5,19,-1

After at(2) : The element at the specified position in the container i.e. 5 10,-1,5,19,-1

After empty()   false 10,-1,5,19,-1

3) Given 10, 12, 1, 3, 6, 3

First pass of bubble sort (in bubble sort we compare the adjacent numbers and shift the largest number to the end in each pass)

compare (10,12): 10 < 12: no swapping --> 10, 12, 1, 3, 6, 3

compare(12,1) : 12 > 1 : swap 12 with 1 --> 10, 1, 12, 3, 6, 3

compare(12,3) : 12 > 3 : swap 12 with 3 --> 10, 1, 3, 12, 6, 3

compare(12,6) : 12 > 6 : swap 12 with 6 --> 10, 1, 3, 6, 12, 3

compare(12,3) : 12 > 3 : swap 12 with 3 --> 10, 1, 3, 6, 3, 12