SlideShare a Scribd company logo
using Java
2015
Data Structure
Prepared by: Mahmoud Rafeek Al-farra
in Java
3. Array Operations
mfarra.cst.ps www.fb.com/MahmoudRFarra
Contents
Complexity
Insert in array (index, in order)
Delete from array (index, value)
Revision
Introduction
mfarra.cst.ps www.fb.com/MahmoudRFarra
Array’s
Operations
Insert
At suitable
position
At index
Delete
Specific
value
Value of
index
Insert value at index
mfarra.cst.ps www.fb.com/MahmoudRFarra
2 7 80 88 90 999 12 15 23 30 45 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0
13
14
To insert value at index 4 (5’th position)
2 7 80 88 90 999 12 15 23 30 45 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0 14
13
Insert value at index
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void insertValue(int[] arr, int value, int index)
2. {
3. for (int i = arr.Length - 1; i > index; i--)
4. arr[i] = arr[i - 1];
5. arr[index] = value;
6. }
In the previous code:
if the loop start from the wanted index up to the length of array,
a logical error will be appeared, (What is it ?)
Insert value at suitable position
mfarra.cst.ps www.fb.com/MahmoudRFarra
2 7 80 88 90 999 12 15 23 30 45 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0
15
14
To insert the value at suitable position (in order)
2 7 80 88 90 999 12 15 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0 14
1 2 3 4 5 6 7 8 9 9
array
0
2 7 80 88 90 999 12 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
99
14
Null
1. Search for the index of value 15.
2. Shift the value to the left (to free the wanted position)
3. Insert the new value (15)
Insert value at suitable position
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void insertValueInOrder(int[] arr, int value)
2. {
3. // to find the position of value
4. int i;
5. for ( i = 0; i < arr.Length-1; i++)
6. {
7. if (id[i] > value)
8. {
9. break;
10. }
11. }
12. // shift value to free wanted position
13. for (int j = arr.Length - 1; j > i; j--)
14. arr[j] = arr[j - 1];
15. arr[i] = value;
16. }
Self Study:
What about, if
all the values in
array is smaller
than the new
value?
Delete value from index
mfarra.cst.ps www.fb.com/MahmoudRFarra
2 7 80 88 90 999 12 15 23 30 45 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0
15
14
To delete the value of 5’th cell
2 7 80 88 90 999 12 15 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0 14
1 2 3 4 5 6 7 8 9 9
array
0
2 7 80 88 90 999 12 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
99
14
Null
Delete value from index
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void deleteValue(int[] arr, int index)
2. {
3. // overwrite on position of value
4. for (int k =index ; k < id.Length-1; k++)
5. id[k] = id[k + 1];
6. }
To delete the value
1. Search for the index of value 15.
2. Overwrite on the index of value from it the last position.
Delete value
mfarra.cst.ps www.fb.com/MahmoudRFarra
2 7 80 88 90 999 12 15 23 30 45 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0
15
14
2 7 80 88 90 999 12 15 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0 14
1 2 3 4 5 6 7 8 9 9
array
0
2 7 80 88 90 999 12 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
99
14
Null
Delete value
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void deleteValue(int[] arr, int value)
2. {
3. // to find the position of value
4. int i;
5. for ( i = 0; i < arr.Length-1; i++)
6. {
7. if (id[i] == 12)
8. {
9. break;
10. }
11. }
12. // overwrite on position of value
13. for (int k =i ; k< id.Length-1; k++)
14. id[k] = id[k + 1];
15. }
Self Study:
What is the
result, if the
wanted value
does not found
in array ?
Time Complexity
mfarra.cst.ps www.fb.com/MahmoudRFarra
 To insert on value in specific index in array with size
(n), we need to shift all values of array (worst state),
this mean the time complexity is O(n).
 What about delete ?
Time Complexity
mfarra.cst.ps www.fb.com/MahmoudRFarra
 To insert on value in suitable index in array with size
(n), we need to:
1. Search for the suitable position by linear approach
(O(n)).
2. Shift all values of array (worst state), this mean the
time complexity is O(n).
 The total complexity O(2n).
 What about delete ?
using Java
2015
FB: M a h m o u d R F a r r a
YouTube: M a h m o u d R F a r
SlidesShare: mralfarra
Thank you

More Related Content

What's hot

Chapter 5: linked list data structure
Chapter 5: linked list data structureChapter 5: linked list data structure
Chapter 5: linked list data structure
Mahmoud Alfarra
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structureChapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
Mahmoud Alfarra
 
