DS UNIT-1 NOTES
DS UNIT-1 NOTES
1.1.DATA STRUCTURES:
Data may be organized in many different ways logical or mathematical model of a program
particularly organization of data. This organized data is called “Data Structure”.
Or
The organized collection of data is called a ‘Data Structure’.
LINKED LIST:
A linked list is a way to store a collection of elements. Each element in a linked list is stored in
the form of a node. A data part stores the element and a next part stores the link to the next
node.
ARRAY:
An array is a linear data structure that collects elements of the same data type and stores them in
contiguous and adjacent memory locations.
1. List ADT
Views of list
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.
2. Stack ADT
View of stack
In Stack ADT Implementation instead of data being stored in each node, the
pointer to data is stored.
The program allocates memory for the data and address is passed to the stack
ADT.
The head node and the data nodes are encapsulated in the ADT. The calling
function can only see the pointer to the stack.
The stack head structure also contains a pointer to top and count of number of
entries currently in stack.
.
3. Queue ADT
View of Queue
The queue abstract data type (ADT) follows the basic design of the stack abstract
data type.
Each node contains a void pointer to the data and the link pointer to the next
element in the queue. The program’s responsibility is to allocate memory for
storing the data.
.
1.3.OVERVIEW OF TIME AND SPACE COMPLEXITY :
Analyzing an algorithm means determining the amount of resources (such as time and
memory) needed to execute it. Algorithms are generally designed to work with an arbitrary
number of inputs, so the efficiency or complexity of an algorithm is stated in terms of time
and space complexity.
The time complexity of an algorithm is basically the running time of a program as a function
of the input size. Similarly, the space complexity of an algorithm is the amount of computer
memory that is required during the program execution as a function of the input size.
In other words, the number of machine instructions which a program executes is called its
time complexity. This number is primarily dependent on the size of the program’s input and
the algorithm used.
Time Complexity:
The amount of time required for an algorithm to complete its execution is its time
complexity. An algorithm is said to be efficient if it takes the minimum (reasonable) amount
of time to complete its execution.
The number of steps any problem statement is assigned depends on the kind of
statement.
For example, comments 0 steps.
1. We introduce a variable, count into the program statement to increment count with initial
value 0.Statement to increment count by the appropriate amount are introduced into the
program.
This is done so that each time a statement in the original program is executes count is incremented
by the step count of that statement.
Algorithm:
Algorithm sum(a,n)
{
s= 0.0;
count = count+1;
for I=1 to n do 8
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}
If the count is zero to start with, then it will be 2n+3 on termination. So each invocation of
sum execute a total of 2n+3 steps.
2. The second method to determine the step count of an algorithm is to build a table in which
we list the total number of steps contributes by each statement.
First determine the number of steps per execution (s/e) of the statement and the total number
of times (ie., frequency) each statement is executed.
By combining these two quantities, the total contribution of all statements, the step count for the
entire algorithm is obtained.
Statement S/e Frequency Total
1. Algorithm 0 - 0
Sum(a,n) 0 - 0
2.{ 1 1 1
3. S=0.0; 1 n+1 n+1
4. for I=1 to n do 1 n n
5. s=s+a[I]; 1 1 1
6. return s; 0 - 0
7. }
Total 2n+3
Space Complexity:
The amount of space occupied by an algorithm is known as Space Complexity. An
algorithm is said to be efficient if it occupies less space and required the minimum amount
of time to complete its execution.
Fixed part:
It varies from problem to problem. It includes the space needed for storing
instructions, constants, variables, and structured variables (like arrays and structures).
Variable part:
It varies from program to program. It includes the space needed for recursion stack,
and for structured variables that are allocated space dynamically during the runtime of a
program.
The space requirement s(p) of any algorithm p may therefore be written as,
S(P) = c+ Sp(Instance characteristics)
Where ‘c’ is a constant.
1.4. Searching:
Searching is a process of finding a particular record, which can be a single
element or a small chunk, within a huge amount of data. The data can be in
various forms: arrays, linked lists, trees, heaps, and graphs etc. With the
increasing amount of data nowadays, there are multiple techniques to perform the
searching operation.is a process of finding a particular record, which can be a
single element or a small chunk, within a huge amount of data. The data can be in
various forms: arrays, linked lists, trees, heaps, and graphs etc. With the
increasing amount of data nowadays, there are multiple techniques to perform the
searching operation.
For instance, in the given animated diagram, we are searching for an element 33.
Therefore, the linear search method searches for it sequentially from the very first
element until it finds a match. This returns a successful search.
In the same diagram, if we have to search for an element 46, then it returns an
unsuccessful search since 46 is not present in the input.
Pseudocode
procedure linear_search (list, value)
end if
end for
end procedure
Analysis
Linear search traverses through every element sequentially therefore, the best
case is when the element is found in the very first iteration. The best-case time
complexity would be O(1).
However, the worst case of the linear search method would be an unsuccessful
search that does not find the key value in the array, it performs n iterations.
Therefore, the worst-case time complexity of the linear search algorithm would
be O(n).
Example
Let us look at the step-by-step searching of the key element (say 47) in an array
using the linear search method.
Step 1 : The linear search starts from the 0th index. Compare the key element
with the value in the 0th index, 34.
Step 2 : Now, the key is compared with value in the 1st index of the array.
Step 3 : The next element 66 is compared with 47. They are both not a match so
the algorithm compares the further elements.
Step 4 : Now the element in 3rd index, 27, is compared with the key value, 47.
They are not equal so the algorithm is pushed forward to check the next element.
Step 5 : Comparing the element in the 4th index of the array, 47, to the key 47. It
is figured that both the elements match. Now, the position in which 47 is present,
i.e., 4 is returned.
Implementation
The Linear Search program can be seen implemented in four programming
languages. The function compares the elements of input with the key value and
returns the position of the key in the array or an unsuccessful search prompt if the
key is not present in the array.
int i, count = 0;
for(i = 0; i < n; i++) {
count = count + 1;
int main(){
int i, n, key;
n = 6;
key = 18;
linear_search(a, n, key);
key = 23;
linear_search(a, n, key);
return 0;
Output:
Binary search is a fast search algorithm with run-time complexity of Ο(log n).
This search algorithm works on the principle of divide and conquer, since it
divides the array into half before searching. For this algorithm to work properly,
the data collection should be in the sorted form.
Binary search looks for a particular key value by comparing the middle most item
of the collection. If a match occurs, then the index of item is returned. But if the
middle item has a value greater than the key value, the right sub-array of the
middle item is searched. Otherwise, the left sub-array is searched. This process
continues recursively until the size of a subarray reduces to zero.
Key<A[MID]
i.e. 35<46.
So search continues at lower half of the array.
Ub=MID-1
=5-1 = 4.
Step 2: MID= [lb+ub]/2
=(1+4)/2
=2.
Key>A[MID]
i.e. 35>12.
So search continues at Upper Half of the array.
Lb=MID+1
=2+1
= 3. 50
Step 3: MID= [lb+ub]/2
=(3+4)/2
=3.
Key>A[MID]
i.e. 35>30.
So search continues at Upper Half of the array.
Lb=MID+1
=3+1
= 4.
ALGORITHM:
BINARY SEARCH[A,N,KEY]
Step 1: begin
Step 2: [Initilization]
Lb=1; ub=n;
Step 3: [Search for the ITEM]
Repeat through step 4,while Lower bound is less than Upper Bound.
Step 4: [Obtain the index of middle value]
MID=(lb+ub)/2
Step 5: [Compare to search for ITEM]
If Key<A[MID] then
Ub=MID-1
Other wise if Key >A[MID] then
Lb=MID+1
Otherwise write “Match Found”
Return Middle.
Step 6: [Unsuccessful Search]
write “Match Not Found”
Step 7: Stop.
Implementation:
Binary search is a fast search algorithm with run-time complexity of Ο(log n).
This search algorithm works on the principle of divide and conquer. For this
algorithm to work properly, the data collection should be in a sorted form.
int mid;
if (a[mid] == key)
printf("Unsuccessful Search\n");
int main(){
n = 5;
low = 0;
high = n-1;
key = 22;
key = 23;
return 0;
Output
Element found at index: 3
Unsuccessful Search
Advantages: When the number of elements in the list is large, Binary Search executed faster
than linear search. Hence this method is efficient when number of elements is large.
Disadvantages: To implement Binary Search method the elements in the list must be in sorted
order, otherwise it fails.
1.5. SORTING
INTRODUCTION
Sorting is a technique of organizing the data. It is a process of arranging the records, either in
ascending or descending order i.e. bringing some order lines in the data. Sort methods are very
important in Data structures.
Sorting can be performed on any one or combination of one or more attributes present in each
record. It is very easy and efficient to perform searching, if data is stored in sorting order. The
sorting is performed according to the key value of each record. Depending up on the makeup of
key, records can be stored either numerically or alphanumerically. In numerical sorting, the
records arranged in ascending or descending order according to the numeric value of the key.
Example1:
Example 2:
To discuss bubble sort in detail, let us consider an arrayA[]that has the
followingelements:
A[] = {30, 52, 29, 87, 63, 27, 19, 54}
Pass 1:
Compare 30 and 52. Since 30 < 52, no swapping is done.
Compare 52 and 29. Since 52 > 29, swapping is
done. 30, 29, 52, 87, 63, 27, 19, 54
Compare 52 and 87. Since 52 < 87, no swapping is done.
Compare 87 and 63. Since 87 > 63, swapping is
done. 30, 29, 52, 63, 87, 27, 19, 54
Compare 87 and 27. Since 87 > 27, swapping is
done. 30, 29, 52, 63, 27, 87, 19, 54
Compare 87 and 19. Since 87 > 19, swapping is
done. 30, 29, 52, 63, 27, 19, 87, 54
Compare 87 and 54. Since 87 > 54, swapping is
done. 30, 29, 52, 63, 27, 19, 54, 87
Observe that after the end of the first pass, the largest element is placed at the
highest index of the array. All the other elements are still unsorted.
Pass 2:
Compare 30 and 29. Since 30 > 29, swapping is
done. 29, 30, 52, 63, 27, 19, 54, 87
Compare 30 and 52. Since 30 < 52, no swapping is done.
Compare 52 and 63. Since 52 < 63, no swapping is done.
Compare 63 and 27. Since 63 > 27, swapping is
done. 29, 30, 52, 27, 63, 19, 54, 87
Compare 63 and 19. Since 63 > 19, swapping is done.
29, 30, 52, 27, 19, 63, 54, 87
Compare 63 and 54. Since 63 > 54, swapping
is done. 29, 30, 52, 27, 19, 54, 63, 87
Observe that after the end of the second pass, the second largest element is
placed at the second highest index of the array. All the other elements are still
unsorted.
Pass 3:
Compare 29 and 30. Since 29 < 30, no swapping is done.
Compare 30 and 52. Since 30 < 52, no swapping is done.
Compare 52 and 27. Since 52 > 27, swapping
is done. 29, 30, 27, 52, 19, 54, 63, 87
Compare 52 and 19. Since 52 > 19, swapping
is done. 29, 30, 27, 19, 52, 54, 63, 87
Compare 52 and 54. Since 52 < 54, no swapping is done.
Observe that after the end of the third pass, the third largest element is placed at
the third highest index of the array. All the other elements are still unsorted.
Pass 4:
Compare 29 and 30. Since 29 < 30, no swapping is done.
Compare 30 and 27. Since 30 > 27, swapping
is done. 29, 27, 30, 19, 52, 54, 63, 87
Compare 30 and 19. Since 30 > 19, swapping
is done. 29, 27, 19, 30, 52, 54, 63, 87
Compare 30 and 52. Since 30 < 52, no swapping is done.
Observe that after the end of the fourth pass, the fourth largest element is placed at
the fourth highest index of the array. All the other elements are still unsorted.
Pass 5:
Compare 29 and 27. Since 29 > 27, swapping
is done. 27, 29, 19, 30, 52, 54, 63, 87
Compare 29 and 19. Since 29 > 19, swapping
is done. 27, 19, 29, 30, 52, 54, 63, 87
Compare 29 and 30. Since 29 < 30, no swapping is done.
Observe that after the end of the fifth pass, the fifth largest element is placed at
the fifth highest index of the array. All the other elements are still unsorted.
Pass 6:
Compare 27 and 19. Since 27 > 19, swapping
is done. 19, 27, 29, 30, 52, 54, 63, 87
Compare 27 and 29. Since 27 < 29, no swapping is done.
Observe that after the end of the sixth pass, the sixth largest element is placed at
the sixth largest index of the array. All the other elements are still unsorted.
Pass 7:
(a) Compare 19 and 27. Since 19 < 27, no swapping is done.
Observe that the entire list is sorted now.
Advantages :
Simple and easy to implement
In this sort, elements are swapped in place without using additional temporary
storage, so the space requirement is at a minimum.
Disadvantages :
It is slowest method . O(n2)
Inefficient for large sorting lists.
Program
#include<stdio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}
Output:
Printing Sorted Element List . . .
7
9
10
12
23
34
34
44
78
101
1.5.2. SELECTION SORT
In selection sort, the smallest value among the unsorted elements of the array is selected in
every pass and inserted to its appropriate position into the array. First, find the smallest element
of the array and place it on the first position. Then, find the second smallest element of the array
and place it on the second position. The process continues until we get the sorted array. The
array with n elements is sorted by using n-1 pass of selection sort algorithm.
Algorithm for selection sort
SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT
SMALLEST (ARR, K, N, POS)
Step 1: [INITIALIZE] SET SMALL = ARR[K]
Step 2: [INITIALIZE] SET POS = K
Step 3: Repeat for J = K+1 to N
IF SMALL > ARR[J]
SET SMALL = ARR[J]
SET POS = J
[END OF IF]
[END OF LOOP]
Step 4: RETURN POS
Example 1: 3, 6, 1, 8, 4, 5
Example2 :
Example: Consider the following array with 6 elements. Sort the elements of the array by using
selection sort.
A = {10, 2, 3, 90, 43, 56}.
Advantages:
It is simple and easy to implement.
It can be used for small data sets.
It is 60 per cent more efficient than bubble sort.
Disadvantages:
Running time of Selection sort algorithm is very poor of 0 (n2).
However, in case of large data sets, the efficiency of selection sort drops as
compared to insertion sort.
Program
#include<stdio.h>
int smallest(int[],int,int);
void main ()
{
int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i,j,k,pos,temp;
for(i=0;i<10;i++)
{
pos = smallest(a,10,i);
temp = a[i];
a[i]=a[pos];
a[pos] = temp;
}
printf("\nprinting sorted elements...\n");
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}
int smallest(int a[], int n, int i)
{
int small,pos,j;
small = a[i];
pos = i;
for(j=i+1;j<10;j++)
{
if(a[j]<small)
{
small = a[j];
pos=j;
}
}
return pos;
}
Output:
printing sorted elements...
7
9
10
12
23
23
34
44
78
101
INSERTION SORT
Insertion sort is one of the best sorting techniques. It is twice as fast as Bubble sort. In Insertion
sort the elements comparisons are as less as compared to bubble sort. In this comparison the
value until all prior elements are less than the compared values is not found. This means that all
the previous values are lesser than compared value. Insertion sort is good choice for small
values and for nearly sorted values.
Working of Insertion sort:
The Insertion sort algorithm selects each element and inserts it at its proper position in a sub list
sorted earlier. In a first pass the elements A1 is compared with A0 and if A[1] and A[0] are not
sorted they are swapped.
In the second pass the element[2] is compared with A[0] and A[1]. And it is inserted at its
proper position in the sorted sub list containing the elements A[0] and A[1]. Similarly doing i th
iteration the element A[i] is placed at its proper position in the sorted sub list, containing the
elements A[0],A[1],A[2],…………A[i-1].
To understand the insertion sort consider the unsorted Array A={7,33,20,11,6}.
Algorithm for insertion sort
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
Example 1:
Consider an array of integers given below. We will sort the values in the
Array using insertion sort
23 15 29 11 1
Example2:
The steps to sort the values stored in the array in ascending order using Insertion sort are given
below:
7 33 20 11 6
7 20 33 11 6
Step 4: Then the fourth element 11 is compared with its previous elements. Since 11 is less than
33 and 20 ; and greater than 7. So it is placed in between 7 and 20. For this the elements 20 and
33 are shifted one position towards the right.
7 20 33 11 6
7 11 20 33 6
Step5: Finally the last element 6 is compared with all the elements preceding it. Since it is
smaller than all other elements, so they are shifted one position towards right and 6 is inserted at
the first position in the array. After this pass, the Array is sorted.
7 11 20 33 6
6 7 11 20 33
Step 6: Finally the sorted Array is as follows:
6 7 11 20 33
Disadvantages:-
It is less efficient on list containing more number of elements.
As the number of elements increases the performance of program would be slow .