Code Sniff Et

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 67

The prefix and postfix forms of the expression p + q - r * s are:

Prefix form: - + p q * r s
Postfix form: p q + r s * -
def countingSort(array, place):
size = len(array)
output = [0] * size
count = [0] * 10
# Calculate count of elements
for i in range(0, size):
index = array[i] // place
count[index % 10] += 1
# Calculate cumulative count
for i in range(1, 10):
count[i] += count[i - 1]
# Place the elements in sorted order
i = size - 1
while i >= 0:
index = array[i] // place
output[count[index % 10] - 1] = array[i]
count[index % 10] -= 1
i -= 1
for i in range(0, size):
array[i] = output[i]
# Main function to implement radix sortdef radixSort(array):
# Get maximum element
max_element = max(array)

# Apply counting sort to sort elements based on place value.


place = 1
while max_element // place > 0:
countingSort(array, place)
place *= 10
data = [121, 432, 564, 23, 1, 45, 788]
radixSort(data)print(data)
Worst case for Radix Sort:
a. O (log n)
b. O(n^2)
c. O(n+k)
d. O(1)
def bubbleSort(array):

# loop to access each array element


for i in range(len(array)):

# loop to compare array elements


for j in range(0, len(array) - i - 1):

# compare two adjacent elements


# change > to < to sort in descending order
if array[j] > array[j + 1]:

# swapping elements if elements


# are not in the intended order
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
data = [-2, 45, 0, 11, -9]
bubbleSort(data)
print('Sorted Array in Ascending Order:')
print(data)

What will be the transactions to be happent when it attains third iteration


of the process?
def selectionSort(array, size):
for step in range(size):
min_idx = step
for i in range(step + 1, size):

# to sort in descending order, change > to < in


this line
# select the minimum element in each loop
if array[i] < array[min_idx]:
min_idx = i

# put min at the correct position


(array[step], array[min_idx]) = (array[min_idx],
array[step])
data = [-2, 45, 0, 11, -9]
size = len(data)
selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data)

What will be the transactions to be happent when it attains fourth iteration


of the process?
Def insertionSort(array):

for step in range(1, len(array)):


key = array[step]
j = step - 1

while j >= 0 and key < array[j]:


array[j + 1] = array[j]
j = j - 1

array[j + 1] = key

data = [9, 5, 1, 4, 3]
insertionSort(data)print('Sorted Array in Ascending Order:')
print(data)

What will be the transactions to be happent when it attains second iteration


of the process?
The no of vertices of odd degree in a Graph is always even.
• In a full binary tree (where every node has either 0 or 2 children), the
number of null pointers is always one more than the number of
nodes. Therefore, a full binary tree with n nodes has n+1 null
pointers.

• In a complete binary tree (where all levels are filled except possibly
the last level, which is filled from left to right), the number of null
pointers can vary depending on the structure of the tree. However, in
the worst case scenario, where the last level is half-full, a complete
binary tree with n nodes has floor(n/2) + 1 null pointers.
The minimum number of AVL nodes is Fib(h+2)-1
Minimum Nodes in an AVL tree with height n is H(n)=H(n−1)+H(n−2)+1.
H(0)=1. H(3)=H(2)+H(1)+1=4+2+1=7. So, the max height with 7 nodes is 3.

You might also like