SlideShare a Scribd company logo
Arrays
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and DataJava and Data
StructuresStructures
Array
• An array is a collection of homogeneous type of data
elements.
• An array is consisting of a collection of elements .
• Operation Performed On Array:
1. Traversing
2. Search
3. Insertion
4. Deletion
5. Sorting
6. Merging
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
1
2
3
4
5
Representation of array
Linear Arrays
• A linear array is the list of finite number ‘N’ of homogeneous
data elements (i.e. data elements of same type)
• Length = UB – LB + 1
• Array A may be denoted by,
– A1, A2, A3, … AN - Subscript Notation
– A(1), A(2), A(3), …., A(N) – Fortran, PL/I, BASIC
– A[1], A[2], A[3],..,A[N] – PASCAL, C, C++, Java
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Traversing
• This algorithm traverses a linear array A with lower bound LB
and upper bound UB.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
//Using for loop
for k := LB to UB
Process -> LA [k];
//Using while loop
k := LB
while(k <= UB)
{
Process -> LA[k];
k:= k + 1;
}
Inserting
22
23
26
28
29
31
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
LA[1]
LA[2]
LA[3]
LA[4]
LA[5]
LA[6]
LA[7]
22
23
25
26
28
29
31
Algorithm Insert(LA, N, K, ITEM)
{
J := N; //Initialize counter
while(J >= K)
{
LA[J + 1] := LA [J]; // Move one elements downward
J := J - 1; // Decrease counter by 1
}
LA[K] := ITEM // Insert element
N := N + 1; // Reset N
}
Deleting
Here LA is a linear array with N elements.
LOC is the location where ITEM is to be deleted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm Delete(LA, N, LOC, ITEM)
{
ITEM := LA[LOC] // Assign the elements to be deleted
for J := LOC to N do J := J + 1
{
LA[J] := LA [J + 1]; // Move Jth element upwards
}
N := N - 1; // Reset N
}
Searching Algorithms
• Linear Search
– A linear search sequentially moves through your collection (or data
structure) looking for a matching value.
– Worst case performance scenario for a linear search is that it needs to
loop through the entire collection; either because the item is the last
one, or because the item isn't found.
– In other words, if you have N items in your collection, the worst case
scenario to find an item is N iterations. This is known as O(N) using the
Big O Notation.
– Linear searches don't require the collection to be sorted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Linear Search
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm LinearSearch (LA, N, ITEM)
{
for J := 1 to N do J := J + 1
{
if(ITEM = LA [j]) then
{
write (ITEM +" found at location " +J);
Return;
}
}
if(J > N) then
{
write (ITEM +" does not exist.");
}
}
55 88 66 77 99 11 6
J = 1 J = 2 J = 3
= 6= 6 = 6= 6 = 6= 6 6 found at location 3
Searching Algorithms
• Binary Search
– Binary search relies on a divide and conquer strategy to find a value
within an already-sorted collection.
– The algorithm is deceptively simple.
– Binary search requires a sorted collection.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Binary Search
ITEM = 40 BEG = 1, END = 13
BEG = 1, END = 6
40 > 30
BEG = 4, END = 6
40 = 40
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
11 22 30 33 40 44 55 60 66 77 80 88 99
11 22 30 33 40 44 55 60 66 77 80 88 99
11 22 30 33 40 44 55 60 66 77 80 88 99
40 < 55
Binary Search
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm Binary (DATA, LB, UB, ITEM, LOC)
{
BEG := LB;
END := UB;
MID := INT((BEG + END)/2);
while(BEG <= END && DATA [MID] != ITEM)
{
if (ITEM < DATA [MID]) then
END := MID - 1;
else
BEG := MID + 1;
MID := INT((BEG + END)/2);
}
if(DATA [MID] = ITEM) then
LOC := MID;
else
LOC := NULL;
}
Sorting
• Bubble Sort
• Insertion Sort
• Selection Sort
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Bubble Sort
Suppose the following numbers are stored in an array A.
We apply bubble sort to the array A.
Pass 1
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
32 51 27 85 66 23 13
32 51 27 85 66 23 13
32 51 27 85 66 23 13
32 27 51 85 66 23 13
32 27 51 85 66 23 13
32 27 51 66 85 23 13
32 27 51 66 23 85 13
32 27 51 66 23 13 85
32 < 51
51 > 27
51 < 85
85 > 66
85 < 23
85 >13
Bubble Sort
Suppose the following numbers are stored in an array A.
We apply bubble sort to the array A.
Pass 2
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
32 51 27 85 66 23 13
32 27 51 66 23 13 85
27 32 51 66 23 13 85
27 32 51 66 23 13 85
27 32 51 66 23 13 85
27 32 51 23 66 13 85
27 32 51 23 13 66 85
27 32 51 23 13 66 85
32 > 27
32 < 51
51 < 66
66 > 23
66 > 13
66 < 85
Bubble Sort
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm Bubble (DATA, N)
{
for K : = 1 to N -1 do K := K + 1
{
PTR : = 1;
While(PTR <= N - K)
{
if(DATA [PTR] > DATA [PTR +1]) then
Interchange (DATA [PTR] and DATA [PTR +1])
PTR := PTR +1;
}
}
}
Insertion Sort
• Suppose an array A with N elements, the insertion sort algorithm scans A
from 1 to N, inserting each element A [K] into its proper position in the
previously sorted sub-array A[1], A [2] , …, A[K-1].
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
77 33 44 11 88 22 66 55
77 33 44 11 88 22 66 55
33 77 44 11 88 22 66 55
33 44 77 11 88 22 66 55
11 33 44 77 88 22 66 55
11 33 44 77 88 22 66 55
11 22 33 44 77 88 66 55
11 22 33 44 66 77 88 55
11 22 33 44 55 66 77 88
Pass
K = 1
K = 2
K = 3
K = 4
K = 5
K = 6
K = 7
K = 8
Sorted
1 2 3 4 5 6 7 8
Insertion Sort
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm INSERTION (DATA, N)
{
for K:= 1 to N do K := K + 1
{
TEMP := DATA [K];
PTR := K - 1;
while (PTR >= 0 && TEMP < DATA [PTR])
{
DATA [PTR + 1] := DATA [PTR];
PTR = PTR - 1;
}
DATA [PTR + 1]:= TEMP;
}
}
Selection Sort
• Selection sort algorithm for sorting A works as follows:
– First find the smallest element in the list and put it in the
first position.
– Then find the second smallest element in the list and put it
in the second position.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Selection Sort
Suppose an array A with 8 elements as follows:
77, 33, 44, 11, 88, 22, 66, 55.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
77 33 44 11 88 22 66 55
11 33 44 77 88 22 66 55
11 22 44 77 88 33 66 55
11 22 33 77 88 44 66 55
11 22 33 44 88 77 66 55
11 22 33 44 55 77 66 88
11 22 33 44 55 66 77 88
11 22 33 44 55 66 77 88
Pass
K = 1
K = 2
K = 3
K = 4
K = 5
K = 6
K = 7
1 2 3 4 5 6 7 8
LOC = 4
LOC = 6
LOC = 6
LOC = 6
LOC = 8
LOC = 7
LOC = 7
Sorted
Selection Sort
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm SELECTION (A, N)
{
for J := 1 to N do J := J + 1
{
MINI(A, J, N, LOC);
[Interchange A[J] and A[LOC]];
TEMP := A[J];
A[J] := A[LOC];
A[LOC] := TEMP;
}
}
MINI(A, K, N, LOC)
{
MIN := A[K] and LOC := K;
for J := K + 1 to N do J := J + 1
{
if (MIN > A [J]) then
{
MIN := A [J];
LOC := J;
}
}
Return LOC;
}
Q & A

