Mid Semester Exam For APT2060A Spring 2023
Mid Semester Exam For APT2060A Spring 2023
(ii) Write down an algorithm for sorting data using bubble sort technique (3
marks)
1. Set n to the number of elements in the array
2. Repeat steps 3-6 n times
3. Set swapped to false
4. For i = 0 to n-2, do steps 5-6
1
5. If the ith element is greater than the (i+1)th element, swap them and set swapped to
true
6. Go to the next pair of adjacent elements
7. If swapped is false, the list is sorted, so stop
8. Otherwise, go to step 3 and repeat the process until the list is sorted
(iii) Demonstrate how to sort the following data 5,34,23,45,78,25,9 using bubble sort
technique. Indicate the status of each value after every pass
(7 marks)
Start by comparing the first two elements (5 and 34), if 5 is greater than 34, swap them,
otherwise leave them as is.
Array: 5, 34, 23, 45, 78, 25, 9
Compare the second and third elements (34 and 23), since 34 is greater than 23, swap
them.
Array: 5, 23, 34, 45, 78, 25, 9
Compare the third and fourth elements (34 and 45), since they are already in order, leave
them as is.
Array: 5, 23, 34, 45, 78, 25, 9
Compare the fourth and fifth elements (45 and 78), since they are already in order, leave
them as is.
Array: 5, 23, 34, 45, 78, 25, 9
Compare the fifth and sixth elements (78 and 25), since 78 is greater than 25, swap them.
Array: 5, 23, 34, 45, 25, 78, 9
Compare the sixth and seventh elements (78 and 9), since 78 is greater than 9, swap
them.
Array: 5, 23, 34, 45, 25, 9, 78
Repeat the above steps until no more swaps are needed. On the second pass:
Array: 5, 23, 34, 25, 45, 9, 78
Third pass:
Array: 5, 23, 25, 34, 9, 45, 78
2
Fourth pass:
Array: 5, 23, 25, 9, 34, 45, 78
Fifth pass:
Array: 5, 23, 9, 25, 34, 45, 78
Sixth pass:
Array: 5, 9, 23, 25, 34, 45, 78
Since no more swaps are needed, the array is now sorted in ascending order:
Array: 5, 9, 23, 25, 34, 45, 78
Assume you got the following information where 10 student’s marks have been inserted in the
array student.
int size=40;
int student[size];
(i) Explain how a new student mark can be inserted at location 2 (3 marks)
1. Check if location 2 is within the bounds of the array (i.e., 2 >= 0 and 2
< size)
2. If the location is valid, then shift all the elements from the location 2 to
size-2 by one position to the right to create an empty space for the new
mark
3. Insert the new student mark at location 2 by assigning it to student 2
(ii) Explain how student mark at location 4 can be deleted from the array (3
marks)
3
1. Check if location 4 is within the bounds of the array (i.e., 4 >= 0 and 4 <
size)
2. If the location is valid, then shift all the elements from location 5 to size-1
by one position to the left to fill the gap left by the deleted mark
3. Set the last element of the array to 0 or any default value to indicate that
the array has one less element.
(iii) Write down an algorithm for sorting data in an array using selection-sort (4
marks)
1. Start with the first element in the array and assume it is the smallest
2. Compare the assumed smallest element with the next element in the array. If the
next element is smaller, then assume it is the smallest.
3. Continue comparing the assumed smallest element with the next element until the
end of the array is reached.
4. Swap the assumed smallest element with the first element in the array.
5. Repeat steps 2 to 4 for the remaining elements in the array until the array is sorted.
(iv) Write down an algorithm for sorting data in an array using insertion-sort (4
marks)
1. Start with the second element in the array and assume it is in the correct position.
2. Compare the second element with the first element in the array. If the second
element is smaller, then swap them.
3. Insert the third element in the correct position between the first and second
elements.
4. Compare the fourth element with the first three elements and insert it in the correct
position.
5. Repeat step 4 for the remaining elements until the array is sorted.
4
Question 3 (10 marks)
(i) Briefly describe stack abstract data structure that can be implemented using array
structure.
(3 marks)
(ii) Write down an algorithm for inserting and deleting integer data in a stack Assume the
stack structure will be implemented using an array
(7 marks)
Queue is an abstract data type that follows the FIFO (First-In-First-Out) principle. It
represents a collection of elements that are inserted at the back and deleted from the front.
It can be visualized as a line of people waiting for a service, where the first person to
enter the line is served first.
(ii) Write down an algorithm for inserting and deleting integer data in a queue. Assume the
queue structure will be implemented using an array
(7 marks)
5
Question 5(10 Marks)
Binary search is a search algorithm used to find the position of a target value in a sorted
array. It works by repeatedly dividing in half the portion of the array that could contain
the target value, until the value is found or it is determined that the value is not in the
array.
(ii) Write down an algorithm for searching data using binary search technique. Assume the
data are stored in an array structure (7
marks)
1. Initialize variables: low = 0, high = n-1, where n is the size of the array.
2. Repeat the following steps until low is greater than high:
a. Calculate mid as the average of low and high: mid = (low + high) / 2
b. If the value at the mid index is equal to the target value, return mid.
c. If the value at the mid index is greater than the target value, set high to mid - 1.
d. If the value at the mid index is less than the target value, set low to mid + 1.
3. If the target value is not found, return -1.
struct Node {
int key;
Node* nextlink;
};
class LinkedList {
6
Node *head, *tail;
public:
LinkedList() {
head = tail = NULL;
}
void createList() {
int i, n;
int x;
cout << "How many values? ";
cin >> n;
cout << "Enter values: ";
for (i = 0; i < n; i++) {
cin >> x;
Node* newNode = new Node();
newNode->key = x;
newNode->nextlink = NULL;
if (head == NULL)
head = tail = newNode;
else {
tail->nextlink = newNode;
tail = newNode;
}
}
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->key << " ";
7
temp = temp->nextlink;
}
}
};
int main() {
LinkedList usiu;
usiu.createList();
usiu.display();
return 0;
}