An array is a fixed-size sequenced collection of variables belonging to the same data types. The array has adjacent memory locations to store values. Since the array provides a convenient structure for representing data it falls under the category of the data structures in C.
The array can also be defined as a particular method of storing elements of indexed data. Elements of data are logically stored sequentially in blocks within the array. Each element is referenced by an index or subscripts. An index is usually a number used to address an element in the array.
The syntax for declaring array are:
Following are the important terminologies used for understanding the concepts of Arrays:
Element: Each and every item stored in an array is termed as an element.
Index: each memory location of an element in an array is denoted by a numerical index which is used for identifying the element.
Need of Arrays for building a specific data structure?
When a program works with many variables which hold comparable forms of data, then organizational and managerial difficulty quickly arise. If you are not using arrays, then the number of variables used will increase. Using the array, the number of variables reduces, i.e. you can use a single name for multiple values, just you need to deal with its index values (starting from 0 to n).
Operations performed on Array
There are some operation that can be performed or those that are supported by the array. These are:
1. Creation: It is the process of creating a memory location to store elements of the same type.
2. Insertion: It is the process of adding a new element into the array at a given index.
3. Traversing: It is the process of accessing each element of the array.
4. Deletion: It is the process of removing elements from the array.
5. Updating: It is used to update an element at a given index.
6. Sorting: It is the process of arranging elements of the array in ascending or descending order.
7. Searching: It is the process of finding values from the array.
Searching can be divided into two categories:
a. Linear Search: It is also known as sequential search which starts from lower bound (LB). In a linear search, every element of the array is compared with the given value until the required element is found we reach the end of the array or the upper bound (UB).
Algorithm for Linear search -
b. Binary Search: In binary search, the array is divided from middle and the required value is compared with the value of the middle element. If the match is found the search is halted otherwise the middle value is compared with the required value. If the middle value is greater than the required value then the search starts from the middle to the upper bound otherwise the search starts from lower bound to the middle. This whole process is repeated until the required value is found in array's element.
Algorithm for Binary search -
The Drawback of Array:
We have seen that we can use arrays whenever we have to store and manipulate collections of elements. However, the use of arrays for this purpose presents several limitations related to the way arrays are handled
The array can also be defined as a particular method of storing elements of indexed data. Elements of data are logically stored sequentially in blocks within the array. Each element is referenced by an index or subscripts. An index is usually a number used to address an element in the array.
The syntax for declaring array are:
data_type array_name [array_size];
Following are the important terminologies used for understanding the concepts of Arrays:
Element: Each and every item stored in an array is termed as an element.
Index: each memory location of an element in an array is denoted by a numerical index which is used for identifying the element.
Need of Arrays for building a specific data structure?
When a program works with many variables which hold comparable forms of data, then organizational and managerial difficulty quickly arise. If you are not using arrays, then the number of variables used will increase. Using the array, the number of variables reduces, i.e. you can use a single name for multiple values, just you need to deal with its index values (starting from 0 to n).
Operations performed on Array
There are some operation that can be performed or those that are supported by the array. These are:
1. Creation: It is the process of creating a memory location to store elements of the same type.
2. Insertion: It is the process of adding a new element into the array at a given index.
3. Traversing: It is the process of accessing each element of the array.
4. Deletion: It is the process of removing elements from the array.
5. Updating: It is used to update an element at a given index.
6. Sorting: It is the process of arranging elements of the array in ascending or descending order.
7. Searching: It is the process of finding values from the array.
Searching can be divided into two categories:
a. Linear Search: It is also known as sequential search which starts from lower bound (LB). In a linear search, every element of the array is compared with the given value until the required element is found we reach the end of the array or the upper bound (UB).
Algorithm for Linear search -
linear_search (A,LB,UB,ITEM) Step 1: [INITIALIZATION] LOC=-1, K=LB Step 2: Repeat step 3 and step 4 while(K<UB) Step 3: if(ITEM == A[K]) LOC=K; break Step 4: K=K+1 Step 5: if(LOC == -1) PRINT Element not found else PRINT Element found at LOC Step 6: EXIT
b. Binary Search: In binary search, the array is divided from middle and the required value is compared with the value of the middle element. If the match is found the search is halted otherwise the middle value is compared with the required value. If the middle value is greater than the required value then the search starts from the middle to the upper bound otherwise the search starts from lower bound to the middle. This whole process is repeated until the required value is found in array's element.
Algorithm for Binary search -
binary_search (A,LB,UB,ITEM) Step 1: [INITIALIZATION] SET BEG=LB, END=UB, LOC=-1 Step 2: Repeat step 3, step 4 and step 5 while(BEG<=END) Step 3: MID = INT (BEG+END)/2 Step 4: if(A[MID] == ITEM) LOC=MID PRINT LOC break Step 5: if(A[MID] > ITEM) END=MID-1 else BEG=MID+1 Step 6: if(LOC == -1) PRINT Element not found Step 7: EXIT
The Drawback of Array:
We have seen that we can use arrays whenever we have to store and manipulate collections of elements. However, the use of arrays for this purpose presents several limitations related to the way arrays are handled
- We must know in advance that how many elements are to be stored in an array.
- The array is a static structure. It means that array is of fixed size. The memory which is allocated to the array can not be increased or reduced.
- Since the array is of fixed size, if we allocate more memory than requirement then the memory space will be wasted. And if we allocate less memory than the requirement, then it will create a problem.
- The elements of the array are stored in consecutive memory locations. So insertions and deletions are very difficult and time-consuming.
No comments:
Post a Comment