More Related Content

What's hot

Machine learning with R
Machine learning with RMachine learning with R
Machine learning with R
Maarten Smeets
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
IRJET Journal
 
Introduction to lambda calculus
Introduction to lambda calculusIntroduction to lambda calculus
Introduction to lambda calculus
Afaq Siddiqui
 
IR-ranking
IR-rankingIR-ranking
IR-rankingFELIX75
 
An Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
An Introduction to Higher Order Functions in Spark SQL with Herman van HovellAn Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
An Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
Databricks
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structures
maznabili
 
Spark Schema For Free with David Szakallas
 Spark Schema For Free with David Szakallas Spark Schema For Free with David Szakallas
Spark Schema For Free with David Szakallas
Databricks
 
Spark schema for free with David Szakallas
Spark schema for free with David SzakallasSpark schema for free with David Szakallas
Spark schema for free with David Szakallas
Databricks
 
Lecture 1 Introduction C++
Lecture 1 Introduction C++Lecture 1 Introduction C++
Lecture 1 Introduction C++
Ajay Khatri
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
talha ijaz
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
jehan1987
 
KDGAN: Knowledge Distillation with Generative Adversarial Networks
KDGAN: Knowledge Distillation with Generative Adversarial NetworksKDGAN: Knowledge Distillation with Generative Adversarial Networks
KDGAN: Knowledge Distillation with Generative Adversarial Networks
Sungchul Kim
 

