SlideShare a Scribd company logo
1 of 31
Unit 2
ARRAYS
NEED OF ARRAYS :
• An array is a collection of similar data elements, it means it can
hold group of integers, group of characters etc.
• Suppose , we wish to read and store three integers, then one
may define three variables of integer type.
eg. Int a, int b, int c;
• Now if we wish to hold twenty integers , its difficult to define
twenty variables and to remember which variable is holding
what value , it is a big task for any programmer.
• So in this case, we think of using arrays.
Declaration of an Array :
• An array is declared as ,
datatype name [size];
Display integers using an
array
#include<stdio.h>
Void main()
{
int i, a[10];
clrscr();
for(i=0; i<5; i++)
{
printf(“Enter value %d : “, i+1);
scanf(“%d”, &a[i]);
printf(“n”);
}
for(i =0; i<5; i++)
{
printf(“ value %d =%dn”, i+1, a[i]);
}
getch();
}
Output :
Enter value 1 : 5
Enter value 2 : 6
Enter value 3 : 3
Enter value 4 : 4
Enter value 5 : 2
Value 1 = 5
Value 2 = 6
Value 3 = 3
Value 4 = 4
Value 5 = 2
Initialisation of ARRAY
• While using arrays we can accept values from user and we can
also initialised array when we declared it.
int a[5] = { 4, 7, 2, 11, 13 };
If we want to print square of given numbers in array :
printf(“ Numbers t Square n”);
For(i=0; i<5;i++)
{
Printf(“%d t “, a[i]);
printf(“%d n”, a[i] *a[i]);
}
Sum of array values
int I, sum =0, num[10];
Printf(“ enter the 10 array values”);
For(i=0; i<10;i++)
{
scanf(“%d”,&num[i]);
sum = num[i] + sum;
}
Printf(“ Sum of values of array is : %d”, sum);
Find maximum number in
array :
• STEP 1: START
• STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}
• STEP 3: length= sizeof(arr)/sizeof(arr[0])
• STEP 4: max = arr[0]
• STEP 5: SET i=0. REPEAT STEP 6 and STEP 7 i<length
• STEP 6: if(arr[i]>max)
max=arr[i]
• STEP 7: i=i+1.
• STEP 8: PRINT "Largest element in given array:" assigning max.
• STEP 9: RETURN 0
• STEP 9: END.
Passing arrays to Function
• Functions are the methods which are called from main ().
• Functions has prototype as
return data type func_name(arg1, arg2,..);
Function has to be declared in main () before it call.
Declaration is like :
int add(int a, int b);
If not returning a value use void datatype.
Continued……
• #include <stdio.h>
• #include <conio.h>
• void main()
• {
• int a[5]={10,5,45,12,19};
• int n=5,m;
• Int max(int a[],int n);
• clrscr();
• m=max(a,n);
• printf("n MAXIMUM NUMBER IS %d", m);
• getch();
• }
• int max(int x[],int k)
• {
• int t, i;
• t = x[0];
• for(i=1; i<k; i++)
• {
• if(x[i]>t)
• t=x[i];
• }
• return(t);
• }
Inserting element in array
For inserting the element at required position, elements must be
moved downwards to new locations.
Insert element in an Array
int main()
{
int student[40],pos,i,size,value;
printf("enter no of elements in array of students:");
scanf("%d",&size);
printf("enter %d elements are:n",size);
for(i=0;i<size;i++)
scanf("%d",&student[i]);
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);
printf("enter the value into that poition:");
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
printf("final array after inserting the value isn");
for(i=0;i<=size;i++)
printf("%dn",student[i]);
return 0;
}
Algorithm to insert value in array :
1. (initialised the value of i] Set i= len
2. Repeat for i=len down to pos
( shift the elements down by 1 position)
set a[i+1] = a[i]
(End of loop)
3. Insert element at required position
set a[pos] =num;
4. (Reset len) Set len =len+1
5. Display the new list of arrays
6. End
Deleting an Element from array
Algorithm
1. Set item = a[pos]
2. Repeat for j = pos to n-1
[ shifting elements 1 position upwards]
set a[j] = a[j+1] (end of loop)
3. Reset n= n-1
4. Display the new list of element of arrays
5. End
Delete element from array
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete elementn");
scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible.n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
printf("Resultant array:n");
for (c = 0; c < n - 1; c++)
printf("%dn", array[c]);
}
return 0;
}
Hash Table
• Hash Table is a data structure which stores data in an associative manner.
• Hash Table uses an array as a storage medium and uses hash technique to
generate an index where an element is to be inserted or is to be located from.
HASHING :
• Hashing is a technique to convert a range of key values into a range of indexes of
an array. We're going to use modulo operator to get a range of key values.
Consider an example of hash table of size 20, and the following items are to be
stored. Item are in the (key,value) format.
•(1,20)
•(2,70)
•(42,80)
•(4,25)
•(12,44)
•(14,32)
•(17,11)
•(13,78)
•(37,98)
Here we can see the key for particular values
Sr. no key Hash Array index
1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17
Indexing of Hash Table
Linear Probing
As we can see, it may happen that the hashing technique is used to create an
already used index of the array. In such a case, we can search the next empty location
in the array by looking into the next cell until we find an empty cell. This technique is
called linear probing.
Sr.No. Key Hash Array Index After Linear Probing, Array Index
1 1 1 % 20 = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % 20 = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % 20 = 12 12 12
6 14 14 % 20 = 14 14 14
7 17 17 % 20 = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18
Hash Collision
When the hash function generates the same index for multiple keys, there will
be a conflict (what value to be stored in that index). This is called a hash collision.
We can resolve the hash collision using one of the following techniques.
Collision resolution by chaining Open Addressing: Linear/Quadratic Probing and
Double Hashing.
1. Collision resolution by chaining
In chaining, if a hash function produces the same index for multiple elements,
these elements are stored in the same index by using a doubly-linked list.
If j is the slot for multiple elements, it contains a pointer to the head of the list of
elements. If no element is present, j contains NIL.
Open Addressing
Unlike chaining, open addressing doesn't store multiple elements into the same slot.
Here, each slot is either filled with a single key or left NIL.
Different techniques used in open addressing are:
1.Linear Probing
Refer slide no 17
2. Quadratic Probing
It works similar to linear probing but the spacing between the slots is increased
(greater than one) by using the following relation.
h(k, i) = (h′(k) + c1i + c2i2) mod m
where,
c1 and c2 are positive auxiliary constants,
i = {0, 1, ….}
3. Double hashing
If a collision occurs after applying a hash function h(k), then another hash function is
calculated for finding the next slot.
h(k, i) = (h1(k) + ih2(k)) mod m
Where m is the size of the table.
Direct addressing :
• Direct Address Table is a data structure that has the capability of mapping
records to their corresponding keys using arrays. In direct address tables,
records are placed using their key values directly as indexes. They facilitate
fast searching, insertion and deletion operations.
• We can understand the concept using the following example. We create an
array of size equal to maximum value plus one (assuming 0 based index)
and then use values as indexes. For example, in the following diagram key
21 is used directly as index.
Continued…..
• Advantages of Direct Addressing :
• Searching in O(1) Time: Direct address tables use arrays which
are random access data structure, so, the key values (which
are also the index of the array) can be easily used to search
the records in O(1) time.
• Insertion in O(1) Time: We can easily insert an element in an
array in O(1) time. The same thing follows in a direct address
table also.
• Deletion in O(1) Time: Deletion of an element takes O(1) time
in an array. Similarly, to delete an element in a direct address
table we need O(1) time.
Applications of Hash Table
Hash tables are implemented where
• constant time lookup and insertion is required
• cryptographic applications
• indexing data is required
Types og Hash Functions
1. Division method
Hash (key) = Elements % table size;
2 = 42 % 10;
8 = 78 % 10;
9 = 89 % 10;
4 = 64 % 10;
2. Mid Square Method
• In this method, the middle part of the squared element is
taken as the index.
• Element to be placed in the hash table are 210, 350, 99, 890
and the size of the table be 100.
• 210* 210 = 44100, index = 1 as the middle part of the result
(44100) is 1.
3. Digit Folding Method
In this method the element to be placed in the table using hash key, which is
obtained by dividing the elements into various parts and then combine the parts
by performing some simple mathematical operations.
Element to be placed are 23576623, 34687734.
hash (key) = 235+766+23 = 1024
hash key) = 34+68+77+34 = 213
Multidimensional array indexing
• A[3][3] b[3][3]
• 3 4 5 1 3 2
• 1 3 2 2 1 1
• 6 1 1 4 1 2
• A[0][0]= 3
• A[0][1]=4
• A[0][2]= 5
• A[1][0]= 1
• A[1][1]= 3
• A[1][2]= 2
• A[2][0]= 6
• A[2][1]= 1
• A[2][2]= 1
Multi Dimensional Matrix
• For(i =1; i < r; i++) r=3 1<3
• {
• for(j = 2; j < 3 ; j++) c=3 2<3
• {
• scanf(“%dn”,&a[i][j]); a[0][0]= 2 2 1 3
• } a[0][1]= 1 4 5 1
• printf(“n”); a[0][2] = 3
• a[1][0]= 4 a[1][1] = 5
• } a[1][2] = 1
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%dt", sum[c][d]);
}
printf("n");
}
Matrix muliplication
matrix a
3 1 2
1 2 4
3 5 2
Matrix b
2 3 1
4 2 1
3 5 4
A[0][1] =1
B[1][0] = 4 4
A[0][2] = 2
B[2][0] = 3
3*2+1*4+2*3
Mul[0][0]
16
Sparse Matrix
Sparse matrices are those matrices that have the majority of their elements equal to
zero. In other words, the sparse matrix can be defined as the matrix that has a greater
number of zero elements than the non-zero elements.
Why do we need to use a sparse matrix instead of a simple matrix?
We can also use the simple matrix to store the elements in the memory; then why do
we need to use the sparse matrix. The following are the advantages of using a sparse
matrix:
Storage: As we know, a sparse matrix that contains lesser non-zero elements than zero so
less memory can be used to store elements. It evaluates only the non-zero elements.
Computing time: In the case of searching n sparse matrix, we need to traverse only the non-
zero elements rather than traversing all the sparse matrix elements. It saves computing time
by logically designing a data structure traversing non-zero elements.
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. The
zeroes in the matrix are of no use to store zeroes with non-zero elements. To avoid such
wastage, we can store only non-zero elements. If we store only non-zero elements, it
Continued……
Array Representation
• Row: It is an index of a row where a non-zero element is located.
• Column: It is an index of the column where a non-zero element is located.
• Value: The value of the non-zero element is located at the index (row, column).
• Let's understand the sparse matrix using array representation through an
example.
Let's understand the sparse matrix using array representation through an
example.
As we can observe above, that sparse matrix is represented using triplets,
i.e., row, column, and value. In the above sparse matrix, there are 13 zero
elements and 7 non-zero elements. This sparse matrix occupies 5*4 = 20
memory space
Analysing Algorithm
In the analysis of the
algorithm, it generally focused
on CPU (time) usage, Memory
usage, Disk usage, and
Network usage. All are
important, but the most
concern is about the CPU
time. Be careful to
• Best case: Define the input for which algorithm takes less time or minimum
time. In the best case calculate the lower bound of an algorithm. Example: In
the linear search when search data is present at the first location of large data
then the best case occurs.
• Worst Case: Define the input for which algorithm takes a long time or
maximum time. In the worst calculate the upper bound of an algorithm.
Example: In the linear search when search data is not present at all then the
worst case occurs.
• Average case: In the average case take all random inputs and calculate the
computation time for all inputs.
And then we divide it by the total number of inputs.

