Sort elements of an array in increasing order of absolute difference of adjacent elements | C # 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
using System ; using System . Collections . Generic ; class GFG { static void sortDiff ( List < int > arr , int n ) { arr . Sort ( ) ; List < int > Out = new List < int > ( ) ; while ( n > 0 ) { Out . Add ( arr [ n / 2 ] ) ; arr . RemoveAt ( n / 2 ) ; n = n - 1 ; } foreach ( int i in Out ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 8 , 1 , 2 , 3 , 0 } ; List < int > arr = new List < int > ( a ) ; int n = 5 ; sortDiff ( arr , n ) ; } }