Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So i have an array for example 4,3,17,5,1,2
and the output should be: 17-1=16 since it is the max difference between the numbers.
i figured it out how to do it, but what I don't understand is, why my teacher is telling me to sort the array and to apply binary search, it should be O(nlogn). I don't understand what is the difference if i do it linear, just comparing the max diff with every other ones and thats all, why i need to sort it???
Posted
Comments
Richard Deeming 16-Feb-15 16:23pm    
The instructions as you've posted them don't make much sense. Sorting the array just to find the min and max would take longer than a linear search, and if you did sort it, you certainly wouldn't need a binary search to find the first and last elements of the array.

However, your teacher is the only person who can answer this question.
Member 11456611 16-Feb-15 16:28pm    
i completely agree with you, i could not catch the teacher yet. but i will definitely ask him.

1 solution

Sorting algorhitm have different complexitiy. In the wikipedia you find some explanations.

If you have sorted data you can take the first and the last element. And you are done.

Your teachers wants you to learn about it. :-O
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900