Abstract Data Types and Array
- ADTs or abstract data types are the ways of classifying data structures by providing a minimal expected interface and some set of methods.
- ADT gives us the blueprint to implement the data structures.
- Abstract Data Types = Minimal required functionality + Operations
- An array is used to store elements of the same data type in contiguous memory location.
- Elements in an array can be accessed using the base address in constant time.
- Although changing the size of an array is not possible, we can reallocate it to some bigger memory location.
- An array ADT holds the collection of given elements accessible by their index.
Minimal Required Functionality
We have three basic functionalities of an array:
- TotalSize: This stores the maximum size of the array.
- UsedSize: This stores the used size of the array.
- *BaseAddress: A pointer variable which contains address of first element of the array.
Operations
We can perform a lot of different operations on the array we created.
- Insertion: Inserting an element in the array.
- Deletion: Deleting an element from the array.
- Merging and Sorting: Merging two arrays to form a single array and then organizing the array in a specific order.
- Linear Search: Finding the position of an element in the array by comparing each element.
- Sorting: Arranging the array in ascending or descending order.
- Binary Search: Finding the position of an element in the array by dividing into halves.
- Traversing: Visiting all the elements of the array by processing each index exactly once.
Algorithm to Insert an Element in an Array
We assume A is an one-dimensional array with N elements. The maximum number of elements it can store is defined by MAX. The index be new inserting element be index and new element be value.
- START
- If N = MAX or index < min_index_of_array or index > N + 1
print "Insertion not possible" - Else
- Set N = N + 1
- Set j = N
- Repeat setps 3.iv. and 3.v. till j ≥ index
- Set A[j+1] = A[j]
- Set j = j - 1
- A[index] = value
- END
Algorithm to Delete an Element from an Array
We assume A is an one-dimensional array with N elements. The index of deleting element be index.
- START
- If index > N or index < min_index_of_array
print "Deletion not possible" - Else
- Set j = index
- Repeat steps 3.iii. and 3.iv. till j ≤ N
- Set A[j] = A[j+1]
- Set j = j + 1
- Set N = N - 1;
- END
Algorithm to Merge an Array into another Array
We assume A and B be two one-dimensional arrays with N and M elements respectively.
- START
- Set j = min_index_of_array
- Repeat steps 4. and 5. till j < N
- Set A[N+j] = B[j]
- Set j = j + 1
- Set N = N + M
- END
Algorithm for Linear Search in an Array
We assume A is an one-dimensional array with N elements. Let item be the element to be searched in the array.
- START
- Set k = 0
- Set j = min_index_of_array
- Repeat steps 5 and 6 till j < N
- If A[j] == item
print "item founded at index j"
Set k = k + 1 - Set j = j + 1
- If k == 0
print "item not founded" - END
Algorithm for Insertion Sorting in Ascending order
We assume A is an one-dimensional array with N elements.
- START
- Set i = min_index_of_array + 1
- Repeat steps 4, 5, 6, 7, 8 and 9 till i < N
- Set temp = A[i]
- Set k = i - 1
- Repeat steps 7and 8 till k ≥ 0 and temp ≤ A[k]
- Set A[k+1] = A[k]
- Set k = k - 1;
- Set A[k+1] = temp;
- END
Algorithm for Binary Search in an Array
We assume A is an one-dimensional ascending order sorted array with N elements. Let item be the element to be searched in the array.
- START
- Set beg = 0
- Set end = N
- Set j = 0
- Repeat steps 6, 7, 8 and 9 till beg ≤ end
- Set mid = (beg + end)/2
- If A[mid] == item
print "item founded at position mid"
Set j = j + 1
Break the loop - Else if A[mid] < item
Set beg = mid + 1 - Else
Set end = mid - 1 - If j == 0
print "item not founded" - END
Comments
Post a Comment