0% found this document useful (0 votes)
25 views15 pages

DS UNIT-I Material

Uploaded by

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

DS UNIT-I Material

Uploaded by

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

UNIT-1

Course: Data Structures


2 Marks Questions & Answers
1. Define Data Structure.
A data structure is a particular way of storing and organizing data in a computer so that it can be
used efficiently.
2. Classify data structures.

3. Write importance of data structure.


 Efficient storage and retrieval of data
 Improved performance
 Better problem solving
 Abstraction
 Reusability
4. What is ADT?
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
values and a set of operations. The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented.
5. Define time and space complexity.
Time complexity: The time complexity of an algorithm is the amount of computing time required
by an algorithm to run its completion.
Space complexity: Space Complexity can be defined as amount of memory (or) space required
by an Algorithm to run.
6. What is searching? List types of searching.
Searching means to find whether a particular value is present in an array or not. If the value is
present in the array, then searching is said to be successful and the searching process gives the
location of that value in an array.
Types of searching:
1. Linear search,
2. Binary search
7. What is sorting? List different types sorting techniques.
Sorting is a technique to rearrange the list of records(elements) either in ascending or descending
order, Sorting is performed according to some key value of each record.
Types of Sorting:
1. Insertion sort
2. Bubble Sort
3. Selection sort
8. What are different operations on data structures?
 Searching
 Inserting
 Deleting
 Sorting
 Merging
 Traversing
9. What is an Algorithm?
Algorithm is step by step logical procedure for solving a problem. In Algorithm each step is
called Instruction. An Algorithm is any well-defined computational procedure that take some
values as inputs and produce some values as output.
10. What is 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.
11. What is 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.
5 Marks Questions & Answers
1. (a) Explain classification of data structures.

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.

push() – Insert an element at one end of the stack called top.


pop() – Remove and return the element at the top of the stack, if it is not empty.
peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
size() – Return the number of elements in the stack.
isEmpty() – Return true if the stack is empty, otherwise return false.
isFull() – Return true if the stack is full, otherwise return false.
2. (a) Explain different operations on queue ADT.
The queue abstract data type (ADT) follows the basic design of the stack abstract data type.
enqueue() – Insert an element at the end of the queue.
dequeue() – Remove and return the first element of the queue, if the queue is not
empty. peek() – Return the element of the queue without removing it, if the queue is not
empty. size () – Return the number of elements in the queue.
is Empty() – Return true if the queue is empty, otherwise return
false. is Full() – Return true if the queue is full, otherwise return
false.

(b) Explain different operations on list ADT.


The data is generally stored in key sequence in a list which has a head structure consisting
of count, pointers and address of compare function needed to compare the data in the list.
The data node contains the pointer to a data structure and a self-referential pointer which
points to the next node in the list.
The List ADT Functions is given below:
get() – Return an element from the list at any given position.
insert() – Insert an element at any position of the list.
remove() – Remove the first occurrence of any element from a non-empty list.
remove At() – Remove the element at a specified location from a non-empty list.
replace() – Replace an element at any position by another element.
size() – Return the number of elements in the list.
Is Empty() – Return true if the list is empty, otherwise return false.
Is Full() – Return true if the list is full, otherwise return false.

3. (a) Explain different operations on data structures.


Traversing: It means to access each data item exactly once so that it can be processed. For
example, to print the names of all the students in a class.
Searching: It is used to find the location of one or more data items that satisfy the given
constraint. Such a data item may or may not be present in the given collection of data items.
For example, to find the names of all the students who secured 100 marks in mathematics.
Inserting: It is used to add new data items to the given list of data items. For example, to add
the details of a new student who has recently joined the course.
Deleting: It means to remove (delete) a particular data item from the given collection of data
items. For example, to delete the name of a student who has left the course.
Sorting: Data items can be arranged in some order like ascending order or descending order
depending on the type of application. For example, arranging the names of students in a class
in an alphabetical order, or calculating the top three winners by arranging the participants’
scores in descending order and then extracting the top three.
Merging: Lists of two sorted data items can be combined to form a single list of sorted data
items.

(b) Demonstrate linear search with an example.


Linear search is a technique which traverses the array sequentially to locate given item or
search element.
In Linear search, we access each element of an array one by one sequentially and see
whether it is desired element or not. We traverse the entire list and match each element of the
list with the item whose location is to be found. If the match found then location of the item
is returned otherwise the algorithm return NULL.
A search is successful then it will return the location of desired element
If A search will unsuccessful if all the elements are accessed and desired element not found.
Linear search is mostly used to search an unordered list in which the items are not sorted.
Linear search is implemented using following steps...
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!" and terminate the
function
Step 4 - If both are not matched, then compare search element with the next element in the
list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!"
and terminate the function.
Example:
Consider the following list of elements and the element to be searched...
4. a) Discuss binary search with an example.
Binary search is the search technique which works efficiently on the sorted lists. Hence, in order
to search an element into some list by using binary search technique, we must ensure that the list
is sorted.
• Binary search follows divide and conquer approach in which, the list is divided into two halves
and the item is compared with the middle element of the list. If the match is found then, the
location of middle element is returned otherwise, we search into either of the halves depend0ing
upon the result produced through the match.
Algorithm:
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and terminate the
function.
Step 5 - If both are not matched, then check whether the search element is smaller or larger than
the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the
left sublist of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the
right sublist of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until sublist
contains only one element.
Step 9 - If that element also doesn't match with the search element, then display "Element is not
found in the list!!!" and terminate the function.
Example:

