Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1

8. How many swappings are required to sort the following array in descending
order?

int arr [6] = {8, 22, 7, 0, 5, 13};

1- Bubble Sort; 2- Insertion Sort; 3- Selection Sort

Original array: [8, 22, 7, 0, 5, 13]

1. Bubble Sort:

Pass 1: [22, 8, 7, 0, 5, 13] - 1 swap

Pass 2: [22, 8, 7, 5, 0, 13] - 1 swap

Pass 3: [22, 8, 7, 5, 13, 0] - 1 swap

Total swaps = 3

2. Insertion Sort:

Insert 8: [22, 8, 7, 0, 5, 13] - 0 swaps (8 is already in the correct position)

Insert 7: [22, 8, 7, 0, 5, 13] - 0 swaps (7 is already in the correct position)

Insert 0: [22, 8, 7, 5, 0, 13] - 1 swap

Insert 5: [22, 8, 7, 5, 0, 13] - 0 swaps (5 is already in the correct position)

Insert 13: [22, 13, 8, 7, 5, 0] - 3 swaps

Total swaps = 4
3. Selection Sort:

Select 22: [22, 8, 7, 0, 5, 13] - 0 swaps (22 is already in the correct position)

Select 13: [22, 13, 7, 0, 5, 8] - 1 swap

Select 8: [22, 13, 8, 0, 5, 7] - 1 swap

Select 7: [22, 13, 8, 7, 5, 0] - 1 swap

Select 5: [22, 13, 8, 7, 5, 0] - 0 swaps (5 is already in the correct position)

Total swaps = 3

9.. How many swappings are required to sort the following array in ascending
order? int arr [6] = {8, 22, 7, 0, 5, 13}; 1- Bubble Sort; 2- Insertion Sort; 3- Selection
Sort

Original array: [8, 22, 7, 0, 5, 13]

1. Bubble Sort:

Pass 1: [8, 7, 0, 5, 13, 22] - 3 swaps

Pass 2: [7, 0, 5, 8, 13, 22] - 1 swap

Pass 3: [0, 5, 7, 8, 13, 22] - 1 swap

Total swaps = 5

2. Insertion Sort:

Insert 22: [8, 22, 7, 0, 5, 13] - 0 swaps (22 is already in the correct position)

Insert 7: [7, 8, 22, 0, 5, 13] - 1 swap

Insert 0: [0, 7, 8, 22, 5, 13] - 2 swaps

Insert 5: [0, 5, 7, 8, 22, 13] - 1 swap

Insert 13: [0, 5, 7, 8, 13, 22] - 1 swap

Total swaps = 5

3. Selection Sort:
Select 0: [0, 22, 7, 8, 5, 13] - 1 swap

Select 5: [0, 5, 7, 8, 22, 13] - 1 swap

Select 7: [0, 5, 7, 8, 22, 13] - 0 swaps (7 is already in the correct position)

Select 8: [0, 5, 7, 8, 22, 13] - 0 swaps (8 is already in the correct position)

Select 13: [0, 5, 7, 8, 13, 22] - 1 swap

Total swaps = 3

10.

11.

12.

13.What is a graph data structure?

A graph data structure is a collection of nodes and edges between them, representing
relationships or connections.

14. What are the components of a graph?

The components of a graph include:

1. Vertices (or Nodes): These are the fundamental units of a graph, representing
entities or points.

2. Edges: These are the connections or relationships between the vertices.

3. Weight (or Cost): Edges can have weights or costs associated with them,
representing the magnitude or cost of the relationship between vertices.

4. Degree of a Vertex: The degree of a vertex is the number of edges incident to it.

5. Path:is a sequence of adjacent vertices

6. Cycle: is a path where the first and last vertices are the same.
15

16

17

18

19

20

21

22

23

24. Which ADT uses FILO? What does FILO mean? What about FIFO? Which ADT
uses it?

FILO (First In, Last Out):An ADT that follows FILO is the stack.
(FIFO):(First In, First Out) An ADT that follows FIFO is A queue

25. What is a merge Sort? Explain with an example sorting values in an array

Merge Sort is a sorting algorithm that works by dividing an array into smaller subarrays,
sorting each subarray, and then merging the sorted subarrays back together to produce
a final sorted array.

The original array [2, 4, 1, 3]

1. Divide: Split the array into two halves: and .[2, 4][1, 3]

2. Recursive Calls:

Left half: Since [2, 4] has only two elements, sorted already.
Right half: Since [1, 3] has only two elements, sorted already.

3. Merge:
To merge the [2, 4] and [1, 3]: we compare the first elements of each sublist: 2
and 1. Since 1 is smaller, we add it to the merged list. Next, we compare 2 from
the first sublist with 3 from the second list. Again, 2 is smaller, so we add it to the
merged list. The final merged list becomes [1, 2, 3, 4].

26. What is an ordered linear search? Explain with an example sorting values in
an array and a key

An ordered linear search, is a searching algorithm that works on sorted arrays. It


traverses the elements of the array one by one until it finds the desired key or reaches
the end of the array

Let's say we have an array of integers sorted in ascending order:

2,4,6,8,Now, let's search for a key, for example, 8.

1. Start from the first element of the array.


2. Compare the key (8) with the current element.
3. If the current element is equal to the key, return the index of the element.
4. If the current element is greater than the key, stop the search because the key
cannot exist in the array (due to sorting).
5. If the current element is less than the key, move to the next element and repeat
steps 2-4.

27.a. What is a stack data structure? What are the main operations found in a
stack implementation? Explain with an example.

a. Stack Data Structure:is a linear data structure that follows the Last In, First Out
(LIFO) principle.
Example A pile of plates piled on top of each other

Main Operations in a Stack:

1. Push(): adds an element to the top


2. Pop(): removes the top element
3. Peek/Top(): retrieves the top element of the stack
4. isEmpty(): checks if the stack is empty.
5. isFull(): checks if the stack is full.

b. What is a queue data structure? What are the main operations found in a queue
implementation? Explain with an example.

b.A queue is another linear data structure that follows the First-In-First-Out (FIFO)
principle.
Example A line of cars on a one way road

Main Operations in a Stack:

1. Enqueue(): Adds an element to queue.


2. Dequeue (): Removes an element queue.
3. Peek(): Gets the data element at the front node without deleting it.
4. IsEmpty(): Checks if the queue is empty.
5. isFull(): checks if the stack is full.

28. What are the differences between singly, doubly and circular linked lists?

Feature Singly Linked List Doubly Linked Circular Linked List


List

Traversal Traversal is Forward only Forward and Forward only in a loop


backward

Finding Can be slow Potentially faster Might loop forever


element
Memory Less More (extra Less (no null at end)
usage pointer)

Function Contains element and link Contains element The last element is
to the next node and link to the linked back to the first
next and previous element, forming a
node circular loop.

29. What are the advantages of arrays?

a. Objects of mixed data types can be stored

b. Elements in an array cannot be sorted

c. Easier to store elements of same data type

d. Index of first element of an array is 1

30. What would be the asymptotic time complexity to find an element in the linked
list?

a. O(1) b. O(n) c. O(n2) d. O(n4)

You might also like