Sort elements of an array in increasing order of absolute difference of adjacent elements | Java implementation to sort the elements of the array in such a way that absolute difference between the adjacent elements are in increasing order ; Function to sort the elements of the array by difference ; Sorting the array ; Array to store the elements of the array ; Iterating over the length of the array to include each middle element of array ; Appending the middle element of the array ; Driver Code
import java . util . * ; class GFG { static void sortDiff ( Vector < Integer > arr , int n ) { Collections . sort ( arr ) ; Vector < Integer > out = new Vector < Integer > ( ) ; while ( n > 0 ) { out . add ( arr . get ( n / 2 ) ) ; arr . remove ( n / 2 ) ; n = n - 1 ; } for ( int i : out ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { Integer [ ] a = { 8 , 1 , 2 , 3 , 0 } ; Vector < Integer > arr = new Vector < Integer > ( Arrays . asList ( a ) ) ; int n = 5 ; sortDiff ( arr , n ) ; } }