What's hot (13)

264finalppt (1)
264finalppt (1)264finalppt (1)
264finalppt (1)
 
Machine learning with R
Machine learning with RMachine learning with R
Machine learning with R
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Introduction to lambda calculus
Introduction to lambda calculusIntroduction to lambda calculus
Introduction to lambda calculus
 
IR-ranking
IR-rankingIR-ranking
IR-ranking
 
An Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
An Introduction to Higher Order Functions in Spark SQL with Herman van HovellAn Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
An Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structures
 
Spark Schema For Free with David Szakallas
 Spark Schema For Free with David Szakallas Spark Schema For Free with David Szakallas
Spark Schema For Free with David Szakallas
 
Spark schema for free with David Szakallas
Spark schema for free with David SzakallasSpark schema for free with David Szakallas
Spark schema for free with David Szakallas
 
Lecture 1 Introduction C++
Lecture 1 Introduction C++Lecture 1 Introduction C++
Lecture 1 Introduction C++
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
KDGAN: Knowledge Distillation with Generative Adversarial Networks
KDGAN: Knowledge Distillation with Generative Adversarial NetworksKDGAN: Knowledge Distillation with Generative Adversarial Networks
KDGAN: Knowledge Distillation with Generative Adversarial Networks
 

Viewers also liked

7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
Nilesh Dalvi
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
Nilesh Dalvi
 
8. String
8. String8. String
8. String
Nilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
Nilesh Dalvi
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
Nilesh Dalvi
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Por que sua próxima aplicação web deve ser em Clojure?
Por que sua próxima aplicação web deve ser em Clojure?Por que sua próxima aplicação web deve ser em Clojure?
Por que sua próxima aplicação web deve ser em Clojure?
Eduardo Aquiles Affonso Radanovitsck
 
Chinese Cultural Entailment 中国文化蕴涵
Chinese Cultural Entailment  中国文化蕴涵Chinese Cultural Entailment  中国文化蕴涵
Chinese Cultural Entailment 中国文化蕴涵
John Jeffery
 
TDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do RailsTDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do Rails
tdc-globalcode
 
TDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viuTDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viu
tdc-globalcode
 
Blog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajasBlog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajas
NELLYS29
 
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o MalTDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
tdc-globalcode
 
Actividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUEActividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUE
Paolachable
 
取是一種本事捨是一種智慧
取是一種本事捨是一種智慧取是一種本事捨是一種智慧
取是一種本事捨是一種智慧Jaing Lai
 
TDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com PythonTDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com Python
tdc-globalcode
 
Como hacer una pagina en wix
Como hacer una pagina en wixComo hacer una pagina en wix
Como hacer una pagina en wixwiston98
 
Exposicion final
Exposicion finalExposicion final
Exposicion final
Person0001
 
TDC2016SP - Luiza Labs - Migrando .NET p/ Python
TDC2016SP - Luiza Labs - Migrando .NET p/ PythonTDC2016SP - Luiza Labs - Migrando .NET p/ Python
TDC2016SP - Luiza Labs - Migrando .NET p/ Python
tdc-globalcode
 
TDC2016SP - Flask para Web
TDC2016SP - Flask para WebTDC2016SP - Flask para Web
TDC2016SP - Flask para Web
tdc-globalcode
 

Viewers also liked (20)

7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
8. String
8. String8. String
8. String
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Por que sua próxima aplicação web deve ser em Clojure?
Por que sua próxima aplicação web deve ser em Clojure?Por que sua próxima aplicação web deve ser em Clojure?
Por que sua próxima aplicação web deve ser em Clojure?
 
Chinese Cultural Entailment 中国文化蕴涵
Chinese Cultural Entailment  中国文化蕴涵Chinese Cultural Entailment  中国文化蕴涵
Chinese Cultural Entailment 中国文化蕴涵
 
TDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do RailsTDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do Rails
 
TDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viuTDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viu
 
Blog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajasBlog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajas
 
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o MalTDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
 
Actividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUEActividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUE
 
取是一種本事捨是一種智慧
取是一種本事捨是一種智慧取是一種本事捨是一種智慧
取是一種本事捨是一種智慧
 
TDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com PythonTDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com Python
 
Como hacer una pagina en wix
Como hacer una pagina en wixComo hacer una pagina en wix
Como hacer una pagina en wix
 
Exposicion final
Exposicion finalExposicion final
Exposicion final
 
TDC2016SP - Luiza Labs - Migrando .NET p/ Python
TDC2016SP - Luiza Labs - Migrando .NET p/ PythonTDC2016SP - Luiza Labs - Migrando .NET p/ Python
TDC2016SP - Luiza Labs - Migrando .NET p/ Python
 
TDC2016SP - Flask para Web
TDC2016SP - Flask para WebTDC2016SP - Flask para Web
TDC2016SP - Flask para Web
 

Similar to 11. Arrays

Sorting
SortingSorting
Sorting
Zaid Shabbir
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
AJAYVISHALRP
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
Dr. Shaukat Wasi
 
Sorting.ppt read only
Sorting.ppt read onlySorting.ppt read only
Sorting.ppt read only
VARSHAKUMARI49
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
PRIANKA R
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to Algorithm
Education Front
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 
Array Operations.pptxdata structure array indsa
Array Operations.pptxdata structure array indsaArray Operations.pptxdata structure array indsa
Array Operations.pptxdata structure array indsa
ar0454492
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Vectors,squence & list
Vectors,squence & listVectors,squence & list
Vectors,squence & list
Monika Sanghani
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)eShikshak
 
arrays
arraysarrays
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban92
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5
Vishal Patil
 

Similar to 11. Arrays (20)

Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
Sorting.ppt read only
Sorting.ppt read onlySorting.ppt read only
Sorting.ppt read only
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to Algorithm
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Array Operations.pptxdata structure array indsa
Array Operations.pptxdata structure array indsaArray Operations.pptxdata structure array indsa
Array Operations.pptxdata structure array indsa
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Vectors,squence & list
Vectors,squence & listVectors,squence & list
Vectors,squence & list
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
arrays
arraysarrays
arrays
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5
 

More from Nilesh Dalvi

2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
Nilesh Dalvi
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
Nilesh Dalvi
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 
File handling
File handlingFile handling
File handling
Nilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Strings
StringsStrings
Strings
Nilesh Dalvi
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
Nilesh Dalvi
 

