It is because you traverse the whole array for each its element in the loop.
Your function is invalid. Also ask you the question why did you sort the arrray? Use the property of sorted arrrays to write the functiion.
Sorting of array was one of the tasks. The assignment asks first to sort the given array and then count and frequency of values. I didn't add here the definition for sort function, as it works OK.
So, what I need to do to solve the problem? Completely change the function?