0% found this document useful (0 votes)
36 views4 pages

Data Structures Pasco

The document discusses various sorting algorithms like insertion sort, radix sort, and selection sort and their implementation. It also discusses different data structures like arrays, linked lists, queues and trees. Key concepts like tree traversal, leaf nodes, non-terminal nodes are explained. Different ways of traversing a tree like preorder and postorder are provided with an example.

Uploaded by

Matin Odoom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views4 pages

Data Structures Pasco

The document discusses various sorting algorithms like insertion sort, radix sort, and selection sort and their implementation. It also discusses different data structures like arrays, linked lists, queues and trees. Key concepts like tree traversal, leaf nodes, non-terminal nodes are explained. Different ways of traversing a tree like preorder and postorder are provided with an example.

Uploaded by

Matin Odoom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

-----------

Question 1
-----------
128 47 8521 186 774 8975 741 121 100 500 7789 7410
i. Insertion sort(ascending order)
pass 1 = 128 47 8521 186 774 8975 741 121 100 500 7789 7410
pass 2 = 47 128 8521 186 774 8975 741 121 100 500 7789 7410
pass 3 = 47 128 8521 186 774 8975 741 121 100 500 7789 7410
pass 4 = 47 128 186 8521 774 8975 741 121 100 500 7789 7410
pass 5 = 47 128 186 774 8521 8975 741 121 100 500 7789 7410
psss 6 = 47 128 186 774 8521 8975 741 121 100 500 7789 7410
pass 7 = 47 128 186 741 774 8521 8975 121 100 500 7789 7410
pass 8 = 47 121 128 186 741 774 8521 8975 100 500 7789 7410
pass 9 = 47 100 121 128 186 741 774 8521 8975 500 7789 7410
pass 10= 47 100 121 128 186 500 741 774 8521 8975 7789 7410
pass 11= 47 100 121 128 186 500 741 774 7789 8521 8975 7410
pass 12 =47 100 121 128 186 500 741 774 7410 7789 8521 8975

ii.
Radix sort(descending order)
According to one's digit
- 7789 128 47 186 8975 774 8521 741 121 100 500 7410

According to the ten's digit


- 7789 186 8975 774 47 741 128 8521 121 7410 100 500

According to hundred's digit


- 8975 7789 774 741 8521 500 7410 186 128 121 100 47

According to thousand's digit


- 8975 8521 7789 7410 774 741 500 186 128 121 100 47

iii. Selection sort(descending order)


pass 1 = 8975 47 8521 186 774 128 741 121 100 500 7789 7410
pass 2 = 8975 8521 47 186 774 128 741 121 100 500 7789 7410
pass 3 = 8975 8521 7789 186 774 128 741 121 100 500 47 7410
pass 4 = 8975 8521 7789 7410 774 128 741 121 100 500 47 186
pass 5 = 8975 8521 7789 7410 774 128 741 121 100 500 47 186
pass 6 = 8975 8521 7789 7410 774 741 128 121 100 500 47 186
pass 7 = 8975 8521 7789 7410 774 741 500 121 100 128 47 186
pass 8 = 8975 8521 7789 7410 774 741 500 186 100 128 47 121
pass 9 = 8975 8521 7789 7410 774 741 500 186 128 100 47 121
pass 10= 8975 8521 7789 7410 774 741 500 186 128 121 47 100
pass 11= 8975 8521 7789 7410 774 741 500 186 128 121 100 47
pass 12= 8975 8521 7789 7410 774 741 500 186 128 121 100 47

iii. 3 comparisons

iv. It will not be found, and the number of comparisons will 4

v.
//code for bubble sort
#include <iostream>

using namespace std;

int main(){
int n;
cout << "Enter the length of your array ";
cin >> n;
cout << "Enter array values\n";
int numbers_array[n];
for (int i = 0; i < n; i++) {
cin >> numbers_array[i];
}

for (int i = 0; i < n; i++) {


for (int j = 1; j < n - i; j++) {
if (numbers_array[j - 1] > numbers_array[j]) {
int temp = numbers_array[j];
numbers_array[j] = numbers_array[j - 1];
numbers_array[j - 1] = temp;
}
}
}

cout << "After Bubble sort the array is:\n";


for (int i = 0; i < n; i++)
cout << numbers_array[i] << " ";
return 0;
}

