# Data Structure – codewindow.in

## Related Topics ## Data Structure

#### Here's an example implementation of counting sort in Python:

``````def counting_sort(arr):
# Find the maximum value in the input data
max_val = max(arr)

# Create a counting array with a length equal to the maximum value + 1
counts =  * (max_val + 1)

# Traverse the input data, counting the number of occurrences of each distinct element
for val in arr:
counts[val] += 1

# Modify the counting array so that each entry contains the number of elements less than or equal to the index of that entry
for i in range(1, len(counts)):
counts[i] += counts[i-1]

# Traverse the input data in reverse order, placing each element in its correct position in the output array using the information in the counting array
output =  * len(arr)
for val in reversed(arr):
index = counts[val] - 1
output[index] = val
counts[val] -= 1

return output
``````

#### Here’s an example implementation of radix sort in Python:

``````def radix_sort(arr):
# Find the maximum value in the input data
max_val = max(arr)

# Sort the input data based on each digit position
exp = 1
while max_val // exp > 0:
arr = counting_sort_by_digit(arr, exp)
exp *= 10

return arr

def counting_sort_by_digit(arr, exp):
# Create a counting array with a length of 10 (one for each digit)
counts =  * 10

# Traverse the input data, counting the number of occurrences of each digit
for val in arr:
digit = (val // exp) % 10
counts[digit] += 1

# Modify the counting array so that each entry contains the number of elements less than or equal to the index of that entry
for i in range(1, len(counts)):
counts[i] += counts[i-1]

# Traverse the input data in reverse order, placing each element in its correct position in the output array using the information in the counting array
output =  * len(arr)
for val in reversed(arr
``````

#### The time complexity of sorting using a heap is O(n log n) for both the best and worst cases, while the space complexity is O(n). The time complexity of sorting using a BST is also O(n log n) for the worst case, but it can be as low as O(n) for the best case if the tree is balanced. The space complexity of sorting using a BST is O(n). #### Questions on Chapter 11  #### Questions on Chapter 12  ## We Loveto Support you

Go through our study material. Your Job is awaiting.