Sort an array when two halves are sorted | C # program to Merge Two Sorted Halves Of Array Into Single Sorted Array ; Merge two sorted halves of Array into single sorted array ; starting index of second half ; Temp Array store sorted resultant array ; First Find the point where array is divide into two half ; If Given array is all - ready sorted ; Merge two sorted arrays in single sorted array ; Copy the remaining elements of A [ i to half_ ! ] ; Copy the remaining elements of A [ half_ ! to n ] ; Driver code ; Print sorted Array
using System class { static void mergeTwoHalf ( int [ ] A , int n ) { int half_i = 0 int int [ ] temp = new int [ n ] for ( i = 0 i < n - 1 i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { half_i = i + 1 break } } if ( half_i == 0 ) return = 0 int = half_i int = 0 while ( i < half_i & & j < n ) { if ( A [ i ] < A [ j ] ) temp [ k ++ ] = A [ i ++ ] else [ k ++ ] = A [ j ++ ] } while ( i < half_i ) temp [ k ++ ] = A [ i ++ ] while ( j < n ) temp [ k ++ ] = A [ j ++ ] for ( i = 0 i < n i ++ ) A [ i ] = temp [ i ] } static public void ( ) { int [ ] A = { 2 , 3 , 8 , - 1 , 7 , 10 } int n = A . Length mergeTwoHalf ( A , n ) for ( int i = 0 i < n i ++ ) Console . Write ( A [ i ] + " ▁ " ) } }