b) Discuss selection sort with an example.


Given a list of data to be sorted, we simply select the smallest item and place it in a sorted list.
These steps are then repeated until we have sorted all of the data.
 In first step, the smallest element is search in the list, once the smallest element is
found, it is exchanged with the element in the first position.
 Now the list is divided into two parts. One is sorted list other is unsorted list. Find out
the smallest element in the unsorted list and it is exchange with the starting position of
unsorted list, after that it will added in to sorted list.
 This process is repeated until all the elements are sorted.
Algorithm:
SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST(ARR, K, N, Loc)
Step 3: SWAP A[K] with ARR[Loc]
Step 4: EXIT
Algorithm for finding minimum element in the list.
SMALLEST (ARR, K, N, Loc)
Step 1: [INITIALIZE] SET Min = ARR[K] Step 2: [INITIALIZE] SET Loc = K
Step 3: Repeat for J = K+1 to N
IF Min > ARR[J]
SET Min = ARR[J]
SET Loc = J [END OF IF]
[END OF LOOP]
Step 4: RETURN Loc

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.

b) Explain bubble sort with an example


Bubble Sort is also called as Exchange Sort
In Bubble Sort, each element is compared with its adjacent element
After the 1st pass the largest element is placed at (N-1)th location. Given a list of n elements,
the bubble sort requires up to n – 1 passes to sort the data.
Algorithm:
BUBBLE SORT(ARR, N)
Step 1: Read the array elements Step 2: i:=0;
Step 3: Repeat step 4 and step 5 until i<n Step 4: j:=0;
Step 5: Repeat step 6 until j<(n-1)-i Step 6: if A[j] > A[j+1]
Swap(A[j],A[j+1])
End if
End loop 5
End loop 3
Step 7: EXIT
Example:
We take an unsorted array for our example.
Bubble sort starts with very first two elements, comparing them to check which one is greater.

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.

6. a) Develop a C program to arrange the given elements in ascending order.


#include<stdio.h>
main()
{
int n,a[n],i,j;
printf(“enter array size=”);
scanf(“%d”,& n);
printf(“enter array elements=”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(“sorted array=”):
for(i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
}

b) Compare and contrast the linear and binary search algorithm.

Linear Search sequentially checks Binary Search continuously divides the


Definition each element in the list until it finds sorted list, comparing the middle
a match or exhausts the list. element with the target value.
The time complexity is O(n), where
Time The time complexity is O(log n),
n is the number of elements in the
Complexity making it faster for larger datasets.
list.
Less efficient, especially for large More efficient, especially for large
Efficiency
datasets. datasets.
Data Does not require the list to be
Requires the list to be sorted.
Requirement sorted.
Requires a more complex
Implementation Easier to implement.
implementation.
Examines each element Eliminates half of the search space with
Search Space
sequentially. each comparison.
Suitable for small and unsorted
Use Case Ideal for large and sorted datasets.
datasets.

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.

8. a) Explain the terms time and space complexities with example.


The efficiency of an algorithm can be computed by measuring the performance of an algorithm.
We can measure the performance of an algorithm in Two(2) ways.
1. Time Complexity
2. Space Complexity
1. Time Complexity: The time complexity of an algorithm is the amount of computing
time required by an algorithm to run its completion.
Example 1: Sum of the elements in an Array
Statements Step count/ Frequency Total Steps
Execution
Algorithm Addition 0 - 0
(A,n)
{ 0 - 0
//A is an array of 0 - 0
size ‘n’
Sum :=0; 1 1 1
for i:=1 to n do 1 n+1 n+1
Sum:=Sum+A[i]; 1 n n
return Sum; 1 1 1
} 0 - 0
Total 2n+3

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.

Data Type Data Structure


The data type is the form of a variable to which Data structure is a collection of different kinds
a value can be assigned. It defines that the of data. That entire data can be represented
particular variable will assign the values of the using an object and can be used throughout the
given data type only. program.
It can hold value but not data. Therefore, it is It can hold multiple types of data within a
dataless. single object.
The implementation of a data type is known as Data structure implementation is known as
abstract implementation. concrete implementation.
In the case of data types, the value of data is While in the case of data structures, the data
not stored because it only represents the type and its value acquire the space in the
of data that can be stored. computer’s main memory. Also, a data
structure can hold different kinds and types of
data within one single object.
Data type examples are int, float, double, etc. Data structure examples are stack, queue, tree,
etc.

You might also like