Data Structures and Algorithms Lecture (4): Arrays PDF
Document Details
Uploaded by StylishSpessartine
University of Science and Technology
2024
Prof. Noureldien A. Noureldien
Tags
Related
- Data Structures and Algorithms with JavaScript PDF
- Data Structures and Algorithms - Simplified Notes PDF
- University of Science and Technology Data Structures and Algorithms Lab Manual PDF
- Data Structures & Algorithms PDF
- Data Structures and Algorithms Lecture (8): Queues - University of Science and Technology
- Python Data Structures Tutorial PDF
Summary
This document is a lecture on data structures and algorithms focusing on the concept of arrays. It explains how to work with arrays, including one-dimensional and multi-dimensional arrays, and details operations like traversal, insertion, and deletion. It's intended for university-level computer science students.
Full Transcript
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Data Structures and Algorithms** **Lecture (4): Arrays** **Prof. Noureldien A. Noureldien** **4.1 Introduction** **Ma**ny applications require the processing of multiple data items that have c...
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Data Structures and Algorithms** **Lecture (4): Arrays** **Prof. Noureldien A. Noureldien** **4.1 Introduction** **Ma**ny applications require the processing of multiple data items that have common characteristics. For example, let say we have to find the average marks obtained in a subject by students of a class. In this case, we have a couple of choices. Either we can store all the values in different variables or we can create an array that will hold all these values in contiguous memory location. If they are stored in different variables and the number of such variables is quite large then it is quite unmanageable as these variables are scattered throughout the memory. In such situations, it is often convenient to place the data items into an array, where they will all share the same name. **4.2 Arrays** **Definition: An array is a collection of the same type of data items, which are stored in consecutive memory locations under a common name.** An array variable must be followed by square bracket enclosing size specification of the array. The size signifies the number of elements of the array. That number should be a positive integer number (greater than 0). **4.2.1 One-dimensional Array** **A one-dimensional array is one in which only one subscript is needed to specify a particular element of the array.** **4.2.2. Declaration of One-Dimensional Array** The one-dimensional array can be declared as follows: ***data\_type array\_name \[size\];*** where array\_name is the name of an array and size is the number of elements of data type data\_type and size must be an integer constant specified within a pair of square brackets. Example: ***int a\[10\];*** **[In C, arrays are zero-based, that means the index of an array starts from zero]**. The ten elements of a 10-element array are numbered from 0 to 9. The subscript, which specifies a single element of an array, is simply an integer expression in square brackets. The first element of the array is a\[0\], the second element is a\[1\], etc. ***Example:*** a\[0\] = 10; a\[1\] = 20; a\[2\] = a\[0\] + a\[1\]; There are some important points about arrays. These are as follows: Arrays are always stored in consecutive memory locations. An array can store multiple values that can be referenced by a single name. An array name is actually an address of the first element of the array. An array can be either an integer, character or floating-point data type can be initialized only during declaration. In C, array index starts from zero. There is no **bound checking** concept for array. **4.2.3. Example 1** ![](media/image2.png) **4.3 Multi-Dimensional Arrays** Multi--dimensional array is nothing but Array of Arrays. In fact, a two-dimensional array is an array of one-dimensional array. A three-dimensional array is an array of two-dimensional arrays. Similarly, any d dimensional array is an array of (d-1) dimensional arrays. Generally, when we talk about multi-dimensional arrays, we generally talk of two-dimensional arrays. Since more than two-dimensional is rarely needed. \` There is a need to store and manipulate two-dimensional data structure such as matrices and tables. A two-dimensional array is one in which two subscript specifications are needed to specify a particular element of the array. Here the array has two subscripts. One subscript denotes the row and the other the column. **4.3.1 Declaration of Two-Dimensional Array** The declaration of two-dimensional arrays is as follows: ***data\_type array\_name \[row\_size\]\[column\_size\];*** ***Example:*** int m\[2\]\[3\]; Here m is declared as a matrix having 2 rows (numbered from 0 to 1) and 3 columns (numbered 0 through 2). The first element of the matrix is m \[0\]\[0\] and the last row last column is m\[1\]\[2\]. The two-dimensional array m\[2\]\[3\] is shown in figure 3.2. The first element of this array denoted by m \[0\]\[0\] and the second element is denoted by m \[0\]\[1\] and so on. ***Example:*** ![](media/image4.png) Therefore, the above code will read all the elements of the two-dimensional array m row by row. Similarly, the following code can be used to print all the elements of the two-dimensional array m in matrix form. ***Example:*** **4.4 Operations Perform on Arrays** Arrays are most important and commonly used data structures. They are also used to implement many other data structures. Almost all types of operations are performed on Arrays. ![](media/image6.png) **4.4.1 Traversing a Linear Array** The method of accessing and processing each element of the array exactly once is known as traversing. In an array, traversal beginning from the first element in the array and ends at the last element of the array. **4.4.2 Inserting an Element into a Linear Array** On array, insertion operation can be performed in different ways. Insert an element in an array can be done in the following ways: To a specific position into the unsorted array By value of the inserted element in the sorted array **4.4.2.1 Inserting an Element to a Specific Position** Now, to insert a key element to a specific position in a linear array, at first, it needed to move each element from the back up one position until getting the specified position. Then insert a key element into a given position. Finally, increment array size by one. ![](media/image8.png) ***Algorithm to insert an element to a specific position into a linear array*** ![](media/image10.png) **4.4.2.2. Inserting an Element in a sorted Array** While inserting an element into a sorted linear array, at first, it needs to find the correct position to insert the key element. When the position cannot be found, then insert at the end. Otherwise, move each element from the back up one position until getting to position into the insert. Then insert a key element into required position. Finally, the array size is incremented by one. ***Example:*** Insert 50 into a sorted array with 5 elements ***Algorithm to insert an element in the sorted Array*** ![](media/image12.png) **4.4.3 Deleting an element from a Linear Array** In the array, delete operation can be performed in different ways. Delete an element in an array can be done in the following ways: C From a specific position of the deleted element By value of the deleted element **4.4.3.1 Deleting an element from a specific position** To delete a key element from a specific position of a linear array, at first, it needed to move each element from one after the given position forward one position until end of the array. Finally, the array size is decrement by one. ***Example:*** Delete an element from 3rd position (i.e. 21) of the array with 6 elements. ![](media/image14.png) **4.4.3.2 Deleting an element by value** When deleting operation performed on the array by the value of the element, then it needed to search through the array to find its position. Now there are two ways to find out the position of the deleted element. In the case of the sorted array, we can use binary search to find the position; otherwise if the array is unsorted then perform a linear search to find the position. The following algorithm, deleting a key element from a specific position of an unsorted array. At first, it needed to search the array to find its position using linear searching. After getting the position, move each element from one after that position forward one position until end of the array. Finally, the array size is decremented by one. The following algorithm, deleting an element of the linear array **4.4.4 Merging** Merging is an operation of combining more than one sorted array together so that resulting array remains sorted. The merge algorithm used in the merge-sort algorithm, a comparison-based sorting algorithm. Merging of two sorted arrays, one can be done in linear time and space. Let, two sorted arrays, A of m elements and B of n elements merge to form a sorted array C of total m + n elements. The first element of array A is compared with the first element of array B. If the first element of array A is smaller than the first element of array B, the element from array A is moved to the new array C. The subscript of the array A is now increased since the first element is now set and we move on. Otherwise, the element from array B should be smaller; it is moved to the new array C. The subscript of array B is increased. This process of comparing the elements in the two arrays continues until either array A or array B is empty. When one array is empty, any elements remaining in the other (non-empty) array are appended to the end of array C and the merge is complete. ***Example:*** Merge two sorted arrays A of 6 elements and B of 5 elements to form a sorted array C of 11 elements ![](media/image16.png) ![](media/image18.png) ![](media/image20.png)