### Find the absolute minimal difference between two elements in an unsorted array basicsorting sorting array    Posted: 7 years ago Updated: 6 years ago Edit answers (1) views (12826)

Suppose you are given an unsorted array containing only non-negative integers. How will you find the absolute minimal difference between any two numbers in the array?

 Posted: 7 years ago 0 0 Edit The algorithm to find the minimal absolute difference between two numbers in the array works as follows: Sort the array. Iterate through the sorted array and take the difference between the two consecutive numbers. Return their minimum. Time complexity = $$O(nlogn)$$.The following code snippet (in Java) illustrates the approach.public int absMinDifferenceInAnArray(int[] array){ /* Sort the array */ Arrays.sort(array); /* Initialize difference between two consecutive numbers to zero */ int minimum = 0; for(int i = 1; i < (array.length - 1); i++){ int temp = Math.abs(array[i+1] - array[i]); if (temp < = minimum) minimum = temp; } /* Return the minimum difference */ return minimum; }