------------
Question 2
-----------
b.In row major order
A[i][j] = B + W(i*n+j)
Given that = B = 25
i= 3
j = 6
n = 11
W = 2
Therefore
A[3][6] = 25+2(3*11+6)
A[3][6] = 103

c . Linear linked list is a collections of data elements(nodes) whose order is not


given by physical arrangements in memory.

d. In the linear linked list, the tail node or the the terminal node points to
nothing
or null whilst in the circular linked list the terminal node points back to
the first
or the head node giving it a ring-like or circular structure.

e. Given i = 1, j= 2,m = 3,n=3 ,size 4 and base address = 200


But for column major order,
A[i][j] = B + W*[(i*Lr) + M(j - Lc)]
But Lr and Lc = 0
therefore
A[1][2] = 200 + 4(1 + 3*(2))
= 228

-------------
Question 3
------------
a. An array is a series of elements of the same
type placed in contiguous memory locations
that can be individually referenced by
adding an index to a unique identifier.

b. i. Elements in arrays are of the same data type


ii. The indexes of arrays range from 0 up to n-1 where n is the length of the
array
iii. Global and static arrays are automatically initialized with their default
values
iv. Regular Array of local scope will NOT be initialized to any default value

c . i Insertion
With insertion in a linked list data structure, there are 2 cases. Is either
we add the
incoming node to the front the linked list or it is inserted into any other
location.
Case 1
--------
If the incoming
node is supposed to be inserted at the front, the head field of the linked
list class points to the incoming
node(new Node) and then the pointer field of the incoming node points to the
initial head node.
Case 2
---------
But if the it supposed to be inserted at any other position then we go
through the list until the
location reached, we then points the previous node in that location to the
incoming node and then we finally points
the incoming node to the next node of that location.

ii. Deletion
With deletion, there are also 2 cases we need to consider. Is either the node
to be deleted is at the front(head node)
or it can be found at any other location.

Case 1
-------
If the node to be deleted is actually the head node, then all we would do is
to point the head field of the linked list
class to the next or second node

Case 2
-------
If the node is in any other location then we would go through the list until
the node to be deleted is found, if found we then
point the node(previous node) before the one we want it to be delete to the
node which is after the one want to deleted.

iii. Linear Search


With linear search in linked list, we would try to go through the list and
compare each node data field to the one value we want
to find. This will continue until, either we find the item and break out of
the loop, or we end up at the tail of the list.

d. Insertion in a Queue(Enqueue)
in insertion in a queue, we first check whether the queue is full(thus rear =
max -1 ), if this condition is met, this therefore means
that the queue.
But if this condition is not met then we increase our rear value by one
(since we start our rear value at -1 when the queue is empty),
and then we insert a value at the location using rear as it index
( queue[rear] = data).
We then finally check if the front is -1, if this true we then assign the
front variable to be zero, since the first item has been inserted.

----------
Question 4
-------------
i .a. A leaf in a tree data structure is simply node with no child.
b. A forest is a union of tree data structures where each tree is not connected
to each other
c. Tree traversal is the process of visting(checking or updating) each node in a
tree data structure.
d. Terminal node is node that have its child node to be null(thus ends the
structure) or points to no node.
e. Node is a basic unit of data structures such as linked list, tree data
structure etc which contains a data field, and may or may not have pointer
to the a next node

ii. Non terminal node -- A,B,C,E,F,I


Terminal node --- D,G,H,J

Level 0 - A
Level 1 - B,C
Level 2 - D,E,F
Level 3 - G,H,I
Level 4 - J

iii. needs to be drawn

iv. Preorder
A B D E G H C F I J
Post order left right root
D G H E B J I F C A

The time complexity of both traversal is O(n)

You might also like