Skip to main content

Command Palette

Search for a command to run...

DSA-Array

Updated
3 min read
DSA-Array
S

I am a data engineer who is responsible for designing, building, maintaining, and testing the infrastructure and systems that are used to store, process, and analyze data. I work closely with data scientists and analysts to ensure that the data pipelines and systems are able to support the data needs of an organization.

I have a strong background in computer science and software engineering, and skilled in programming languages such as Python, Java, and SQL also familiar with database systems and big data technologies like Hadoop, Spark, and NoSQL databases.

Some of my key responsibilities as a data engineer:

Designing and building data pipelines to extract, transform, and load data from various sources Setting up and maintaining data storage and processing systems, including data warehouses and data lakes Collaborating with data scientists and analysts to understand their data needs and ensure that the data infrastructure can support their requirements Performing data quality checks and troubleshooting any issues that arise Implementing security and privacy measures to protect sensitive data

Array:

  • A collection of items in a contiguous manner.
  • Python list based on array data structures.
  • Array index always starts from 0 and ends at n-1 where n is length of an array.
  • Each index have own address ( in hexadecimal format).
  • In array we can access any value by their index and time complexity for this is constant - O(1)
arr = [1, 3, 5, 2, 7]
print(arr[0]) 
## we are accessing a base address i.e 0th index value

Applications of array:

Mostly this DS used of searching operation

Lets see some of searching operations.

We don't need sorted array for this type of searching, it will search until the match found. Most of the times developer rely on this type of searching but this is not a good practice always. Try to use upcoming optimized searching if possible. Time complexity for this is O(n) why because we have used one for loop.

arr = [1, 3, 5, 2, 7]
find_value = 3
result = -1
for i in range(0, len(arr)-1):
  if arr[i] == find_value:
    result = arr[i]
    break;
print(result)

Binary search is an algorithm to find a value by repeatedly dividing the search values by half. By default it should be sorted in nature. Because of this we could able to achieve the time complexity to O(log n).

Steps for Binary search:

  • Split the full array into two half.
  • Then starts with mid element.
  • If the search value lesser than the mid element we go for left side.
  • If the search value greater than the mid element we go for right side.
  • The above steps repeatedly occurs until the search value found or length of array 0.
  • If the search element not found, It should return -1.

To find mid value we should use below formula: mid = (left + right)//2 or mid = left + (right - left)//2 -> This formula is recommended to avoid index out of range error.

Now will see the code implementation,

def binarySearch(arr, x):
  while(left < right):
    mid = left + (right - left)//2
    if arr[mid] == x:
      return mid
    elif arr[mid] < x:
      left = mid + 1
    else:
      right = mid - 1
  return -1
arr = [1, 2, 3, 4, 5, 6, 7]
x = 6
print(binarySearch(arr, x))

This is similar to binary search, the only different here is we are splitting the array into three parts.

Lets see implementations of this,

def ternarySearch(arr, x, i, j):
  while i <= j:
    mid_1 = i + (j - i)//3
    mid_2 = j - (j - i)//3
    if arr[mid_1] == x:
      return mid_1
    elif arr[mid_2] == x:
      return mid_2
    elif arr[mid_1] < x:
      return ternarySearch(arr, x, i, mid_1 - 1)
    elif arr[mid_2] > x:
      return ternarySearch(arr, x, mid_1 + 1, j)
    else:
      return ternarySearch(arr, x, mid_1 + 1, mid_1 - 1)

arr = [1, 2,3,4,5,6,7,8,9]
x = 8
i = 0
j = len(arr) - 1
print(ternarySearch(arr, x, i, j))

More from this blog

Sivaraman Arumugam

27 posts

I will share my thoughts and notes about my studies