An Array
- Array is a linear data structure.
- An array is a collection of items stored at contiguous memory locations.
- An array is a collection of homogenous elements(same data type elements). In arrays data will be stored in using indexes.
- Array is the simplest data structure where each data element can be randomly accessed by using its index number.
Syntax: data type, Array name[size];
Basically, there are two types of arrays based on dimensions:
- One-dimensional array
- Multi-dimensional array
One-dimensional array
- In One-dimensional array, the data will be stored in only one dimension i. e. index.
-
There are two types of 1-D arrays depend on size
- Fixed size array
- Variable size array
Fixed size array
- In fixed size arrays the total size of an array is fixed or constant. We can implement fixed size arrays of complie time i. e. while writing the program itself, we will fix the size of an array.
- Eg:int a [10];
- Main disadvantage is that memory will be wasted if we could not use the full size of an array.
Variable size array
- In Variable size array the total size of an array is variable.
- We can implement variable size arrays at run time , while writing program we will give only variable size for the declaration of an array and we will give exact size at run time, i. e. defining an array.
- Eg: int size; int a[size];
Initializing values in array:
- We can assign values in an array in two ways:
-
Compile time assignment-
- Here, we can assign the values or elements at compile time i. e. while writing the program itself, by using braces {} and commas(,)
- Eg: int a[5]={10,20,30,40,50}
-
Runtime assignment-
- Here, we can assign the values or elements at compile time i. e. while writing the program itself, by using braces {} and commas(,)
- Eg: int a[5]={10,20,30,40,50}
Operations in an array:
- We can perform various operations in arrays. Those are as follows:
- Traversing, Sorting, Searching, Inserting, Deletion, Merging
1) Traversing operation:
- This operation is used to visit all elements in an array.
- Algorithm for traversing array
- Input:An array A
- Output:Visiting all the elements using process function.
- Data Structure:Array A[Lower,……upper]..
Steps
1. Start
2. i= lower bound
3. while i≤upper bound do
4. process A[i]
5. i=i+1
6. end the while
7. stop.
- The process () function to perform digiting elements one by one and prints all the elements of an array in the output screen.
2) Sorting operation
- The sorting operation performs ascending and descending actions, i. e. the sorting operation keeps the elements of an array in particular order(either ascending or descending order).
Algorithm for Sorting array
- Input:An array A
- Output: the elements in particular order.
- Data Structure:Array A[Lower,……upper]
Steps
1. Start
2. i=upper bound
3. while i≥lower bound do
4. j=1
5. while j<i do
6. if order of (A[i],A[j+1]=FALSE)
7. swap (A[j];A[j+1]
8. end if
9. j=j+1
10. end while
11. i=i-1
12. end while
13. stop.
Example: Arranging elements in ascending order.
3) Searching operation
- In searching operation, identifies the required elements is present in an array or not.
- The searching operation will also finds the respective address or index of required elements. The algorithm of searching operation is as follows:
Algorithm for search array:
- Input:An array A and searching element ‘KEY’.
- Output:The index of the key element or the element is not present in the array, the unsuccessful search message should come.
- Data structure:Array A[L……U]
Steps
1. Start
2. i=L, found=0, location=0
3. while (i≤U) and found =0 do
4. if compare [a[i]=KEY]=TRUE then
5. found=1;
6. location=i;
7. else
8. i=i+1
9. end if
10. end while
11. if(found=0) then
12. printf(unsuccessful search) ;
13. else
14. printf(successful search) ;
15. end if
16. printf(loaction) ;
17. Stop.
Program
def search(array,x):
for i in range(len(array)):
if array[i]==x:
return i
return -1
4) Insertion
- This operation describes about inserting an element into an array if at all the array is not full.
- Algorithm
- Input:An array A and an element which we are going to insert.
- Output:An array with new element.
- Data structure:Array A[l….u]
- In an array, null index indicates there is a space in the array .
Steps:
1. Start
2. if u=null then
3. print “the array is full”
4. else
5. i=u
6. while(i>location do)
7. A[i]=A[i-1]
8. i=i+1
9. end while
10. A[location]=KEY
11. End else
12. Stop.
- In above algorithm, location indicates the address where we can insert over new elements . For that case, first we have to make that location part empty by moving elements towards upper bound element.
5) Deletion
- In this operation, we can delete a particular arrangement from an array. This deletion can be done by shifting by over writing the sub-sequential elements into the deleting position.
Algorithm
- Input:An array A and deleting element key.
- Output:Array A with deleting element.
- Data structure:Array A[l… …u]
Steps
1. Start
2. i=searching array(A,KEY)
3. if i=0 then
4. print Key is not found.
5. exit
6. else
7. while(i<u)
8. A[i]=A[i+1]
9. i=i+1
10. end while
11. end if
12. A[u]=null
13. u=u-1
14. stop.
6) Merging
- In this operation, we can combine two or more arrays into a single array.
Algorithm for merging of two arrays.
- Input:Three array A, B, C
- Output:C array with elements of A &B
- Data structure:Array [l… u]
Steps
1. Start
2. i1=L1 and i2=L2
3. L=L1, u=u1+u2-L2+1
4. i=L
5. allocate the memory [size (u-L+1) ]/ to allocate memory in c
6. while(i1≤u1) do
7. c[1]=A[i1]
8. i=i+1, i1=i1+1
9. end while
10. while(i2≤u2) do
11. c[i]=B[i2]
12. i=i+1, i2=i2+1
13. end while
14. stop.