Question: # using the methods available in StaticArray class because we are not allowed to use the built - in methods in Python minVal = array.get

# using the methods available in StaticArray class because we are not allowed to use the built-in methods in Python
minVal = array.get(0)
maxVal = array.get(0)
arrayLength = array.length()
# Finding min and max values of the input array
for x in range(1, arrayLength):
temp = array.get(i)
# We must not use inbuilt min() and max() functions
if(maxVal < temp):
maxVal = temp
elif(minVal > temp):
minVal = temp
# Finding the range of values present in the array
rangeOfValues =(maxVal - minVal)+1
# count_arr stores the count of each value in the array
count_arr = StaticArray(rangeOfValues)
for i in range(rangeOfValues):
count_arr.set(i,0)
for i in range(arrayLength):
temp = array.get(i)
j = temp - minVal
count_arr.set(j, count_arr.get(j+1))
# sorted array to be returned
sortedStaticArray = StaticArray(arrayLength)
lastIndex = arrayLength -1
for i in range(rangeOfValues-1,-1,-1): # loop thrugh the array in the reverse order
while(count_arr.get(i)>0):
temp = i + minVal
sortedStaticArray.set(lastIndex, temp)
lastIndex -=1
count_arr.set(i,(count_arr.get(i)-1))
return sortedStaticArray

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!