Data Structure
Data Structure
Structure
Syllabus:
Data Structure
Why ?
Want to store : 1-5
If store: {4,1,2,3,5} not useful
If store: { 1,2,3,4,5}
{5,4,3,2,1} is useful
Point To Remember:
How to store Data ?
What are the different methods use
to store this DS?
Data Structure Divided into
following Categories
Primitive :
Int,float,char,Double,Pointer
If any keywords founds in datatype
is called primitive DS (Predefine
reserved words)
Operation Performed in DS
1) Searching
2) Sorting
3) Inserting 12345
4) Deletion 1 345
5) Updating 1345
6) Traverse
LDS:
A DS is called Linear if all of its
elements are arranged in the linear
order.
1-2-3-4-
5.1====2===3===4====5
NLDS:
This DS does not form a sequence
i.e. each item or element is
connected with two or more items
in non-linear arrangement.
Multivalued Cycle
Adv:
1) Efficient
2) Reusability
Day 2
Chap 2 Linear Data Structure
Array:
An array is finite ordered collection
of homogeneous data element
which provide random access to
the element .
Int a[5];
Int ---------data type
a------------array name
5 ----------------size
a
0 1 2
3 4
10 20 30 40 50
Application Of Array
Stored list of value
Matrix operation
Search algorithm
Sorting algorithm
Data structure
Cpu scheduling
Index 0 1 2
5.2 3.2 6.1
Address 200 300 400
Int==========2 or 4 bytes
Float ============ 4 bytes
Char =============1 byte
Day 3
Type of array
1) One dimensional
2) Multidimentional
a)two Dimensional
b) n- Dimensional
One DA
A1
2byt
es
A2
-
-
-
An
Q1 : if int a[5] is an array of 5
elements. Each integer is of 2 bytes
and base address is 100,then find
the address of 3rd element.
0 1 2
3 4
1stele 2ndele ?3 ele 4 ele 5 ele
100 ?
Formal:
Address ith element=Base address
+ (offset of the ith element from
the base address)
A[i]=base address+(offset x size of
datatype)
A[2]= 100+(2x2)
=100+4
=104
4th element
A[3]=100+(3x2)
=100+6
=106
5th element
A[]= ? 108
Two DA
Int A[i][j]
1) i========row
2) j========col
Num[2][1]=?
1018
H.W
A[2][3]=101+[(2 x 3+3) x 2]
=101 +[(6+3) x2]
=101 + [9 x 2]
=101 + 18
=119
11 july 2024
Sorting :
Sorting refers to the operation of
arranging data in some given
sequence such as ascending order
or descending order .
For eg: 30 36 50 49 22
AO: 22 30 36 49 50
DO: 50 49 36 30 22
Sorting stability
Ex:
A Ab O s p R
75 90 80 85 85 82
ss
Ab S P r o a
90 85 85 82 80 75
Us_s
Ab P S r o a
90 85 85 82 80 75
Sorting Techniques
Two main categories
1) Internal sort: use primary
memories (main memories)
Ex: Bubble sort, insertion sort,
Quick sort
2) External sort : use secondary
memories(Hard disk,magnetic
tape)
Ex: merge sort
Sorting Methods :
1. Bubble sort
2. Insertion sort
3. Merge sort
4. Quick sort
5. Selection sort
Swapping
A=1 B=2
Swapping: A=2 B=1
Temp=A temp= 1 a=?
B=2 temp=1
A=BA=2 A=2 B=?
temp=1
B=TempB= 1 A=2 B=1
Swapping
Bubble Sort
In Bubble Sort ,each element is
compared its adjacent element.if
the first element is larger thanthe
second one then the position of the
element are
interchanged ,otherwise it is no
changed.
13 11 14 15 19 9
Sort using Bubble sort
Process:
Pass 1:
13 11 14 15 19 9 13 > 11
true
11 13 14 15 19 9 13 >
14 false
11 13 14 15 19 9 14>15
false
11 13 14 15 19 9
15>19 flase
11 13 14 15 19 9 19>9
true
11 13 14 15 9 19
Pass 2:
11 13 14 15 9 19
11 13 14 15 9 19
11 13 14 15 9 19
11 13 14 15 9 19
11 13 14 9 15 19
11 13 14 9 15 19
Pass 3:
11 13 14 9 15 19
11 13 14 9 15 19
11 13 14 9 15 19
11 13 9 14 15 19
11 13 9 14 15 19
11 13 9 14 15 19
Pass 4:
11 13 9 14 15 19
11 13 9 14 15 19
11 9 13 14 15 19
11 9 13 14 15 19
11 9 13 14 15 19
11 9 13 14 15 19
Pass 5:
11 9 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
9 11 13 14 15 19
======= sorted array
Algorithm: (arranging the elements
in ascending order)
1) Read N,number of elements in
the list.
2) Read array a[0],a[1],------a[n-1]
3) For i=0 to n-1
For j=0 to n-i-1
If (a[j] >a[j+1]) then
Temp= a[j]
A[j]=a[j+1]
A[j+1]=Temp
4) Stop
1) N=5
8 1 7 2 5
3) i=0 to N-1
j=0 to n-i-1( 5-0-1)
if(a[j]0 >a[j+1]
1]======a[0] >a[1]
8>1
a[j]=a a[j+1]= b
Temp=a[j]
Temp=a
A[j]=a[j+1]
A=b
A[j+1]=temp
B=temp
===================
===========
13 july 2024
Q1:Write a program for Bubble
sort using C
Return(a+b);
}
Call by value
Here values of actual parameter
will be copied to formal parameters
and these two different store value
in different location.
x y
xy 2 1
0 0
10 20
Call of reference
}
Output:
X=20 , y=10
P1 p2
100 200
0 0
X y
20 10
#include<stdio.h>
Int main(void)
{
Int a[ ]={1,3,7,9,0,2,4,5,8,6};
Int length=10;
For(int i=0;i<length;i++)
{
For(j=0; j<(length-1);j++)
{
If(a[j]>a[j+1])
{
Temp= a[j];
A[j]=a[j+1];
A[j+1]=temp;
}
}
}
For(int i=0;i<length;i++)
Printf(“a[%d]=%d \n”,I,a[i]);
Return 0;
}
15 th july 2024
Insertion sort
Insertion sort arrange N elements
of array by inserting particular item
in a particular place such a way
that the item are in sorted order.
16 15 4 13 2 1
A[0] A[1] A[2] A[3]
A[4] A[5]
Max= 6
15<16 true
15 16 4 13 2 1
A[0] A[1] A[2] A[3]
A[4] A[5]
4< 15 true
4 15 16 13 2 1
A[0] A[1] A[2] A[3]
A[4] A[5]
13<4 false
13 <15 true (shift)
4 13 15 16 2 1
A[4]
2<4 true (shift)
2 4 13 15 16 1
A[5]
1< 2 true (shift)
1 2 4 13 15 16
Algorithm
Step 1: set i=1 set i=1
Step2: key= A[i] key=A[1]
key=15
Step3: set j=i-1 set j=i-1
j=1-1=0
Step4:if key< A[j] if
15<A[0]16
A[j+1]=A[j];
A[0+1]=A[0]A[1]=16
J=j-1;j=0-1=-1
Repeat step 2 until j>= 0
Step 5:A[j+1]=key; A[-
1+1]=15 A[0]=15
i=i+1;i=1+1=2
Step 6: Repeat step from 2 to 5 till
i<N
Step 7:stop
For(i=1;i<n;i++)
Insertion sort: {
Temp=a[i];
Sorted sub list J=i-1;
and unsorted While(j>=0 &&
sub list a[j]>temp)
Sorted unsorted
{
5 4 1 1 6 2
A[j+1]=a[j];
0
0 1 2 3 4
J--;
5 }
N=6 A[j+1]=temp;
4 5 10 1 6 2 }
Temp=4
4 5 10 1 6 2
0 1 2 3
4 5 Temp=10
1 4 5 10 6 2
0 1 2 3
4 5
Temp=1
1 4 5 6 10 2
Tem=6
1 2 4 5 6 10
Temp=2
#include<stdio.h>
Void main()
{
Int i , j,temp,a[10],n;
Printf(\n enter the number of
elements”);
Scanf(“%d”,&n);
Printf(“\n enter n element);
For(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
Insertion_sort(n,a);
Print(\n the sorted element are:”)
For(i=0;i<n;i++)
{
Printf(“%d”,a[i]);
}
}
18 july 2024
Quick sort:
ke
y
=pivot(any element from array,it
can be first element,last element or
random element
n
Partition piv Partition 2(right)
1(left) ot
Values <pivotpivot<value
Small
large
Quick sort:
0 1 2 3 4 5 6 Partition:
7 8 9 1) i=low
2 4 3 9 1 4 8 7 5 6 2) j=hight
3) i++ until
Pivot
L S
2 4 3 9 1 4 8 7 5 6 element >
Pivotijj
23 july 2024
42 84 20 75 10
60 5 30
20 42 75 84 5
10 30 60
5 10 20 30 42 60 75 84
4,2,6,1,8,3
1,8,3
4,2,6
4,2 6 1,8
3
4 2 1 8
2 4 6 18
3
246 138
123468
25 July 2024
Searching
Searching is a process to finding an
element within the list of an
element stores in any order.
Searching
Linear Search:
2 3 5 1 6 7
6
Binary Search:
1 2 3 5 6 7
Pass 1pass2
Mid = 0+5/2=2.5
Linear Search :
We access each element of an
array/list one by one sequentially
and see whether it is desired
element or not.
2 4 6 3 1 5
0 1 2 3
4 5
Find: x=3, given n=6
Binary Search :
This search technique the given
item in minimum possible
comparison.In this searching first
we had to sort array element then
following operation perform
1 first find the middle element of
the array.
2compare the mid element with
an item.
3There are three cases
a) if it is desired element then
search is
complete .
b) if its less than then desired
item then search only the
first half of th array.
c) If its greater than the desired
item then search in the
second half of the array.
Pass-1Midpass-2
2 3 4 5 6 7
0 1 2 3
4 5
Mid=[(0+5)/2]=5/2=[2.5]=2
Search=6
Algorithm: 2 3 5 8 9
1) Set beg=LB and 0 1 23
end=UP and 4
mid=int(beg+en
Beg=0 end=4 mid=
d/2)
2) Repeat step 3 0+4/2=2
and 4 while 0<=4 and a[2]!=85!
beg<=end and =8 true
a[mid]!=item 8<5 false
3) If item<a[mid] Else
then
Set end=mid-1 Beg=2+1=3
Else End=4
Set beg=mid+1
Mid=int
4) set
mid=int(beg+en (3+4/2)=7/2=int(3.5)
d/2) =3
Goto step 2 A[mid]!=item
5) If a[mid]=item
A[3]!=8
then
Set loc=mid 8!=8 flase
Else 88
Set loc=null
6)exit
Date :30/07/24
Chap 3: Linked List
Def:
A linked list or one-way list is a
linear collection of data elements
call nodes.Each node contain two
parts
1) Data
2) Successor
Array/list
1 3 6 8
Link list
1
3
Characteristics
1) Place element anywhere in the
memory
2) Dynamic allocation
3) Insertion and deletion od data
not required any data element ,
4) Each element is a collection of
data and successor.
5) Linked needs pointers and
dynamic memory allocation.
6) Required Head node .
7) Two field
Data field and pointer field.
8) Data field consist of information.
9) The link list must always have at
least one pointer pointing to first
node call head
10) Head=null
11) Pointer of the last node node of
the linked list contain a value null.
12) Every node must contain at least
one link field.
Primitive operation
a) Create
b) Traverse.
c) Insert node
d) Delete node
e) Searching a node.
f) Update a node.
g) Counting length.
h) Reverse the linked list.
i) Sort the linked list
j) Concatenate two linked list.
k) Merge two sorted linked list
31/07/24
Types of Linked list
1) Single linked list: Navigation
forwards only
2) Doubly: forwards and backward
navigation
3) Circular linked list: last element is
linked to the first element.
Data Link
Node
Representation SLL
Head
10
0
23 20 54 30 78 40 9 NUL
0 0 0 0 L
100 200 300
400
Node Representation in C
Node & link/successor/next
Data Link
Exam: Syntax:
Struct node Struct node
{ {
Int data; Data_type member1;
Struct node *link; Data_type member2;
}: .
.
.
.
.
Struct node *link;
}
Printf(“%d”,headdata);
Return 0;
}
Output
45
Struct node
{
Int data;
Struct node *link;
};
Method 1
Int main()
{
Struct node *head=malloc(size of(struct node));
Headdata=45;
Headlink=null;
Struct node *current=malloc(size of(struct node));
currentdata=98;
currentlink=null;
Headlink=current;
Struct node *current1=malloc(size of(struct node));
Current1data=3;
Currentlink=null;
Currentlink=current1;
Return 0;
}
Method 2
Method -1
Method-2
#include <stdio.h>
#include<stdlib.h>
Struct node
{
Int data;
Struct node *next;
};
Int main();
Int i, item, n;
Struct node *head,*p,*q;
Printf(“enter the no of node”);
Scanf(“%d”, &n);
Printf(“enter the value of the head node”);
Scanf(“%d”,&item);
for(i=1;i<n;i++)
{
Printf(“enter the value of next node”);
Scanf(“%d”,&item);
Int main()
{
Struct node *head=null;
Int n,ch,pos,
}
Struct node
{
Struct node * prev;
Int data;
Struct node *next;
}
Int main()
{
Struct
node*head=malloc(siz
eof(struct node));
Headprev=null;
Headdata=10;
Headnext=null;
}