Data Structure & Algorithms | Array in Python

Prajjwal Sule
3 min readApr 11, 2023

--

Data-Structure-and-Algorithms-with-Python-Array-in-Python

Arrays are a fundamental data structure in computer science, and they are used to store collections of elements of the same type in contiguous memory locations. In Python, arrays are represented using the array module, which provides an array class that allows you to create and manipulate arrays of different types.

To create an array in Python, you first need to import the `array` module. Then you can create an array by specifying its type code and its initial values

import array

# create an array of integers
arr = array.array('i', [1, 2, 3, 4, 5])

# print the array
print(arr)

This will create an array of integers with the values 1, 2, 3, 4, and 5, and then print the array to the console.

NOTE: List in Python is also works as an array but list can store heterogeneous data inside it, where as the array only store’s the homogeneous data which have been defined while declaring the array.

In Python, there are several algorithms that can be applied to arrays to perform common tasks such as searching, sorting, and manipulating the elements in the array.

Linear search

This is a simple algorithm that searches an array for a given value by iterating through each element of the array one by one until it finds the value it’s looking for.

def linear_search(arr, value):
for i in range(len(arr)):
if arr[i] == value:
return f"Array Position: {i}"
return -1

This function takes an array and a value to search for, and returns the index of the value in the array if it’s found, or -1 if it’s not found.

import array
arr = array.array('i', [1,2,3,4,5,34,2,3,5,4,3,2,3,5,6,78,7,5,4,3])
value = 78
linear_search(arr,value)

#OutPut :
'Array Position: 15'

Bubble sort

This is a simple sorting algorithm that repeatedly iterates through an array, compares adjacent elements, and swaps them if they’re in the wrong order.

def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

This function takes an array and sorts it in ascending order using the bubble sort algorithm.

import array
arr = array.array('i', [1,2,3,4,5,34,2,3,5,4,3,2,3,5,6,78,7,5,4,3])
bubble_sort(arr)

#OutPut :
[1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 7, 34, 78]

Insertion sort

This is another simple sorting algorithm that works by iterating through an array, comparing each element with the elements before it, and inserting it into the correct position in the array.

def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key

This function takes an array and sorts it in ascending order using the insertion sort algorithm.

import array
arr = array.array('i', [1,2,3,4,5,34,2,3,5,4,3,2,3,5,6,78,7,5,4,3])
insertion_sort(arr)

#Output:
[1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 7, 34, 78]

--

--

Prajjwal Sule
Prajjwal Sule

Written by Prajjwal Sule

Data Science Enthusiast | Continuous Learner | You can connect with me — https://www.linkedin.com/in/prajjwal-sule/

No responses yet