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());
}
}

array lecture engineeringinformatin_technology.pptx

  • 1.
  • 2.
     It isone 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: ArrayA 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 tostore 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 aretwo 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  Sequentialcollection 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 ofArray:  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 ofArray:  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 ofArray:  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: publicint Search(int key) { int index = -1;  for (int i = 0; i < length; i++) { if (items[i] == key)  { index = i; break; } } return index; }
  • 12.
     Appending toArray: 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 tothe 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 toArray:  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: Arraysize = 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:  publicvoid 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 sizedoes 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 classArrayADT { 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 intospecific 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 fromspecific 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 voidmain(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 valueto 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 specificindex-------------*/ 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()); } }