SlideShare a Scribd company logo
1 of 21
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 RMaarten Smeets
 
Introduction to lambda calculus
Introduction to lambda calculusIntroduction to lambda calculus
Introduction to lambda calculusAfaq 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 HovellDatabricks
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structuresmaznabili
 
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 SzakallasDatabricks
 
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 SzakallasDatabricks
 
Lecture 1 Introduction C++
Lecture 1 Introduction C++Lecture 1 Introduction C++
Lecture 1 Introduction C++Ajay Khatri
 
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 Onejehan1987
 
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 NetworksSungchul 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

6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
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 Railstdc-globalcode
 
TDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viuTDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viutdc-globalcode
 
Blog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajasBlog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajasNELLYS29
 
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 Maltdc-globalcode
 
Actividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUEActividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUEPaolachable
 
取是一種本事捨是一種智慧
取是一種本事捨是一種智慧取是一種本事捨是一種智慧
取是一種本事捨是一種智慧Jaing Lai
 
TDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com PythonTDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com Pythontdc-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 finalPerson0001
 
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/ Pythontdc-globalcode
 
TDC2016SP - Flask para Web
TDC2016SP - Flask para WebTDC2016SP - Flask para Web
TDC2016SP - Flask para Webtdc-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

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-14sumitbardhan
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTSwapnil 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).pdfMustafaJutt4
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to AlgorithmEducation Front
 
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
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5Vishal 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
 
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
 
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
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 

More from Nilesh Dalvi

1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of JavaNilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cppNilesh Dalvi
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh 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

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 

Recently uploaded (20)

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.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