ds unit 1 new
ds unit 1 new
Data structure is a method of organizing large amount of data more efficiently so that
any operation on that data becomes easy.
OR
Data Structure is a method of storing and organizing data effectively so that it can be
used efficiently.
Whenever we want to work with large amount of data, then organizing the data is very
important. If the data is not organized effectively, it is very difficult to perform any task
on that data. If the data is organized effectively then any operation can be performed
easily on that data.
Note
Data structures are used to organize the large amount of data.
Every data structure follows a particular principle
Operations on a data structure should not violate the basic principle of that data
structure.
Types of Data Structures
Data structures are classified into 2 types. one is Linear Data Structures and non
linear data structures.
Linear Data Structures
Linear Data Structure is a data structure in which data is organized in sequential order.
Example
1. Arrays
2. Stack
3. Queue
4. Linked List
Non - Linear Data Structures
Non Linear Data Structure is a data structure in which data is organized randomly.
Example
1. Tree
2. Graph
Limitations
Array size should be fixed.
Array elements should be same data type.
Array elements are stored in contiguous memory locations which may not be
always possible.
Array element Insertion and deletion can be problematic because of shifting of
elements from one location to another location.
Stacks:
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 works on LIFO
(last-in first-out) principle. i.e. the last element which is added to the stack is the
first element which is deleted from the stack. Stacks can be implemented using
arrays or linked lists. Every stack has a variable top associated with it. Top is used
to store the index value of the topmost element of the stack. It is the position
from where the element will be added or deleted. There is another variable SIZE,
which is used to store the size of stack. If top == -1 then the stack is empty and if
top == SIZE–1 then the stack is full. A stack supports three basic operations: push,
pop, and peep. Push operation adds an element to the top of the stack. Pop
operation removes the element from the top of the stack. Peep operation returns
the value of the topmost element of the stack.
Linked Lists:
Linked list is a linear data structure in which elements are called nodes, those are
connected by links. Every node contains the data of the node and a pointer which
stores address of the next node. Every node in the list connects to the next node.
First node of linked list is called Head node. if Head==NULL indicates Linked list is
empty. The operations performed on a linked list include: Insertion: Adding a new
node to the list at a specific position. Deletion: Removing an existing node from
the list at a specific position. Traversal: Accessing each element in linked list.
Trees:
A tree is a non-linear data structure which contains of a group of nodes arranged
in a hierarchical order. First node of tree is called root node. Tree contains of a
root node and left sub tree and right sub-tree. Each node contains a data
element, a left pointer which points to the left sub-tree, and a right pointer
which points to the right sub-tree. If root == NULL then the tree is empty. The
operations performed on a tree include: Insertion: Adding a new node into the
tree. Deletion: Removing an existing node from tree. Traversal: we can display
elements of tree in inorder form or preorder form or postorder form.
Importance of Data Structures (or) Need of Data Structures (or) Advantages of Data
Structures
Linear Data structures are a fundamental concept in computer science and play a crucial
role in many aspects of software development. Here are some of the reasons why linear
data structures are important:
1. Effective storage and retrieval of data:
Data structures allow you to store and retrieve data in an efficient manner. For
example, using a hash table data structure, you can access an element in constant
time, whereas searching for an element in an unordered list would take linear time.
An abstract data type is defined in term of its data items and its
associated operations rather than its implementation.
Or
An abstract data type consists of data as well as the operations to be
performed on the data and implementation will be hidden.
List ADT, Stacks ADT and queues ADT. The user focuses only about the data and
its operations.
We can implement Stack ADT and Queue ADT using an array or a linked list.
Advantages of ADT:
1. It helps to develop the program efficiently.
2. Facilitates to break down a complex task into simple subtasks.
3. Helps to reduce the number of things to remember at any time.
4. Simplifies testing and debugging of complex applications.
2. Space Complexity:
Space Complexity can be defined as amount of memory (or) space required by an
Algorithm to run.
To compute the space complexity we use 2 factors
i. Constant
ii. Instructions.
Searching
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.
Algorithm
Step 1 - Read the search element from the user.
Step 2 – Set i=0
Step 3 – Repeat steps 4 to 6 while i<N
Step 4 - Compare the search element with A[i].
Step 5 - If both are matched, then display "Given element is found" and
terminate the function
Step 6 - If both are not matched, then set i=i+1.
Step 7 – If i==N then display "Element is not found" and terminate the function.
Example:
Given list of elements are 10, 50, 30, 20, 5, 12 and Search element is 5.
Step 1:
Compare search element (5) with first element (10)
Algorithm:
Step 1 - Read the search element from the user.
Step 2 – Set First=0, Last=N-1
Step 3 – Repeat Step 4 to while First<=Last
Step 4 - Find mid=(First+Last)/2.
Step 5 - Compare the search element with the middle element.
Step 6 - If both are matched, then display "Given element is found" and terminate
the function.
Step 7 - If the search element is smaller than middle element then set Last=mid-1
and goto Step 3.
Step 8 - If the search element is greater than middle element then set
First=mid+1 and goto Step 3.
Step 9 - If First>Last then display "Element is not found in the list" and terminate
the function.
Example 1: Given list of elements are 10,12,20,32,50,55,65,80,99 and search
element is 12.
Sorting
From given a list of elements, first we find first smallest element and swap it with
first element. Then we find second smallest element and swap it with second
element. These steps are then repeated until we sort all the elements.
Algorithm :
SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for I = 0 to N-1
Step 2: find the position[POSN] of smallest element.
Step 3: SWAP ARR[I] with ARR[POSN]
Step 4: EXIT
Example :
Best-case: O(n2), best case occurs when the array is already sorted. (where n is
the number of integers in an array)
Average-case: O(n2), the average case arises when the elements of the array are
in a disordered or random order, without a clear ascending or descending
pattern.
Worst-case: O(n2), The worst-case scenario arises when we need to sort an array
in ascending order, but the array is initially in descending order.
C Program to perform Selection Sort
#include<stdio.h>
void main()
{
int a[100],n,i,j,t,posn;
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for( i=0;i<n;i++ )
scanf("%d",&a[i]);
printf("Array before sort: ");
for( i=0;i<n;i++ )
printf("%d ",a[i]);
printf("\n");
for( i=0;i<n;i++ )
{
posn=i;
for( j=i+1;j<n;j++)
{
if( a[j] < a[posn] )
posn=j;
}
t=a[i];
a[i]=a[posn];
a[posn]=t;
}
printf("Array after sort: ");
for( i=0;i<n;i++ )
printf("%d ",a[i]);
printf("\n");
}
Bubble Sort
First Pass
Compare 13 and 32.
Second Pass
Compare 13 and 26.
Third Pass
compare 13 and 26.
Fourth Pass
Compare 13 and 10
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:=N-1;
Step 5: Repeat step 6 until j>=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
Best Case Time Complexity: O(N)
The best case occurs when the array is already sorted. So the number of comparisons
required is N-1 and the number of swaps required = 0. Hence the best case complexity
is O(N).
Worst Case Time Complexity: O(N2)
The worst-case condition for bubble sort occurs when elements of the array are
arranged in decreasing order. In the worst case, the total number of iterations or passes
required to sort a given array is (N-1). Here ‘N’ is the number of elements present in the
array. Hence the worst case time complexity is O(N2)
Quick Sort uses divide and conquer technique. Quick sort is a highly
efficient sorting algorithm and is based on partitioning an array into smaller sub
arrays. A large array is partitioned into two half arrays. First array holds smaller than
the pivot. Second array holds higher than pivot.
Quick sort partitions an array and then calls itself recursively twice to sort the two
resulting sub arrays. This algorithm is quite efficient for large-sized data sets as its
average and worst case complexity are of Ο(n2), where n is the number of items.
Program
#include<stdio.h>
MERGE SORT
Merge sort is a sorting technique based on divide and conquer technique. With
the worst-case time complexity being Ο(n log n), it is one of the most respected
algorithms.
An array is a variable which can store multiple values of same data type.
Operations of Array
Following operations can be performed on arrays:
1. Insertion
2. Deletion
3. Traversing
4. Searching
5. Sorting
Insertion
Insertion is a process of inserting one or more elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given position.
INSERT( A,N,P,ITEM )
// This algorithm inserts an element into the Array.
// A is name of array.
// N is size of array
// P is position where to be inserted.
// ITEM is the value to be inserted.
Step1: set J=N
Step2: Repeat Step 3 and 4 while J>=P
Step3: A[J]=A[J-1];
Step4: J=J-1
Step5: A[P]=ITEM
Step6: N=N+1
DELETE( A,N,P)
// This algorithm deletes an element from the Array.
// A is name of array.
// N is size of array
// P is the position of element to be deleted.
Step1: set J=P
Step2: Repeat Step 3 and 4 while J<N
Step3: A[J-1]=A[J];
Step4: J=J+1
Step5: N=N-1
Traverse
It is a process of displaying all elements of array.
DISPLAY( A,N )
// This algorithm displays all element of Array.
// A is name of array.
// N is size of array
Step1: set I=0
Step2: Repeat Step 3 and 4 while I<N
Step3: write( A[I] )
Step4: I=I+1
Searching
It is a process of checking whether an element is present in array or not.
SEARCH( A,N,ITEM )
// This algorithm searches an element of Array.
// A is an array name.
// N is the array Size
// ITEM is the element to be searched.
Step1: set I=0
Step2: Repeat Step 3 and 4 while I<N
Step3: if A[I]==ITEM then
write( ITEM is found at I position )
break
Step4: I=I+1
Step5: if I==N then
write( ITEM not found )
Sorting( A,N)
This algorithm arranges all elements of Array in
order.
A is an array name.
N is the array Size
Step1: set I=0
Step2: Repeat Step 3 to 7 while I<N
Step3: Set J=I+1
Step4: Repeat Step 5 to 6 while J<N
Step5: If A[I] > A[J] then
Swap A[I] and A[J]
Step6: J=J+1
Step7: I=I+1