Chapter 6: stack data structure
Chapter 6:  stack data structureChapter 6:  stack data structure
Chapter 6: stack data structure
Mahmoud Alfarra
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
Selvin Josy Bai Somu
 
Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
Mahmoud Alfarra
 
Queue
QueueQueue
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Chapter1 intro toprincipleofc#_datastructure_b_cs
Chapter1  intro toprincipleofc#_datastructure_b_csChapter1  intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
Mahmoud Alfarra
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
Ravi Shetye
 
Queue
QueueQueue
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
Kuber Chandra
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Priority queues
Priority queuesPriority queues
Priority queues
Priyanka Rana
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
SAGARDAVE29
 

What's hot (20)

Chapter 5: linked list data structure
Chapter 5: linked list data structureChapter 5: linked list data structure
Chapter 5: linked list data structure
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structureChapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
 
Chapter 6: stack data structure
Chapter 6:  stack data structureChapter 6:  stack data structure
Chapter 6: stack data structure
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
 
Queue
QueueQueue
Queue
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Team 6
Team 6Team 6
Team 6
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Chapter1 intro toprincipleofc#_datastructure_b_cs
Chapter1  intro toprincipleofc#_datastructure_b_csChapter1  intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
 
Queue
QueueQueue
Queue
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Priority queues
Priority queuesPriority queues
Priority queues
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 

Viewers also liked

Array operations
Array operationsArray operations
Array operations
ZAFAR444
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
Khirulnizam Abd Rahman
 
01 05 - introduction xml
01  05 - introduction xml01  05 - introduction xml
01 05 - introduction xml
Siva Kumar reddy Vasipally
 
linked list
linked listlinked list
linked list
Abbott
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1Kumar
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Sriram Raj
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
Mahmoud Alfarra
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)
Adam Mukharil Bachtiar
 
Data structures
Data structuresData structures
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
Rai University
 
1 intro of data structure course
1  intro of data structure course1  intro of data structure course
1 intro of data structure course
Mahmoud Alfarra
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
Mahmoud Alfarra
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
Mahmoud Alfarra
 
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
Array data structure
Array data structureArray data structure
Array data structure
maamir farooq
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Hassan Ahmed
 

Viewers also liked (20)

Array operations
Array operationsArray operations
Array operations
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
 
L6 structure
L6 structureL6 structure
L6 structure
 
01 05 - introduction xml
01  05 - introduction xml01  05 - introduction xml
01 05 - introduction xml
 
linked list
linked listlinked list
linked list
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)
 
Data structures
Data structuresData structures
Data structures
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
1 intro of data structure course
1  intro of data structure course1  intro of data structure course
1 intro of data structure course
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Array ppt
Array pptArray ppt
Array ppt
 
Array data structure
Array data structureArray data structure
Array data structure
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 

Similar to 3 Array operations

C arrays
C arraysC arrays
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
Syed Mustafa
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
NiravPanchal50
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Getachew Ganfur
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++vidyamittal
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
AkhilaaReddy
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
05 queues
05 queues05 queues
05 queues
Rajan Gautam
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
RenalthaPujaBagaskar
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
SushantRaj25
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
KamalAlbashiri
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
shafat6712
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 

Similar to 3 Array operations (20)

C arrays
C arraysC arrays
C arrays
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Chapter 6 arrays part-1
Chapter 6   arrays part-1Chapter 6   arrays part-1
Chapter 6 arrays part-1
 
Unit 3
Unit 3 Unit 3
Unit 3
 
05 queues
05 queues05 queues
05 queues
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Vcs16
Vcs16Vcs16
Vcs16
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Grid gain paper
Grid gain paperGrid gain paper
Grid gain paper
 
Unit 3
Unit 3 Unit 3
Unit 3
 

More from Mahmoud Alfarra

Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
3 classification
3  classification3  classification
3 classification
Mahmoud Alfarra
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
2 programming-using-java how to built application
2 programming-using-java how to built application2 programming-using-java how to built application
2 programming-using-java how to built application
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Mahmoud Alfarra
 

More from Mahmoud Alfarra (20)

Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
 
3 classification
3  classification3  classification
3 classification
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
 
2 programming-using-java how to built application
2 programming-using-java how to built application2 programming-using-java how to built application
2 programming-using-java how to built application
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
 
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
 

Recently uploaded

Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
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
 
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
 
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
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
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
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
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
 
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
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
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
 
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
 

Recently uploaded (20)

Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.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
 
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
 
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
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
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
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
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...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
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
 
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 ...
 

