SlideShare a Scribd company logo
DATA STRUCTURE &
ALGORITHMS
 It is one of the most important and well-known data
structures, and it is built and available in all programming
languages.
 It has Sequential collection of elements of the same type.
 It has Fixed size.
 Array data structure simply means that it is a storage format
of the data in the memory in which the data are arranged in
contiguous blocks of memory.
 Ex: Array A with 4
elements:
 A[4]
 A=[ 2, 3 ,4 ,5 ]
 A[0]=2 , A[1]=3 , A[3]=4 , A[4]=5
2 3 4 5
0 3
1 2
Not Real
Address
 Ex: Array A with 4
elements:
 A[4]
 A=[ 2, 3 ,4 ,5 ]
 A[0]=2 , A[1]=3 , A[3]=4 ,
A[4]=5
2 3 4 5
0 3
1 2
Not Real
Address
Memor
y
103
102
101
100
Real
Address
5
4
3
2
 How to store array in memory:
1. You need to store an entire block the size of the array.
2. Sequential storage method.
3. The access method is random, depending on the access
equation.
 Access equation for each element:
Loc ( A [ i ] ) = Real Address + (i *size) where: Real Address
is starting
address and size is element size.
Ex: Loc ( A [ 3 ] ) = 100 + (3 *1) = 103 = 5
 There are two ways to represent Arrays data structure in memory:
• Static memory allocation
• Dynamic memory allocation
Question
What are the differences between Static and Dynamic memory
allocation?
 Traversing : accessing/visiting each data element exactly once so that
certain items in the data may be processed.
 Searching : finding the location of a given data element (key) in
the structure.
 Insertion: adding new value in the structure.
 Deletion: deleting value in the structure.
 Sorting: arrange the data either in ascending or descending order.
 Merging: merge two structures of the same type into one.
 Data
 Sequential collection of elements of the same type.
 Operations :
 The possible operations on arrays data structure
are:
1) Creating 2) Filling 3) Traversing (Displaying)
4) Appending 5) Searching 6) Insertion
7) Deletion 8) getLength 9) getSize
10) Sorting 11) Merging 12) ) Enlarge
 Creation of Array:
 public class MyClass {
 private static int SIZE;
 private static int length = 0;
 private static int[] items = new int[SIZE];
 }
public class Array {
private int SIZE;
public Array(int arrsize)
{ this.SIZE = arrsize; } }
Item
s
 Filling of Array:
 public void Fill() {
 System.out.println("how many items want to fill?");
 int no_of_items = scanner.nextInt();
 if (no_of_items > SIZE)
 {
 System.out.println("you cannot exceed the array size");
 return;
 }
 else { for (int i = 0; i < no_of_items; i++)
 {
 System.out.println("Enter item no" + i);
 items[i] = scanner.nextInt();
 length++;
 } } }
 Display of Array:
 public void Display()
 {
 System.out.println(" Display array content");
 for (int i = 0; i < length; i++)
 {
 System.out.print(items[i] + "t");
 }
 }
Search in Array:
public int Search(int key)
{
int index = -1;
 for (int i = 0; i < length; i++)
{
if (items[i] == key)
 {
index = i;
break;
}
}
return index;
}
 Appending to Array:
