Data Structure
Data Structure
REG.NO. 14/02496
CAT 1
Discuss the steps involved in the development of an algorithm. (4 Marks)
i. Define the problem-clearly state what the algorithm will do.e.g problem could be: calculate
speed given time and didtance.
v. Test the algorithm-using a pen and a paper go through the steps and plug in the inputs to
determine the output.
Distinguish between lists and arrays and provide a scenario where either is ideal in a
situation. (4 Marks)
Linked list is a complex data type that organize elements inform of a set of nodes where each
node has a pointer to some other node while an array is a built in data structure that contains a
set of elements of the same type stored in contiguous location.
Arrays are used to implement complex data structures e.g. hash tables
Linked list is used to create complex data structures e.g. binary tree
i. Linked lists
Linear complex data structure that stores values in nodes such that each node has data part
and a pointer to next node.
Applications:
Used in memory management.
Creating other complex data structures.
Implementation
Implemented using pointers and structures.
Applications:
Used to store addresses, undo/redo, Convert infix expressions into postfix or prefix, stack of
cards.
iii. Queues
Linear complex data structure that stores values at one end called rear and removes values
from another end called front. Follows first in, first out policy (FIFO).
a) Demonstrate how the following sorting algorithm will be used to sort the array in
ascending order.
i. Merge sort (3 Marks)
34 56 19 41 34 51 32
34 56 19 41 34 51 32
34 56 41 34 51 32
34 56 19 41 34 51 32
14 28 33 99 109 18
14 28 33 99 109 18
14 28 99 109
14 28 99 109 18
33
P
34 56 19 41 34 51 32 14 28 33 99 109 18
L R
Case 1: 34<18 No
Swap
P
18 56 19 41 34 51 32 14 28 33 99 109 34
L R
CASE 2: 56<34 NO
Swap
P
18 34 19 41 34 51 32 14 28 33 99 109 56
L R
CASE 3: 99<56 NO
Swap P
18 34 19 41 34 51 32 14 28 33 56 109 99
L R
CASE 4: 109<99 NO
Swap P
18 34 19 41 34 51 32 14 28 33 56 99 109
L R
New array
P
18 34 19 41 34 51 32 14 28 33 56 99
LR
P
18 34 19 41 34 51 32 14 28 33 56
LR
P
18 34 19 41 34 51 32 14 28 33
L R
CASE: 18<14 NO
SWAP
P
14 18 19 28 32 33 34 34 41 51
LR
b) Write a Program that implements each of the above algorithms. The program
accepts an array of n numbers ( such as the ones given above , outputs the sequence
of steps for each sorting step and prints the final output (6 Marks)
Marge sort
#include<iostream>
using namespace std;
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void merge(int *array, int l, int m, int r) {
int i, j, k, nl, nr;
//size of left and right sub-arrays
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr];
//fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;
//marge temp arrays to real array
while(i < nl && j<nr) {
if(larr[i] <= rarr[j]) {
array[k] = larr[i];
i++;
}else{
array[k] = rarr[j];
j++;
}
k++;
}
while(i<nl) { //extra element in left array
array[k] = larr[i];
i++; k++;
}
while(j<nr) { //extra element in right array
array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int *array, int l, int r) {
int m;
if(l < r) {
int m = l+(r-l)/2;
// Sort first and second arrays
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r);
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
mergeSort(arr, 0, n-1); //(n-1) for last index
cout << "Array after Sorting: ";
display(arr, n);
}
Consider the following binary tree below