Mr. Muhammad Umer
Department of Computer Science,
KFUEIT
Data Structures & Algorithms
[COSC-2101]
Array in Data Structure
Array: is a container which can hold fix number of items and these
items should be of same type.
Alternative Definition:
Array: is collection of variables of same type that are referenced by a
common name
Most of the data structure make use of array to implement their
algorithms.
Lowest address of array corresponds to first element.
Highest address of array corresponds to last element.
Introduction
Following are important terms to understand the concepts of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index
which is used to identify the element.
Array Representation
Arrays can be declared in various ways
in different languages. For illustration,
let's take JAVA array declaration.
Types of Array Structures
Single Dimension Array:
Elements specified by single subscript.
type array_name [size]
e.g int[] multiple = new int[10]; // 1D integer array with size 4
Two Dimensional Array:
An array of more than one dimension is known as a multi-dimensional array. Two of the most
common examples of multi-dimensional arrays are two and three-dimensional array, known as
2D and 3D array.
e.g int[][] multiples = new int[4][2]; // 2D integer array with 4 rows
and 2 columns
Basic Operations on Array
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − add an element at given index.
• Deletion − delete an element at given index.
• Search − search an element using given index or by value.
• Update − update an element at given index.
Insertion Operation
Insert operation is to insert one or more data elements into an array.
Based on the requirement, new element can be added at the
beginning, end or any given index of array.
Here, we see an algorithm of insertion operation.
Deletion Operation
Deletion refers to removing an existing element from the array and re-
organizing all elements of an array.
Here, we see an algorithm of deletion operation.
Search Operation
You can perform a search for array element based on its value or its
index.
Here, we see an algorithm of search operation.
Update Operation
Update operation refers to updating an existing element from the array
at a given index.
Here, we see an algorithm of update operation.
ARRAY IMPLEMENTATION OF LISTS LIST:
• A list is a sequential data structure, ie. a collection of items accessible
one after another beginning at the head and ending at the tail.
• It is a widely used data structure for applications which do not need
random access Addition and removals can be made at any position in
the list
ARRAY IMPLEMENTATION OF LISTS LIST:
A list can be implemented in two ways:
1. Array list
2. Linked list
JAVA ArrayList
• Java provides the ArrayList class, which can be used to store an
unlimited number of objects.
• ArrayList is known as a generic class with a generic type E.
• You can specify a concrete type to replace E when creating
• an ArrayList.
For example:
ArrayList <String> cities = new ArrayList<> ();
Comparison of JAVA Array & ArrayList
Applications of JAVA in Data Structure
• Arrays are used to implement mathematical vectors and matrices, as
well as other kinds of rectangular tables. Many databases, small and
large, consist of one-dimensional arrays whose elements are records.
• Arrays are used to implement other data structures, such as lists,
heaps, hash tables, deques, queues and stacks.
• One or more large arrays are sometimes used to emulate in-program
dynamic memory allocation, particularly memory pool allocation.
Historically, this has sometimes been the only way to allocate
"dynamic memory" portably.
Applications of JAVA in Data Structure
• Arrays can be used to determine partial or complete control flow in
programs, as a compact alternative to (otherwise repetitive) multiple
“if” statements. They are known in this context as control tables and
are used in conjunction with a purpose built interpreter whose
control flow is altered according to values contained in the array. The
array may contain subroutine pointers(or relative subroutine numbers
that can be acted upon by SWITCH statements) that direct the path of
the execution.
References
1. https://www.slideshare.net/mralfarra/5-array-list-data-structure-
course?qid=adea7ee5-e8a5-46ea-a479-
60fc69c22f1e&v=&b=&from_search=1
2. http://learntoprogramming.com/content/applications-arrays
3. https://www.tutorialspoint.com/data_structures_algorithms/
array_data_structure.htm
4. https://www.brainkart.com/article/Array-Implementation-of-Lists-
List_6969/

