Sort an array when two halves are sorted | 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
def mergeTwoHalf ( A , n ) : NEW_LINE INDENT half_i = 0 NEW_LINE temp = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( A [ i ] > A [ i + 1 ] ) : NEW_LINE INDENT half_i = i + 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( half_i == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT i = 0 NEW_LINE j = half_i NEW_LINE k = 0 NEW_LINE while ( i < half_i and j < n ) : NEW_LINE INDENT if ( A [ i ] < A [ j ] ) : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT while i < half_i : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( j < n ) : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT A [ i ] = temp [ i ] NEW_LINE DEDENT DEDENT A = [ 2 , 3 , 8 , - 1 , 7 , 10 ] NEW_LINE n = len ( A ) NEW_LINE mergeTwoHalf ( A , n ) NEW_LINE print ( * A , sep = ' ▁ ' ) NEW_LINE