More from Nilesh Dalvi (12)

2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Templates
TemplatesTemplates
Templates
 
File handling
File handlingFile handling
File handling
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Strings
StringsStrings
Strings
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 

11. Arrays

  • 1. Arrays By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and DataJava and Data StructuresStructures
  • 2. Array • An array is a collection of homogeneous type of data elements. • An array is consisting of a collection of elements . • Operation Performed On Array: 1. Traversing 2. Search 3. Insertion 4. Deletion 5. Sorting 6. Merging Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 1 2 3 4 5 Representation of array
  • 3. Linear Arrays • A linear array is the list of finite number ‘N’ of homogeneous data elements (i.e. data elements of same type) • Length = UB – LB + 1 • Array A may be denoted by, – A1, A2, A3, … AN - Subscript Notation – A(1), A(2), A(3), …., A(N) – Fortran, PL/I, BASIC – A[1], A[2], A[3],..,A[N] – PASCAL, C, C++, Java Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. Traversing • This algorithm traverses a linear array A with lower bound LB and upper bound UB. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). //Using for loop for k := LB to UB Process -> LA [k]; //Using while loop k := LB while(k <= UB) { Process -> LA[k]; k:= k + 1; }
  • 5. Inserting 22 23 26 28 29 31 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). LA[1] LA[2] LA[3] LA[4] LA[5] LA[6] LA[7] 22 23 25 26 28 29 31 Algorithm Insert(LA, N, K, ITEM) { J := N; //Initialize counter while(J >= K) { LA[J + 1] := LA [J]; // Move one elements downward J := J - 1; // Decrease counter by 1 } LA[K] := ITEM // Insert element N := N + 1; // Reset N }
  • 6. Deleting Here LA is a linear array with N elements. LOC is the location where ITEM is to be deleted. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm Delete(LA, N, LOC, ITEM) { ITEM := LA[LOC] // Assign the elements to be deleted for J := LOC to N do J := J + 1 { LA[J] := LA [J + 1]; // Move Jth element upwards } N := N - 1; // Reset N }
  • 7. Searching Algorithms • Linear Search – A linear search sequentially moves through your collection (or data structure) looking for a matching value. – Worst case performance scenario for a linear search is that it needs to loop through the entire collection; either because the item is the last one, or because the item isn't found. – In other words, if you have N items in your collection, the worst case scenario to find an item is N iterations. This is known as O(N) using the Big O Notation. – Linear searches don't require the collection to be sorted. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. Linear Search Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm LinearSearch (LA, N, ITEM) { for J := 1 to N do J := J + 1 { if(ITEM = LA [j]) then { write (ITEM +" found at location " +J); Return; } } if(J > N) then { write (ITEM +" does not exist."); } } 55 88 66 77 99 11 6 J = 1 J = 2 J = 3 = 6= 6 = 6= 6 = 6= 6 6 found at location 3
  • 9. Searching Algorithms • Binary Search – Binary search relies on a divide and conquer strategy to find a value within an already-sorted collection. – The algorithm is deceptively simple. – Binary search requires a sorted collection. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 10. Binary Search ITEM = 40 BEG = 1, END = 13 BEG = 1, END = 6 40 > 30 BEG = 4, END = 6 40 = 40 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 11 22 30 33 40 44 55 60 66 77 80 88 99 11 22 30 33 40 44 55 60 66 77 80 88 99 11 22 30 33 40 44 55 60 66 77 80 88 99 40 < 55
  • 11. Binary Search Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm Binary (DATA, LB, UB, ITEM, LOC) { BEG := LB; END := UB; MID := INT((BEG + END)/2); while(BEG <= END && DATA [MID] != ITEM) { if (ITEM < DATA [MID]) then END := MID - 1; else BEG := MID + 1; MID := INT((BEG + END)/2); } if(DATA [MID] = ITEM) then LOC := MID; else LOC := NULL; }
  • 12. Sorting • Bubble Sort • Insertion Sort • Selection Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 13. Bubble Sort Suppose the following numbers are stored in an array A. We apply bubble sort to the array A. Pass 1 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 32 51 27 85 66 23 13 32 51 27 85 66 23 13 32 51 27 85 66 23 13 32 27 51 85 66 23 13 32 27 51 85 66 23 13 32 27 51 66 85 23 13 32 27 51 66 23 85 13 32 27 51 66 23 13 85 32 < 51 51 > 27 51 < 85 85 > 66 85 < 23 85 >13
  • 14. Bubble Sort Suppose the following numbers are stored in an array A. We apply bubble sort to the array A. Pass 2 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 32 51 27 85 66 23 13 32 27 51 66 23 13 85 27 32 51 66 23 13 85 27 32 51 66 23 13 85 27 32 51 66 23 13 85 27 32 51 23 66 13 85 27 32 51 23 13 66 85 27 32 51 23 13 66 85 32 > 27 32 < 51 51 < 66 66 > 23 66 > 13 66 < 85
  • 15. Bubble Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm Bubble (DATA, N) { for K : = 1 to N -1 do K := K + 1 { PTR : = 1; While(PTR <= N - K) { if(DATA [PTR] > DATA [PTR +1]) then Interchange (DATA [PTR] and DATA [PTR +1]) PTR := PTR +1; } } }
  • 16. Insertion Sort • Suppose an array A with N elements, the insertion sort algorithm scans A from 1 to N, inserting each element A [K] into its proper position in the previously sorted sub-array A[1], A [2] , …, A[K-1]. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 77 33 44 11 88 22 66 55 77 33 44 11 88 22 66 55 33 77 44 11 88 22 66 55 33 44 77 11 88 22 66 55 11 33 44 77 88 22 66 55 11 33 44 77 88 22 66 55 11 22 33 44 77 88 66 55 11 22 33 44 66 77 88 55 11 22 33 44 55 66 77 88 Pass K = 1 K = 2 K = 3 K = 4 K = 5 K = 6 K = 7 K = 8 Sorted 1 2 3 4 5 6 7 8
  • 17. Insertion Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm INSERTION (DATA, N) { for K:= 1 to N do K := K + 1 { TEMP := DATA [K]; PTR := K - 1; while (PTR >= 0 && TEMP < DATA [PTR]) { DATA [PTR + 1] := DATA [PTR]; PTR = PTR - 1; } DATA [PTR + 1]:= TEMP; } }
  • 18. Selection Sort • Selection sort algorithm for sorting A works as follows: – First find the smallest element in the list and put it in the first position. – Then find the second smallest element in the list and put it in the second position. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 19. Selection Sort Suppose an array A with 8 elements as follows: 77, 33, 44, 11, 88, 22, 66, 55. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 77 33 44 11 88 22 66 55 11 33 44 77 88 22 66 55 11 22 44 77 88 33 66 55 11 22 33 77 88 44 66 55 11 22 33 44 88 77 66 55 11 22 33 44 55 77 66 88 11 22 33 44 55 66 77 88 11 22 33 44 55 66 77 88 Pass K = 1 K = 2 K = 3 K = 4 K = 5 K = 6 K = 7 1 2 3 4 5 6 7 8 LOC = 4 LOC = 6 LOC = 6 LOC = 6 LOC = 8 LOC = 7 LOC = 7 Sorted
  • 20. Selection Sort Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm SELECTION (A, N) { for J := 1 to N do J := J + 1 { MINI(A, J, N, LOC); [Interchange A[J] and A[LOC]]; TEMP := A[J]; A[J] := A[LOC]; A[LOC] := TEMP; } } MINI(A, K, N, LOC) { MIN := A[K] and LOC := K; for J := K + 1 to N do J := J + 1 { if (MIN > A [J]) then { MIN := A [J]; LOC := J; } } Return LOC; }
  • 21. Q & A