Lecture_2_Arrays in Data Structure - Copy.pptx

  • 1.
    Mr. Muhammad Umer Departmentof Computer Science, KFUEIT Data Structures & Algorithms [COSC-2101]
  • 2.
    Array in DataStructure Array: is a container which can hold fix number of items and these items should be of same type. Alternative Definition: Array: is collection of variables of same type that are referenced by a common name Most of the data structure make use of array to implement their algorithms. Lowest address of array corresponds to first element. Highest address of array corresponds to last element.
  • 3.
    Introduction Following are importantterms to understand the concepts of Array. • Element − Each item stored in an array is called an element. • Index − Each location of an element in an array has a numerical index which is used to identify the element. Array Representation Arrays can be declared in various ways in different languages. For illustration, let's take JAVA array declaration.
  • 4.
    Types of ArrayStructures Single Dimension Array: Elements specified by single subscript. type array_name [size] e.g int[] multiple = new int[10]; // 1D integer array with size 4 Two Dimensional Array: An array of more than one dimension is known as a multi-dimensional array. Two of the most common examples of multi-dimensional arrays are two and three-dimensional array, known as 2D and 3D array. e.g int[][] multiples = new int[4][2]; // 2D integer array with 4 rows and 2 columns
  • 5.
    Basic Operations onArray Following are the basic operations supported by an array. • Traverse − print all the array elements one by one. • Insertion − add an element at given index. • Deletion − delete an element at given index. • Search − search an element using given index or by value. • Update − update an element at given index.
  • 6.
    Insertion Operation Insert operationis to insert one or more data elements into an array. Based on the requirement, new element can be added at the beginning, end or any given index of array. Here, we see an algorithm of insertion operation.
  • 7.
    Deletion Operation Deletion refersto removing an existing element from the array and re- organizing all elements of an array. Here, we see an algorithm of deletion operation.
  • 8.
    Search Operation You canperform a search for array element based on its value or its index. Here, we see an algorithm of search operation.
  • 9.
    Update Operation Update operationrefers to updating an existing element from the array at a given index. Here, we see an algorithm of update operation.
  • 10.
    ARRAY IMPLEMENTATION OFLISTS LIST: • A list is a sequential data structure, ie. a collection of items accessible one after another beginning at the head and ending at the tail. • It is a widely used data structure for applications which do not need random access Addition and removals can be made at any position in the list
  • 11.
    ARRAY IMPLEMENTATION OFLISTS LIST: A list can be implemented in two ways: 1. Array list 2. Linked list
  • 12.
    JAVA ArrayList • Javaprovides the ArrayList class, which can be used to store an unlimited number of objects. • ArrayList is known as a generic class with a generic type E. • You can specify a concrete type to replace E when creating • an ArrayList. For example: ArrayList <String> cities = new ArrayList<> ();
  • 13.
    Comparison of JAVAArray & ArrayList
  • 14.
    Applications of JAVAin Data Structure • Arrays are used to implement mathematical vectors and matrices, as well as other kinds of rectangular tables. Many databases, small and large, consist of one-dimensional arrays whose elements are records. • Arrays are used to implement other data structures, such as lists, heaps, hash tables, deques, queues and stacks. • One or more large arrays are sometimes used to emulate in-program dynamic memory allocation, particularly memory pool allocation. Historically, this has sometimes been the only way to allocate "dynamic memory" portably.
  • 15.
    Applications of JAVAin Data Structure • Arrays can be used to determine partial or complete control flow in programs, as a compact alternative to (otherwise repetitive) multiple “if” statements. They are known in this context as control tables and are used in conjunction with a purpose built interpreter whose control flow is altered according to values contained in the array. The array may contain subroutine pointers(or relative subroutine numbers that can be acted upon by SWITCH statements) that direct the path of the execution.
  • 16.
    References 1. https://www.slideshare.net/mralfarra/5-array-list-data-structure- course?qid=adea7ee5-e8a5-46ea-a479- 60fc69c22f1e&v=&b=&from_search=1 2. http://learntoprogramming.com/content/applications-arrays 3.https://www.tutorialspoint.com/data_structures_algorithms/ array_data_structure.htm 4. https://www.brainkart.com/article/Array-Implementation-of-Lists- List_6969/