DS UNIT-I Material
DS UNIT-I Material
Linear data structure: If the elements of a data structure are stored in a linear or sequential
order, then it is a linear data structure.
Examples: arrays, linked lists, stacks, and queues.
Arrays: An array is a collection of similar data elements. These data elements have the same
data type. The elements of the array are stored in consecutive memory locations and are
referenced by an index.
syntax:
datatype name[size];
Ex: int marks[10];
Linked lists: Linked list is a dynamic data structure in which elements (called nodes) form a
sequential list.
In a linked list, each node is allocated space as it is added to the list.
Every node in the list points to the next node in the list.
Every node contains the following: The value of the node or any other data that corresponds to
that node. A pointer or link to the next node in the list.
The first node in the list is pointed by Head/Start/First. The last node in the list contains a NULL
pointer to indicate that it is the end or tail of the list.
Stack: A stack is a linear data structure in which insertion and deletion of elements are done at
only one end, which is known as the top of the stack.
Stack is called a last-in, first-out (LIFO).
Stacks can be implemented using arrays or linked lists.
Queue : A Queue is a linear data structure in which insertion can be done at rear end and
deletion of elements can be dome at front end.
A queue is a first-in, first-out (FIFO)
Queues can be implemented by using either arrays or linked lists.
Non-linear data structure: If the elements of a data structure are not stored in a sequential
order, then it is a non-linear data structure.
Examples: trees and graphs.
Trees: A tree is a non-linear data structure which consists of a collection of nodes arranged in a
hierarchical order.
One of the nodes is designated as the root node, and the remaining nodes can be partitioned into
disjoint sets such that each set is a sub-tree of the root.
Graphs: A graph is a non-linear data structure which is a collection of vertices (also called
nodes) and edges that connect these vertices. A node in the graph may represent a city and the
edges connecting the nodes.
(b) Explain different operations on stack ADT.
5. a) Illustrate insertion sort algorithm and trace the steps of insertion sort for sorting the
list with an example.
In Insertion sort the list can be divided into two parts, one is sorted list and other is
unsorted list. In each pass the first element of unsorted list is transfers to sorted list by
inserting it in appropriate position or proper place.
The similarity can be understood from the style we arrange a deck of cards. This sort
works on the principle of inserting an element at a particular position, hence the name
Insertion Sort.
Algorithm:
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted
array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to the next
element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with
27. We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
Then we move to the next two values, 35 and 10. We know then that 10 is smaller 35.
We swap these values. We find that we have reached the end of the array. After one iteration, the
array should look like this –
To be defined, we are now showing how an array should look like after each iteration. After the
second iteration, it should look like this
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
7. a) Illustrate the differences between insertion and selection sort with an example.
Insertion sort Selection sort
Gradually creates a sorted section at the Finds the minimum (or maximum) element
beginning of the list, inserting each new item from the unsorted section and places it at the
in its proper place in this section. end of the sorted section.
Generally more efficient than selection sort, Less efficient, especially for large lists, as it
especially for small lists or partially sorted lists
always performs O(n) comparisons for each
pass.
O(n) (when the list is already sorted) O(n^2), regardless of the initial order of the
elements
O(n^2) (when the list is sorted in reverse order) O(n^2), regardless of the initial order of the
elements.
O(n^2), regardless of the initial order of the Suitable for scenarios where swapping cost is
elements. high, as it makes minimal number of swaps
b) Illustrate the differences between bubble and selection sort with an example.
Bubble sort Selection sort
Bubble sort involves comparing and potentially Selection sort involves finding the smallest
swapping two adjacent elements. If the element in the list and swapping it with the
elements are in the correct order, we move to first element in the unsorted portion of the list.
the next pair.
The time complexity of Bubble sort is O(n) in The time complexity of Selection sort is O(n2)
the best case and O(n2) in the worst case in both best and worst cases.
Bubble sort is generally less efficient compared Selection sort is generally more efficient
to selection sort. compared to bubble sort.
Bubble sort uses a swapping method. Selection sort uses a selection method.
Bubble sort is usually slower than selection Selection sort is usually faster than bubble sort.
sort.
2. Space Complexity: Space Complexity can be defined as amount of memory (or) space
required by an Algorithm to run.
The space requirement S(p) can be given as S(p) = C+Sp
Example 1: Sum of three numbers
Algorithm Add(a,b,c)
{
//a,b,c are float type variables return a+b+c;
}
The space required for this algorithm is: Assume a,b,c are occupies 1 word size each, total size
comes to be 3.
b) What is data structure? How data structure varies from data type give examples for
each.