abstract.html.docs[1]
abstract.html.docs[1]
Bubble Sort
Description:
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent items, and swaps them if they are in the wrong order. The process is repeated until the
list is sorted.
Algorithm:
Pseudocode:
css
Copy code
for i = 0 to n-1
for j = 0 to n-i-2
if arr[j] > arr[j+1]
swap(arr[j], arr[j+1])
Time Complexity:
2. Insertion Sort
Description:
Insertion Sort builds the sorted array one element at a time. It takes each element from the
unsorted portion and places it at the correct position in the sorted portion of the array.
Algorithm:
css
Copy code
for i = 1 to n-1
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key
arr[j+1] = arr[j]
j = j - 1
arr[j+1] = key
Time Complexity:
3. Heap Sort
Description:
Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure. The
algorithm builds a max-heap (for ascending order) and repeatedly extracts the largest element to
place it at the end of the array.
Algorithm:
Pseudocode:
less
Copy code
heapify(arr, n, i):
largest = i
left = 2*i + 1
right = 2*i + 2
if left < n and arr[left] > arr[largest]
largest = left
if right < n and arr[right] > arr[largest]
largest = right
if largest != i:
swap(arr[i], arr[largest])
heapify(arr, n, largest)
heapSort(arr):
n = length of arr
for i = n//2 - 1 down to 0
heapify(arr, n, i)
for i = n-1 down to 1
swap(arr[0], arr[i])
heapify(arr, i, 0)
Time Complexity:
Description:
Divide and Conquer is a strategy for solving problems by recursively breaking them into smaller
subproblems, solving the subproblems, and combining their solutions.
Examples:
1. Merge Sort:
o Divide the array into two halves.
o Recursively sort each half.
o Merge the sorted halves.
2. Quick Sort:
o Choose a pivot element.
o Partition the array into two subarrays: one with elements smaller than the pivot,
and one with elements larger.
o Recursively sort each subarray.
General Algorithm:
Time Complexity:
Description:
Binary Search is an efficient algorithm to search for a target element in a sorted array. It works
by repeatedly dividing the search range in half.
Algorithm:
Pseudocode:
vbnet
Copy code
binarySearch(arr, low, high, target):
if low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
return binarySearch(arr, mid + 1, high, target)
else:
return binarySearch(arr, low, mid - 1, target)
return -1 # Target not found
Time Complexity:
Description:
To find the maximum and minimum elements in an array, iterate through the array once, keeping
track of the largest and smallest elements found so far.
Algorithm:
Pseudocode:
arduino
Copy code
findMaxMin(arr):
max = arr[0]
min = arr[0]
for i = 1 to n-1:
if arr[i] > max:
max = arr[i]
if arr[i] < min:
min = arr[i]
return max, min
Time Complexity:
7. Matrix Multiplication
Description:
Matrix multiplication involves multiplying two matrices and obtaining a resulting matrix. The
number of rows of the first matrix must match the number of columns of the second matrix.
For two matrices A (m × n) and B (n × p), the resulting matrix C will have dimensions m × p.
Algorithm:
1. For each row of A and each column of B, compute the dot product.
2. The element in row iii and column jjj of the resulting matrix C is computed as the sum of
the products of the corresponding elements of row iii from A and column jjj from B.
Pseudocode:
less
Copy code
matrixMultiplication(A, B):
C = matrix of size m × p
for i = 0 to m-1:
for j = 0 to p-1:
C[i][j] = 0
for k = 0 to n-1:
C[i][j] += A[i][k] * B[k][j]
return C
Time Complexity:
8. Quick Sort
Description:
Quick Sort is a divide-and-conquer sorting algorithm. It selects a pivot element, partitions the
array into two subarrays (one with elements smaller and one with larger elements), and
recursively sorts the subarrays.
Algorithm:
Pseudocode:
less
Copy code
quickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
quickSort(arr, low, pivotIndex - 1)
quickSort(arr, pivotIndex + 1, high)
Time Complexity:
Asymptotic Notation
Asymptotic notation is used to describe the behavior of algorithms as the input size becomes
large. It gives us a way to express the efficiency or complexity of an algorithm in terms of time
or space. The most common asymptotic notations are:
Formal Definition:
An algorithm is O(f(n)) if there exist constants c > 0 and n₀ such that for all n ≥ n₀, the
time complexity is less than or equal to c * f(n).
Formal Definition:
An algorithm is Θ(f(n)) if there exist constants c₁ > 0, c₂ > 0, and n₀ such that for all n
≥ n₀, the time complexity is bounded by c₁ * f(n) and c₂ * f(n).
Heap Sort is a comparison-based sorting algorithm that utilizes a binary heap data structure to
sort elements. The binary heap is a complete binary tree that satisfies the heap property. Heap
Sort works by building a max heap (for sorting in ascending order) and then repeatedly
extracting the maximum element to place it in the correct position in the array.
Key Concepts
1. Binary Heap:
o Max Heap: A complete binary tree where each parent node is greater than or
equal to its child nodes.
o Min Heap: A complete binary tree where each parent node is less than or equal to
its child nodes.
2. Heap Property:
o In a Max Heap, the key at each node is greater than or equal to the keys of its
children, and the maximum key is at the root.
o In a Min Heap, the key at each node is less than or equal to the keys of its
children, and the minimum key is at the root.
Detailed Algorithm
def heapSort(arr):
n = len(arr)
Heap Sort's performance is consistent, as it always requires O(nlogn)O(n \log n) time, regardless
of the input data's initial order.
Space Complexity
Heap Sort is an in-place sorting algorithm, meaning it does not require extra space proportional
to the input size. The only space used is for recursive calls during the heapify process, which is
O(logn)O(\log n) in the worst case.
Space Complexity:
1. Not Stable: Heap Sort is not a stable sort. This means that the relative order of equal
elements may not be preserved.
2. Slow in Practice: Although the time complexity is O(nlogn)O(n \log n), Heap Sort is
generally slower than algorithms like Merge Sort and Quick Sort in practice, especially
due to the constant factors involved in the heap operations.
3. Cache Inefficiency: Heap operations may not take advantage of modern memory
hierarchies as efficiently as other algorithms like Quick Sort.
Conclusion
Heap Sort is an efficient, comparison-based, in-place sorting algorithm with a worst-case time
complexity of O(nlogn)O(n \log n). While it is guaranteed to perform well and does not require
additional space, it is not stable and may be slower than other sorting algorithms in practice, such
as Quick Sort. However, its consistent O(nlogn)O(n \log n) performance makes it a reliable
choice in cases where stability or cache efficiency is not a concern.