15,877,966 members
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
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.

## Solution 1

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

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

Top Experts
Last 24hrsThis month
 Richard Deeming 40 OriginalGriff 20 Richard MacCutchan 15 Dave Kreskowiak 10 Andre Oosthuizen 10
 Pete O'Hanlon 1,465 OriginalGriff 1,230 Richard Deeming 723 Graeme_Grant 700 Dave Kreskowiak 549

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