0% found this document useful (0 votes)
2 views

Array

The document provides an overview of arrays, including their definition and implementation in Python using lists. It covers essential operations such as traversing, searching (linear and binary search), and sorting (bubble sort and quick sort) arrays, along with code examples for each operation. The content serves as a foundational guide for understanding and manipulating arrays in programming.

Uploaded by

Kanza Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array

The document provides an overview of arrays, including their definition and implementation in Python using lists. It covers essential operations such as traversing, searching (linear and binary search), and sorting (bubble sort and quick sort) arrays, along with code examples for each operation. The content serves as a foundational guide for understanding and manipulating arrays in programming.

Uploaded by

Kanza Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Operations on Arrays: Traversing, Searching,

and Sorting
Introduction to Arrays
 An array is a data structure that can hold a collection of elements of the same data type.
 In Python, arrays are implemented using lists.

my_array = [1, 2, 3, 4, 5]

Traversing Arrays
 Traversing an array means visiting each element one by one.
 It's often done using loops.

for element in my_array:


print(element)

Array Operations
 Common array operations include adding, removing, and modifying elements.

my_array.append(6) # Add an element to the end


my_array.insert(2, 7) # Insert 7 at index 2
my_array.remove(4) # Remove the element with value 4
my_array[3] = 8 # Modify the element at index 3

Searching in Arrays
Linear Search
 Linear search involves iterating through the array element by element to find a specific
value.
 It's not the most efficient for large arrays.
def linear_search(arr, target):
for i, element in enumerate(arr):
if element == target:
return i
return -1 # Target not found
https://www.cs.usfca.edu/~galles/visualization/Search.html
Binary Search

 Binary search is a more efficient way to search for a value in a sorted array.
 It repeatedly divides the search interval in half.

def binary_search(arr, target):


left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1 # Target not found

Sorting Arrays
Bubble Sort

 Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps
them if they are in the wrong order.

def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(n - 1 - i):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
https://www.w3schools.com/dsa/dsa_algo_bubblesort.php

Quick Sort

 Quick sort is a divide-and-conquer algorithm that selects a "pivot" element and partitions
the other elements into two sub-arrays, according to whether they are less than or greater
than the pivot.

def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
https://www.w3schools.com/dsa/dsa_algo_quicksort.php

You might also like