More Related Content

Similar to DS Unit 1.pptx

Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashingDabbal Singh Mahara
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptxParagAhir1
 
Array
ArrayArray
ArrayPRN USM
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptxMURADSANJOUM
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdfAmuthachenthiruK
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notesGOKULKANNANMMECLECTC
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptxMrMaster11
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...nsitlokeshjain
 

Similar to DS Unit 1.pptx (20)

Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Array
ArrayArray
Array
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Arrays
ArraysArrays
Arrays
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
Arrays
ArraysArrays
Arrays
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Lec4
Lec4Lec4
Lec4
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 

Recently uploaded

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 

Recently uploaded (20)

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 

DS Unit 1.pptx

  • 2. NEED OF ARRAYS : • An array is a collection of similar data elements, it means it can hold group of integers, group of characters etc. • Suppose , we wish to read and store three integers, then one may define three variables of integer type. eg. Int a, int b, int c; • Now if we wish to hold twenty integers , its difficult to define twenty variables and to remember which variable is holding what value , it is a big task for any programmer. • So in this case, we think of using arrays.
  • 3. Declaration of an Array : • An array is declared as , datatype name [size];
  • 4. Display integers using an array #include<stdio.h> Void main() { int i, a[10]; clrscr(); for(i=0; i<5; i++) { printf(“Enter value %d : “, i+1); scanf(“%d”, &a[i]); printf(“n”); } for(i =0; i<5; i++) { printf(“ value %d =%dn”, i+1, a[i]); } getch(); } Output : Enter value 1 : 5 Enter value 2 : 6 Enter value 3 : 3 Enter value 4 : 4 Enter value 5 : 2 Value 1 = 5 Value 2 = 6 Value 3 = 3 Value 4 = 4 Value 5 = 2
  • 5. Initialisation of ARRAY • While using arrays we can accept values from user and we can also initialised array when we declared it. int a[5] = { 4, 7, 2, 11, 13 }; If we want to print square of given numbers in array : printf(“ Numbers t Square n”); For(i=0; i<5;i++) { Printf(“%d t “, a[i]); printf(“%d n”, a[i] *a[i]); }
  • 6. Sum of array values int I, sum =0, num[10]; Printf(“ enter the 10 array values”); For(i=0; i<10;i++) { scanf(“%d”,&num[i]); sum = num[i] + sum; } Printf(“ Sum of values of array is : %d”, sum);
  • 7. Find maximum number in array : • STEP 1: START • STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56} • STEP 3: length= sizeof(arr)/sizeof(arr[0]) • STEP 4: max = arr[0] • STEP 5: SET i=0. REPEAT STEP 6 and STEP 7 i<length • STEP 6: if(arr[i]>max) max=arr[i] • STEP 7: i=i+1. • STEP 8: PRINT "Largest element in given array:" assigning max. • STEP 9: RETURN 0 • STEP 9: END.
  • 8. Passing arrays to Function • Functions are the methods which are called from main (). • Functions has prototype as return data type func_name(arg1, arg2,..); Function has to be declared in main () before it call. Declaration is like : int add(int a, int b); If not returning a value use void datatype.
  • 9. Continued…… • #include <stdio.h> • #include <conio.h> • void main() • { • int a[5]={10,5,45,12,19}; • int n=5,m; • Int max(int a[],int n); • clrscr(); • m=max(a,n); • printf("n MAXIMUM NUMBER IS %d", m); • getch(); • } • int max(int x[],int k) • { • int t, i; • t = x[0]; • for(i=1; i<k; i++) • { • if(x[i]>t) • t=x[i]; • } • return(t); • }
  • 10. Inserting element in array For inserting the element at required position, elements must be moved downwards to new locations.
  • 11. Insert element in an Array int main() { int student[40],pos,i,size,value; printf("enter no of elements in array of students:"); scanf("%d",&size); printf("enter %d elements are:n",size); for(i=0;i<size;i++) scanf("%d",&student[i]); printf("enter the position where you want to insert the element:"); scanf("%d",&pos); printf("enter the value into that poition:"); scanf("%d",&value); for(i=size-1;i>=pos-1;i--) student[i+1]=student[i]; student[pos-1]= value; printf("final array after inserting the value isn"); for(i=0;i<=size;i++) printf("%dn",student[i]); return 0; }
  • 12. Algorithm to insert value in array : 1. (initialised the value of i] Set i= len 2. Repeat for i=len down to pos ( shift the elements down by 1 position) set a[i+1] = a[i] (End of loop) 3. Insert element at required position set a[pos] =num; 4. (Reset len) Set len =len+1 5. Display the new list of arrays 6. End
  • 13. Deleting an Element from array Algorithm 1. Set item = a[pos] 2. Repeat for j = pos to n-1 [ shifting elements 1 position upwards] set a[j] = a[j+1] (end of loop) 3. Reset n= n-1 4. Display the new list of element of arrays 5. End
  • 14. Delete element from array int main() { int array[100], position, c, n; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to delete elementn"); scanf("%d", &position); if (position >= n+1) printf("Deletion not possible.n"); else { for (c = position - 1; c < n - 1; c++) array[c] = array[c+1]; printf("Resultant array:n"); for (c = 0; c < n - 1; c++) printf("%dn", array[c]); } return 0; }
  • 15. Hash Table • Hash Table is a data structure which stores data in an associative manner. • Hash Table uses an array as a storage medium and uses hash technique to generate an index where an element is to be inserted or is to be located from. HASHING : • Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and the following items are to be stored. Item are in the (key,value) format. •(1,20) •(2,70) •(42,80) •(4,25) •(12,44) •(14,32) •(17,11) •(13,78) •(37,98) Here we can see the key for particular values
  • 16. Sr. no key Hash Array index 1 1 1 % 20 = 1 1 2 2 2 % 20 = 2 2 3 42 42 % 20 = 2 2 4 4 4 % 20 = 4 4 5 12 12 % 20 = 12 12 6 14 14 % 20 = 14 14 7 17 17 % 20 = 17 17 8 13 13 % 20 = 13 13 9 37 37 % 20 = 17 17 Indexing of Hash Table
  • 17. Linear Probing As we can see, it may happen that the hashing technique is used to create an already used index of the array. In such a case, we can search the next empty location in the array by looking into the next cell until we find an empty cell. This technique is called linear probing. Sr.No. Key Hash Array Index After Linear Probing, Array Index 1 1 1 % 20 = 1 1 1 2 2 2 % 20 = 2 2 2 3 42 42 % 20 = 2 2 3 4 4 4 % 20 = 4 4 4 5 12 12 % 20 = 12 12 12 6 14 14 % 20 = 14 14 14 7 17 17 % 20 = 17 17 17 8 13 13 % 20 = 13 13 13 9 37 37 % 20 = 17 17 18
  • 18. Hash Collision When the hash function generates the same index for multiple keys, there will be a conflict (what value to be stored in that index). This is called a hash collision. We can resolve the hash collision using one of the following techniques. Collision resolution by chaining Open Addressing: Linear/Quadratic Probing and Double Hashing. 1. Collision resolution by chaining In chaining, if a hash function produces the same index for multiple elements, these elements are stored in the same index by using a doubly-linked list. If j is the slot for multiple elements, it contains a pointer to the head of the list of elements. If no element is present, j contains NIL.
  • 19. Open Addressing Unlike chaining, open addressing doesn't store multiple elements into the same slot. Here, each slot is either filled with a single key or left NIL. Different techniques used in open addressing are: 1.Linear Probing Refer slide no 17 2. Quadratic Probing It works similar to linear probing but the spacing between the slots is increased (greater than one) by using the following relation. h(k, i) = (h′(k) + c1i + c2i2) mod m where, c1 and c2 are positive auxiliary constants, i = {0, 1, ….} 3. Double hashing If a collision occurs after applying a hash function h(k), then another hash function is calculated for finding the next slot. h(k, i) = (h1(k) + ih2(k)) mod m Where m is the size of the table.
  • 20. Direct addressing : • Direct Address Table is a data structure that has the capability of mapping records to their corresponding keys using arrays. In direct address tables, records are placed using their key values directly as indexes. They facilitate fast searching, insertion and deletion operations. • We can understand the concept using the following example. We create an array of size equal to maximum value plus one (assuming 0 based index) and then use values as indexes. For example, in the following diagram key 21 is used directly as index.
  • 21. Continued….. • Advantages of Direct Addressing : • Searching in O(1) Time: Direct address tables use arrays which are random access data structure, so, the key values (which are also the index of the array) can be easily used to search the records in O(1) time. • Insertion in O(1) Time: We can easily insert an element in an array in O(1) time. The same thing follows in a direct address table also. • Deletion in O(1) Time: Deletion of an element takes O(1) time in an array. Similarly, to delete an element in a direct address table we need O(1) time.
  • 22. Applications of Hash Table Hash tables are implemented where • constant time lookup and insertion is required • cryptographic applications • indexing data is required
  • 23. Types og Hash Functions 1. Division method Hash (key) = Elements % table size; 2 = 42 % 10; 8 = 78 % 10; 9 = 89 % 10; 4 = 64 % 10; 2. Mid Square Method • In this method, the middle part of the squared element is taken as the index. • Element to be placed in the hash table are 210, 350, 99, 890 and the size of the table be 100. • 210* 210 = 44100, index = 1 as the middle part of the result (44100) is 1.
  • 24. 3. Digit Folding Method In this method the element to be placed in the table using hash key, which is obtained by dividing the elements into various parts and then combine the parts by performing some simple mathematical operations. Element to be placed are 23576623, 34687734. hash (key) = 235+766+23 = 1024 hash key) = 34+68+77+34 = 213
  • 25. Multidimensional array indexing • A[3][3] b[3][3] • 3 4 5 1 3 2 • 1 3 2 2 1 1 • 6 1 1 4 1 2 • A[0][0]= 3 • A[0][1]=4 • A[0][2]= 5 • A[1][0]= 1 • A[1][1]= 3 • A[1][2]= 2 • A[2][0]= 6 • A[2][1]= 1 • A[2][2]= 1
  • 26. Multi Dimensional Matrix • For(i =1; i < r; i++) r=3 1<3 • { • for(j = 2; j < 3 ; j++) c=3 2<3 • { • scanf(“%dn”,&a[i][j]); a[0][0]= 2 2 1 3 • } a[0][1]= 1 4 5 1 • printf(“n”); a[0][2] = 3 • a[1][0]= 4 a[1][1] = 5 • } a[1][2] = 1 for (c = 0; c < m; c++) { for (d = 0 ; d < n; d++) { sum[c][d] = first[c][d] + second[c][d]; printf("%dt", sum[c][d]); } printf("n"); }
  • 27. Matrix muliplication matrix a 3 1 2 1 2 4 3 5 2 Matrix b 2 3 1 4 2 1 3 5 4 A[0][1] =1 B[1][0] = 4 4 A[0][2] = 2 B[2][0] = 3 3*2+1*4+2*3 Mul[0][0] 16
  • 28. Sparse Matrix Sparse matrices are those matrices that have the majority of their elements equal to zero. In other words, the sparse matrix can be defined as the matrix that has a greater number of zero elements than the non-zero elements. Why do we need to use a sparse matrix instead of a simple matrix? We can also use the simple matrix to store the elements in the memory; then why do we need to use the sparse matrix. The following are the advantages of using a sparse matrix: Storage: As we know, a sparse matrix that contains lesser non-zero elements than zero so less memory can be used to store elements. It evaluates only the non-zero elements. Computing time: In the case of searching n sparse matrix, we need to traverse only the non- zero elements rather than traversing all the sparse matrix elements. It saves computing time by logically designing a data structure traversing non-zero elements. Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. The zeroes in the matrix are of no use to store zeroes with non-zero elements. To avoid such wastage, we can store only non-zero elements. If we store only non-zero elements, it
  • 29. Continued…… Array Representation • Row: It is an index of a row where a non-zero element is located. • Column: It is an index of the column where a non-zero element is located. • Value: The value of the non-zero element is located at the index (row, column). • Let's understand the sparse matrix using array representation through an example. Let's understand the sparse matrix using array representation through an example. As we can observe above, that sparse matrix is represented using triplets, i.e., row, column, and value. In the above sparse matrix, there are 13 zero elements and 7 non-zero elements. This sparse matrix occupies 5*4 = 20 memory space
  • 30. Analysing Algorithm In the analysis of the algorithm, it generally focused on CPU (time) usage, Memory usage, Disk usage, and Network usage. All are important, but the most concern is about the CPU time. Be careful to
  • 31. • Best case: Define the input for which algorithm takes less time or minimum time. In the best case calculate the lower bound of an algorithm. Example: In the linear search when search data is present at the first location of large data then the best case occurs. • Worst Case: Define the input for which algorithm takes a long time or maximum time. In the worst calculate the upper bound of an algorithm. Example: In the linear search when search data is not present at all then the worst case occurs. • Average case: In the average case take all random inputs and calculate the computation time for all inputs. And then we divide it by the total number of inputs.