public void append(int newItem) {
if (length < SIZE) {
items[length] = newItem;
length++;
System.out.println("Item " + newItem + " appended to the
array.");
} else {
System.out.println("Array is full");
}
}
 Inserting to the
Array:
Array size = 7
Length = 5
Insert (3,90)
Items[3] = 90
0 1 2 3 4 5 6
20 30 40 50 60
0 1 2 3 4 5 6
20 30 40 50 60
Insert (3,90)
Shift
right
for (int i = length; i > index; i--)
{
items[i] = items[i - 1];
}
items[index] = newitem;
length++;
Insert
(3,90)
 Inserting to Array:
 public void insert(int index, int newItem) {
 if (index >= 0 && index < size) {
 for (int i = length; i > index; i--) {
 items[i] = items[i - 1];
 }
 items[index] = newItem;
 length++;
 }
 else
 {
 System.out.println("error index out of range");
 }
 }
 Deleting from
Array:
Array size = 7
Length = 5
Delete (3)
Item[3] =
null
0 1 2 3 4 5 6
20 30 40 50 60 70
0 1 2 3 4 5 6
20 30 40 60 70
Delete (3)=50
Shift
left
for(inti=index;i<length-1;i++)
{
items[i]=items[i+1];
}
length--;
Delete (3)

Deleting from Array:

public void delete(int index) {

if (index >= 0 && index < SIZE)

{

for (int i = index; i < length - 1; i++)

{

items[i] = items[i + 1];

}

length--;

}

else

{

System.out.println("Index out of Array Range");

}

}
 Fixed size does not change when needed.
 The process of inserting and deleting from the middle of the arrays is
very expensive, take long time and this forced us to use linked lists data
structure instead of arrays.
Question
What are the defects of Arrays Data Structures?
import java.util.Scanner;
public class ArrayADT {
static int SIZE; // The size of the array
static int length = 0; // The current number of elements in the array
static int[] items; // Array declaration
/*--------Create dynamic array-------------------*/
public static void createArray(int arrsize) {
SIZE = arrsize;
items = new int[SIZE]; // Allocate memory for the array
}
/* ----------------Fill Operation---------------- */
public static void fill() {
Scanner scanner = new Scanner(System.in);
System.out.println("How many items do you want to fill?");
int no_of_items = scanner.nextInt();
if (no_of_items > SIZE) {
System.out.println("You cannot exceed the array size.");
return;
}
for (int i = 0; i < no_of_items; i++) {
System.out.println("Enter item no " + i + ":");
items[i] = scanner.nextInt();
length++;
}
}
/* ----------------Display Operation---------------- */
public static void display() {
System.out.println("Displaying array content:");
for (int i = 0; i < length; i++) {
System.out.print(items[i] + "t");
}
System.out.println();
}
/* ----------------Get size Operation---------------- */
public static int getSize() {
return SIZE;
}
/* ----------------Get length Operation---------------- */
public static int getLength() {
return length;
}
/* ----------------Search Operation---------------- */
public static int search(int key) {
for (int i = 0; i < length; i++) {
if (items[i] == key) {
return i;
}
}
return -1;
}
/*-------------------Append Operation---------------------*/
public static void append(int newitem) {
if (length < SIZE) {
items[length] = newitem;
length++;
} else {
System.out.println("Array is full.");
}
}
/*-------Insert operation into specific index----------*/
public static void insert(int index, int newitem) {
if (index >= 0 && index < SIZE) {
for (int i = length; i > index; i--) {
items[i] = items[i - 1];
}
items[index] = newitem;
length++;
} else {
System.out.println("Error - Index out of
Range.");
}
}
/*-------Delete operation from specific index----------*/
public static void delete(int index) {
if (index >= 0 && index < length) {
for (int i = index; i < length - 1; i++) {
items[i] = items[i + 1];
}
length--;
} else {
System.out.println("Index out of Array Range.");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Hello, this is Array ADT.");
System.out.println("Enter the Array Size:");
int arraysize = scanner.nextInt();
createArray(arraysize);
fill();
System.out.println("Array size = " + getSize() + " while length = " + getLength());
display();
/*------------------Search------------------------*/
System.out.println("Enter the value to search for:");
int key = scanner.nextInt();
int index = search(key);
if (index == -1) {
System.out.println("Item not found.");
} else {
System.out.println("Item found @ position " + index);
}
/*-------------------Append---------------------------*/
System.out.println("Enter new item to add to the array:");
int newitem = scanner.nextInt();
append(newitem);
display();
/*--------------Insert into specific index-------------*/
System.out.println("Enter index to insert the
item:");
index = scanner.nextInt();
System.out.println("Enter new item to insert at
index:");
newitem = scanner.nextInt();
insert(index, newitem);
display();
System.out.println("Array size = " + getSize() + "
while length = " + getLength());
/*--------------Delete from specific index-------------*/
System.out.println("Enter index to delete its
item:");
index = scanner.nextInt();
delete(index);
display();
System.out.println("Array size = " + getSize() + "
while length = " + getLength());
}
}

More Related Content

PDF
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
PPT
java detailed aadvanced jaavaaaaa2-3.ppt
PPTX
Tophcjdjjdjsjssjjdkdkdkfkfkdfkdic-2.pptx
PDF
Sec 06 Basics Data Structure & Array Implementation .pdf
PPTX
Data Structures - Array presentation .pptx
PDF
M v bramhananda reddy dsa complete notes
PDF
Data Structures Notes 2021
PPT
1st lecture of DSA computer science 2024.ppt
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
java detailed aadvanced jaavaaaaa2-3.ppt
Tophcjdjjdjsjssjjdkdkdkfkfkdfkdic-2.pptx
Sec 06 Basics Data Structure & Array Implementation .pdf
Data Structures - Array presentation .pptx
M v bramhananda reddy dsa complete notes
Data Structures Notes 2021
1st lecture of DSA computer science 2024.ppt

Similar to array lecture engineeringinformatin_technology.pptx (20)

PPTX
DS Module1 (1).pptx
PDF
java I am trying to run my code but it is not letting me .pdf
PPT
1st lecture.ppt
PPTX
introduction of Data strutter and algirithm.pptx
PPTX
CPCS204-02-03-Arrays - Overview - OperationsAnalysing.pptx
PPTX
data structure unit -1_170434dd7400.pptx
PPTX
Tips On The AP FR 21 How to handle AP Review
PDF
PPT
Data structure and algorithm with java by shikra
PDF
Aj unit2 notesjavadatastructures
PPTX
Collection and framework
PPTX
Arrays and Strings engineering education
PPSX
Lecture 1
PPTX
Introduction to Data Structures and their importance
PPTX
2. Array in Data Structure
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
PPT
Arrays in JAVA.ppt
PPTX
A singly linked list is a linear data structure
PPTX
Data structures in c#
DOCX
Array list
DS Module1 (1).pptx
java I am trying to run my code but it is not letting me .pdf
1st lecture.ppt
introduction of Data strutter and algirithm.pptx
CPCS204-02-03-Arrays - Overview - OperationsAnalysing.pptx
data structure unit -1_170434dd7400.pptx
Tips On The AP FR 21 How to handle AP Review
Data structure and algorithm with java by shikra
Aj unit2 notesjavadatastructures
Collection and framework
Arrays and Strings engineering education
Lecture 1
Introduction to Data Structures and their importance
2. Array in Data Structure
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
Arrays in JAVA.ppt
A singly linked list is a linear data structure
Data structures in c#
Array list
Ad

Recently uploaded (20)

PDF
Understanding Forklifts - TECH EHS Solution
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Complete React Javascript Course Syllabus.pdf
PDF
AI in Product Development-omnex systems
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
System and Network Administraation Chapter 3
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Online Work Permit System for Fast Permit Processing
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Digital Strategies for Manufacturing Companies
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
Understanding Forklifts - TECH EHS Solution
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Complete React Javascript Course Syllabus.pdf
AI in Product Development-omnex systems
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 41
System and Network Administraation Chapter 3
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Online Work Permit System for Fast Permit Processing
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
How Creative Agencies Leverage Project Management Software.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Digital Strategies for Manufacturing Companies
Upgrade and Innovation Strategies for SAP ERP Customers
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
2025 Textile ERP Trends: SAP, Odoo & Oracle
The Five Best AI Cover Tools in 2025.docx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
How to Choose the Right IT Partner for Your Business in Malaysia
Ad

array lecture engineeringinformatin_technology.pptx

  • 2.  It is one of the most important and well-known data structures, and it is built and available in all programming languages.  It has Sequential collection of elements of the same type.  It has Fixed size.  Array data structure simply means that it is a storage format of the data in the memory in which the data are arranged in contiguous blocks of memory.  Ex: Array A with 4 elements:  A[4]  A=[ 2, 3 ,4 ,5 ]  A[0]=2 , A[1]=3 , A[3]=4 , A[4]=5 2 3 4 5 0 3 1 2 Not Real Address
  • 3.  Ex: Array A with 4 elements:  A[4]  A=[ 2, 3 ,4 ,5 ]  A[0]=2 , A[1]=3 , A[3]=4 , A[4]=5 2 3 4 5 0 3 1 2 Not Real Address Memor y 103 102 101 100 Real Address 5 4 3 2
  • 4.  How to store array in memory: 1. You need to store an entire block the size of the array. 2. Sequential storage method. 3. The access method is random, depending on the access equation.  Access equation for each element: Loc ( A [ i ] ) = Real Address + (i *size) where: Real Address is starting address and size is element size. Ex: Loc ( A [ 3 ] ) = 100 + (3 *1) = 103 = 5
  • 5.  There are two ways to represent Arrays data structure in memory: • Static memory allocation • Dynamic memory allocation Question What are the differences between Static and Dynamic memory allocation?
  • 6.  Traversing : accessing/visiting each data element exactly once so that certain items in the data may be processed.  Searching : finding the location of a given data element (key) in the structure.  Insertion: adding new value in the structure.  Deletion: deleting value in the structure.  Sorting: arrange the data either in ascending or descending order.  Merging: merge two structures of the same type into one.
  • 7.  Data  Sequential collection of elements of the same type.  Operations :  The possible operations on arrays data structure are: 1) Creating 2) Filling 3) Traversing (Displaying) 4) Appending 5) Searching 6) Insertion 7) Deletion 8) getLength 9) getSize 10) Sorting 11) Merging 12) ) Enlarge
  • 8.  Creation of Array:  public class MyClass {  private static int SIZE;  private static int length = 0;  private static int[] items = new int[SIZE];  } public class Array { private int SIZE; public Array(int arrsize) { this.SIZE = arrsize; } } Item s
  • 9.  Filling of Array:  public void Fill() {  System.out.println("how many items want to fill?");  int no_of_items = scanner.nextInt();  if (no_of_items > SIZE)  {  System.out.println("you cannot exceed the array size");  return;  }  else { for (int i = 0; i < no_of_items; i++)  {  System.out.println("Enter item no" + i);  items[i] = scanner.nextInt();  length++;  } } }
  • 10.  Display of Array:  public void Display()  {  System.out.println(" Display array content");  for (int i = 0; i < length; i++)  {  System.out.print(items[i] + "t");  }  }
  • 11. Search in Array: public int Search(int key) { int index = -1;  for (int i = 0; i < length; i++) { if (items[i] == key)  { index = i; break; } } return index; }
  • 12.  Appending to Array: public void append(int newItem) { if (length < SIZE) { items[length] = newItem; length++; System.out.println("Item " + newItem + " appended to the array."); } else { System.out.println("Array is full"); } }
  • 13.  Inserting to the Array: Array size = 7 Length = 5 Insert (3,90) Items[3] = 90 0 1 2 3 4 5 6 20 30 40 50 60 0 1 2 3 4 5 6 20 30 40 50 60 Insert (3,90) Shift right for (int i = length; i > index; i--) { items[i] = items[i - 1]; } items[index] = newitem; length++; Insert (3,90)
  • 14.  Inserting to Array:  public void insert(int index, int newItem) {  if (index >= 0 && index < size) {  for (int i = length; i > index; i--) {  items[i] = items[i - 1];  }  items[index] = newItem;  length++;  }  else  {  System.out.println("error index out of range");  }  }
  • 15.  Deleting from Array: Array size = 7 Length = 5 Delete (3) Item[3] = null 0 1 2 3 4 5 6 20 30 40 50 60 70 0 1 2 3 4 5 6 20 30 40 60 70 Delete (3)=50 Shift left for(inti=index;i<length-1;i++) { items[i]=items[i+1]; } length--; Delete (3)
  • 16.  Deleting from Array:  public void delete(int index) {  if (index >= 0 && index < SIZE)  {  for (int i = index; i < length - 1; i++)  {  items[i] = items[i + 1];  }  length--;  }  else  {  System.out.println("Index out of Array Range");  }  }
  • 17.  Fixed size does not change when needed.  The process of inserting and deleting from the middle of the arrays is very expensive, take long time and this forced us to use linked lists data structure instead of arrays. Question What are the defects of Arrays Data Structures?
  • 18. import java.util.Scanner; public class ArrayADT { static int SIZE; // The size of the array static int length = 0; // The current number of elements in the array static int[] items; // Array declaration /*--------Create dynamic array-------------------*/ public static void createArray(int arrsize) { SIZE = arrsize; items = new int[SIZE]; // Allocate memory for the array }
  • 19. /* ----------------Fill Operation---------------- */ public static void fill() { Scanner scanner = new Scanner(System.in); System.out.println("How many items do you want to fill?"); int no_of_items = scanner.nextInt(); if (no_of_items > SIZE) { System.out.println("You cannot exceed the array size."); return; } for (int i = 0; i < no_of_items; i++) { System.out.println("Enter item no " + i + ":"); items[i] = scanner.nextInt(); length++; } }
  • 20. /* ----------------Display Operation---------------- */ public static void display() { System.out.println("Displaying array content:"); for (int i = 0; i < length; i++) { System.out.print(items[i] + "t"); } System.out.println(); } /* ----------------Get size Operation---------------- */ public static int getSize() { return SIZE; } /* ----------------Get length Operation---------------- */ public static int getLength() { return length; }
  • 21. /* ----------------Search Operation---------------- */ public static int search(int key) { for (int i = 0; i < length; i++) { if (items[i] == key) { return i; } } return -1; } /*-------------------Append Operation---------------------*/ public static void append(int newitem) { if (length < SIZE) { items[length] = newitem; length++; } else { System.out.println("Array is full."); } }
  • 22. /*-------Insert operation into specific index----------*/ public static void insert(int index, int newitem) { if (index >= 0 && index < SIZE) { for (int i = length; i > index; i--) { items[i] = items[i - 1]; } items[index] = newitem; length++; } else { System.out.println("Error - Index out of Range."); } }
  • 23. /*-------Delete operation from specific index----------*/ public static void delete(int index) { if (index >= 0 && index < length) { for (int i = index; i < length - 1; i++) { items[i] = items[i + 1]; } length--; } else { System.out.println("Index out of Array Range."); } }
  • 24. public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Hello, this is Array ADT."); System.out.println("Enter the Array Size:"); int arraysize = scanner.nextInt(); createArray(arraysize); fill(); System.out.println("Array size = " + getSize() + " while length = " + getLength()); display();
  • 25. /*------------------Search------------------------*/ System.out.println("Enter the value to search for:"); int key = scanner.nextInt(); int index = search(key); if (index == -1) { System.out.println("Item not found."); } else { System.out.println("Item found @ position " + index); } /*-------------------Append---------------------------*/ System.out.println("Enter new item to add to the array:"); int newitem = scanner.nextInt(); append(newitem); display();
  • 26. /*--------------Insert into specific index-------------*/ System.out.println("Enter index to insert the item:"); index = scanner.nextInt(); System.out.println("Enter new item to insert at index:"); newitem = scanner.nextInt(); insert(index, newitem); display(); System.out.println("Array size = " + getSize() + " while length = " + getLength()); /*--------------Delete from specific index-------------*/ System.out.println("Enter index to delete its item:"); index = scanner.nextInt(); delete(index); display(); System.out.println("Array size = " + getSize() + " while length = " + getLength()); } }