Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Write a bubble short algorithm:

Bubble Sort is the simplest sorting algorithm that works by repeatedly


swapping the adjacent elements if they are in the wrong order. This
algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high.

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

write marge short algorithm

• Create a new empty array, which will be used to store the merged and sorted elements of
the original arrays.
• Initialize two pointers, one for each of the original arrays.
• Compare the elements at the current pointers in each of the original arrays. Whichever
element is smaller, add it to the new array and increment the pointer for that array.
• Repeat step 3 until one of the original arrays is exhausted.
• Once one of the original arrays is exhausted, add the remaining elements from the other
array to the new array.
• Return the new array, which contains the merged and sorted elements of the original arrays.

def merge_arrays(arr1, arr2):


# Create a new empty array for the merged elements
merged = []
# Initialize pointers for each of the original arrays
i, j = 0, 0
# Compare elements at the current pointers and add the smaller one to the merged
array
while i < len(arr1) and j < len(arr2):
if arr1[i] < arr2[j]:
merged.append(arr1[i])
i += 1
else:
merged.append(arr2[j])
j += 1
# Add any remaining elements from one of the arrays to the merged array
while i < len(arr1):
merged.append(arr1[i])
i += 1
while j < len(arr2):
merged.append(arr2[j])
j += 1
return merged

insertion short algorithm


• Here is a basic algorithm for sorting an array using the insertion sort method:
• Starting with the second element of the array, iterate through the array.
• Compare the current element with the elements before it.
• If the current element is smaller than any of the previous elements, find the correct position
for it by shifting the larger elements one position to the right.
• Insert the current element into its correct position.
• Repeat steps 2-4 for each element in the array.
• Return the sorted array.

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

selection short algorithm


• Iterate through the array, from the first element to the second-to-last element.
• For each iteration, find the minimum element in the remaining unsorted portion of the
array.
• Swap the minimum element with the current element.
• Repeat steps 2 and 3 for each element in the array.
• Return the sorted array.

def selection_sort(arr):
for i in range(len(arr)):
# Find the minimum element in the remaining unsorted portion of the array
min_index = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
# Swap the minimum element with the current element
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr

What is algorithm explain?


An algorithm is a procedure used for solving a problem or performing a
computation. Algorithms act as an exact list of instructions that conduct specified
actions step by step in either hardware- or software-based routines. Algorithms are
widely used throughout all areas of IT.

Characteristics of Insertion Sort:


• This algorithm is one of the simplest algorithm with simple
implementation
• Basically, Insertion sort is efficient for small data values
• Insertion sort is adaptive in nature, i.e. it is appropriate for data sets
which are already partially sorted.

procedure insertionSort(A: list of sortable items)


n = length(A)
for i = 1 to n - 1 do
j = i
while j > 0 and A[j-1] > A[j] do
swap(A[j], A[j-1])
j = j - 1
end while
end for
end procedure

You might also like