You are on page 1of 4

DSA March 2015

Qsn1
a) Pointer: A pointer is a derived data type that stores the address of another variable. It contains
memory addresses as their values and can be used to assign, access, and manipulate data
values stored in the memory allotted to a variable since it can access the memory address of
that variable. Pointers are used to store the location of a value present in the memory. The
process of obtaining the value stored at a location being referenced by a pointer is known as
dereferencing.
b) Recursion: The process in which a function calls itself directly or indirectly is called recursion
and the corresponding function is called a recursive function.
c) Queue: A queue is a linear data structure that is open at both ends and the operations are
performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and all
deletions from the list are made at the other end. The element which is first pushed into the
order, the delete operation is first performed on that.
d) A stack is a linear data structure in which the insertion of a new element and removal of an
existing element takes place at the same end represented as the top of the stack.s
e) An algorithm is defined as a step-by-step procedure or method for solving a problem by a
computer in a finite number of steps. Steps of an algorithm definition may include branching
or repetition depending upon what problem the algorithm is being developed for. While
defining an algorithm steps are written in human understandable language and independent of
any programming language. We can implement it in any programming language of our
choice.
Besides merely being a finite set of rules which gives a sequence of operations for solving a
specific type of problem, a well defined algorithm has five important features:
 Finiteness. An algorithm must always terminate after a finite number of steps.
 Definiteness. Each step of an algorithm must be precisely defined; the actions to be carried
out must be rigorously and unambiguously specified for each case.
 Input. An algorithm has zero or more inputs, i.e, quantities which are given to it initially
before the algorithm begins.
 Output. An algorithm has one or more outputs i.e, quantities which have a specified relation
to the inputs.
 Effectiveness. An algorithm is also generally expected to be effective. This means that all of
the operations to be performed in the algorithm must be sufficiently basic that they can in
principle be done exactly and in a finite length of time.
f) Bubble sort: Bubble sort is a sorting algorithm that compares two adjacent elements and
swaps them until they are in the intended order. Just like the movement of air bubbles in the
water that rise up to the surface, each element of the array move to the end in each iteration.
Therefore, it is called a bubble sort.
Working of Bubble Sort
Suppose we are trying to sort the elements in ascending order.
1. First Iteration (Compare and Swap)
Starting from the first index, compare the first and the second elements.
If the first element is greater than the second element, they are swapped.
Now, compare the second and the third elements. Swap them if they are not in order.
The above process goes on until the last element.

2. Remaining Iteration
The same process goes on for the remaining iterations.
After each iteration, the largest element among the unsorted elements is placed at the end.

In each iteration, the comparison takes place up to the last unsorted element.

The array is sorted when all the unsorted elements are placed at their correct positions.
Bubble Sort Code in C++
// Bubble sort in C++

#include <iostream>
using namespace std;

// perform bubble sort


void bubbleSort(int array[], int size) {

// loop to access each array element


for (int step = 0; step < size; ++step) {

// loop to compare array elements


for (int i = 0; i < size - step; ++i) {

// compare two adjacent elements


// change > to < to sort in descending order
if (array[i] > array[i + 1]) {

// swapping elements if elements


// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}

// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}
cout << "\n";
}
int main() {
int data[] = {-2, 45, 0, 11, -9};

// find array's length


int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);

cout << "Sorted Array in Ascending Order:\n";


printArray(data, size);
}

2. GRAPH TERMS
i) Indegree:

You might also like