3 Array operations

  • 1. using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 3. Array Operations
  • 2. mfarra.cst.ps www.fb.com/MahmoudRFarra Contents Complexity Insert in array (index, in order) Delete from array (index, value) Revision
  • 4. Insert value at index mfarra.cst.ps www.fb.com/MahmoudRFarra 2 7 80 88 90 999 12 15 23 30 45 70 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 13 14 To insert value at index 4 (5’th position) 2 7 80 88 90 999 12 15 23 30 45 70 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 14 13
  • 5. Insert value at index mfarra.cst.ps www.fb.com/MahmoudRFarra 1. public void insertValue(int[] arr, int value, int index) 2. { 3. for (int i = arr.Length - 1; i > index; i--) 4. arr[i] = arr[i - 1]; 5. arr[index] = value; 6. } In the previous code: if the loop start from the wanted index up to the length of array, a logical error will be appeared, (What is it ?)
  • 6. Insert value at suitable position mfarra.cst.ps www.fb.com/MahmoudRFarra 2 7 80 88 90 999 12 15 23 30 45 70 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 15 14 To insert the value at suitable position (in order) 2 7 80 88 90 999 12 15 23 30 45 15 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 14 1 2 3 4 5 6 7 8 9 9 array 0 2 7 80 88 90 999 12 23 30 45 15 77 1 2 3 4 5 6 7 8 9 10 11 12 13 99 14 Null 1. Search for the index of value 15. 2. Shift the value to the left (to free the wanted position) 3. Insert the new value (15)
  • 7. Insert value at suitable position mfarra.cst.ps www.fb.com/MahmoudRFarra 1. public void insertValueInOrder(int[] arr, int value) 2. { 3. // to find the position of value 4. int i; 5. for ( i = 0; i < arr.Length-1; i++) 6. { 7. if (id[i] > value) 8. { 9. break; 10. } 11. } 12. // shift value to free wanted position 13. for (int j = arr.Length - 1; j > i; j--) 14. arr[j] = arr[j - 1]; 15. arr[i] = value; 16. } Self Study: What about, if all the values in array is smaller than the new value?
  • 8. Delete value from index mfarra.cst.ps www.fb.com/MahmoudRFarra 2 7 80 88 90 999 12 15 23 30 45 70 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 15 14 To delete the value of 5’th cell 2 7 80 88 90 999 12 15 23 30 45 15 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 14 1 2 3 4 5 6 7 8 9 9 array 0 2 7 80 88 90 999 12 23 30 45 15 77 1 2 3 4 5 6 7 8 9 10 11 12 13 99 14 Null
  • 9. Delete value from index mfarra.cst.ps www.fb.com/MahmoudRFarra 1. public void deleteValue(int[] arr, int index) 2. { 3. // overwrite on position of value 4. for (int k =index ; k < id.Length-1; k++) 5. id[k] = id[k + 1]; 6. }
  • 10. To delete the value 1. Search for the index of value 15. 2. Overwrite on the index of value from it the last position. Delete value mfarra.cst.ps www.fb.com/MahmoudRFarra 2 7 80 88 90 999 12 15 23 30 45 70 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 15 14 2 7 80 88 90 999 12 15 23 30 45 15 77 1 2 3 4 5 6 7 8 9 10 11 12 13 array 0 14 1 2 3 4 5 6 7 8 9 9 array 0 2 7 80 88 90 999 12 23 30 45 15 77 1 2 3 4 5 6 7 8 9 10 11 12 13 99 14 Null
  • 11. Delete value mfarra.cst.ps www.fb.com/MahmoudRFarra 1. public void deleteValue(int[] arr, int value) 2. { 3. // to find the position of value 4. int i; 5. for ( i = 0; i < arr.Length-1; i++) 6. { 7. if (id[i] == 12) 8. { 9. break; 10. } 11. } 12. // overwrite on position of value 13. for (int k =i ; k< id.Length-1; k++) 14. id[k] = id[k + 1]; 15. } Self Study: What is the result, if the wanted value does not found in array ?
  • 12. Time Complexity mfarra.cst.ps www.fb.com/MahmoudRFarra  To insert on value in specific index in array with size (n), we need to shift all values of array (worst state), this mean the time complexity is O(n).  What about delete ?
  • 13. Time Complexity mfarra.cst.ps www.fb.com/MahmoudRFarra  To insert on value in suitable index in array with size (n), we need to: 1. Search for the suitable position by linear approach (O(n)). 2. Shift all values of array (worst state), this mean the time complexity is O(n).  The total complexity O(2n).  What about delete ?
  • 14. using Java 2015 FB: M a h m o u d R F a r r a YouTube: M a h m o u d R F a r SlidesShare: mralfarra Thank you