Operations on One Dimensional Array

Operations on One Dimensional Array

  • Traversing one dimensional array
  • Inserting element in one dimensional array
  • Deleting element from one dimensional array
  • Merging one dimensional arrays
  • Searching an element from one dimensional array

  • Traversing One Dimensional Array

    Traversing one dimensional array is a simple process to print all the array elements one by one.

    Algorithm

    Repeat for I = LB to UB
    Apply PROCESS to A[I]
    [End of for loop]
    Exit
    A= linear array
    LB = Specifies the beginning of array (Lower bound)
    UB = Specifies the ending of array (Upper bound)

    Inserting element in One Dimensional Array

    Inserting element in one dimensional array is the operation of adding an element to existing set of elements. A new element can be added at the beginning, end, or any given index of array.

    Inserting Element in Unsorted One Dimensional Array

    Set I = N [Initialize counter]
    Repeat While (I >= LOC)
    Set A[I+1] = A[I] [Move elements downward]
    Set I = I – 1 [Decrease counter by 1]
    [End of While Loop]
    Set A[LOC] = ITEM [Insert element]
    Set N = N + 1 [Reset N]
    Exit
    A is an array with N elements.
    Loc is the location where ITEM has been inserted, ITEM is the new value to be inserted.

    Inserting Element in Sorted One Dimensional Array

    Set I = N [Initialize counter]
    Repeat While (ITEM < A[I]) and (I >= 1)
    Set A[I+1] = A[I] [Move elements downward]
    Set I = I – 1 [Decrease counter by 1]
    [End of While Loop]
    Set A[I+1] = ITEM [Insert element]
    Set N = N + 1 [Reset N]
    Exit
    A is an array
    N is the number of element in an array A
    ITEM is the new element to be inserted in A

    Deleting Element from One Dimensional Array

    Deleting an element from one dimensional array is the operation of removing an element from existing list of elements.
    Algorithm
    Set ITEM = A[LOC] [Assign the element to be deleted to ITEM]
    Repeat For I = LOC to N
    Set A[I] = A[I+1] [Move the Ith element upwards]
    [End of For Loop]
    Set N = N – 1 [Reset N]
    Exit
    A is an array
    N is the number of elements in array A
    LOC is the position from where ITEM to be deleted

    Merging One Dimensional Array

    Merging is the operation of combining the elements of two linear arrays into a single array.

    Two cases of Merging One Dimensional Array
  • Merging two unsorted arrays
  • Merging two sorted arrays
  • Merging two Unsorted Arrays

    Repeat For I = 1 to M
    Set C[I] = A[I] [Assign the elements of array A to array C]
    [End of For Loop]
    Set K = 1 [Initialize counter]
    Repeat For J = M+1 to M+N
    Set C[J] = B[K] [Assign the elements of array B to array C]
    Set K = K + 1 [Increase the counter by 1]
    [End of For Loop]
    Exit
    A is an array with M elements and B is an array with N elements.
    C is an empty array with P locations

    Merging two Sorted Arrays

    Set I = J = K = 1 [Initialize counters]
    Repeat While (I <= M) and (J <= N)
    If (A[I] < B[J]) Then
    Set C[K] = A[I] [Assign elements of array A to array C]
    Set I = I + 1
    Else
    Set C[K] = B[J] [Assign elements of array B to array C]
    Set J = J + 1
    [End of If]
    Set K = K + 1
    [End of While Loop]
    If (I > M) Then [Array A is empty]
    Repeat While (J <= N)
    Set C[K] = B[J] [Assign the remaining elements of array B to array C]
    Set J = J+1 and K = K+1
    [End of While Loop]
    [End of If]
    If (J > N) Then [Array B is empty]
    Repeat While (I <= M)
    Set C[K] = A[I] [Assign the remaining elements of array A to array C]
    Set I = I+1 and K = K+1
    [End of While Loop]
    [End of If]
    Exit
    A is a sorted array with M elements
    B is a sorted array with N elements
    C is an empty array with P locations where P >= M + N

    Searching an Element from One Dimensional Array

    You can perform a search for an array element based on its value or its index. Searching operation find the location of target item from given array of elements.
    Start
    Set J = 0
    Repeat steps 4 and 5 while J < N
    IF LA[J] is equal ITEM THEN GOTO STEP 6
    Set J = J +1
    PRINT J, ITEM
    Stop
    LA is an leaner array with N elements
    K is positive integer such that K<=N