SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Destaque

Array operations
Array operationsArray operations
Array operationsZAFAR444
 
linked list
linked listlinked list
linked listAbbott
 
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 duttAnil 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 StructureSriram Raj
 
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
 
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 structureRai University
 
1 intro of data structure course
1  intro of data structure course1  intro of data structure course
1 intro of data structure courseMahmoud Alfarra
 
Array data structure
Array data structureArray data structure
Array data structuremaamir 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
 

Destaque (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...
 

Semelhante a 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
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01
 
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
 

Mais de 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 2Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using JavaMahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structureMahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structureMahmoud 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 structureMahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structureMahmoud 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 20102011Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011Mahmoud 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-tooop20102011Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011Mahmoud 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 computerMahmoud 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 applicationMahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introductionMahmoud 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 2Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1Mahmoud 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
 

Mais de 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)
 

Último

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Último (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

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