Sort array after converting elements to their squares | C # program to Sort square of the numbers of the array ; Function to sort an square array ; First convert each array elements into its square ; Sort an array using " inbuild ▁ sort ▁ function " in Arrays class . ; Driver Code
using System ; class GFG { public static void sortSquares ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] * arr [ i ] ; Array . Sort ( arr ) ; } public static void Main ( ) { int [ ] arr = { - 6 , - 3 , - 1 , 2 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( " Before ▁ sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; sortSquares ( arr ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " After ▁ Sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }