Sort a stream of integers | Function to sort a stream of integers ; Stores the position of array elements ; Traverse through the array ; If any element is found to be greater than the current element ; If an element > num is not present ; Otherwise , place the number at its right position ; Function to print the sorted stream of integers ; Stores the sorted stream of integers ; Traverse the array ; Function Call ; Print the array after every insertion ; Driver Code
def Sort ( ans , num ) : NEW_LINE INDENT pos = - 1 ; NEW_LINE for i in range ( len ( ans ) ) : NEW_LINE INDENT if ( ans [ i ] >= num ) : NEW_LINE INDENT pos = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( pos == - 1 ) : NEW_LINE INDENT ans . append ( num ) ; NEW_LINE DEDENT else : NEW_LINE INDENT ans . insert ( pos , num ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def sortStream ( arr , N ) : NEW_LINE INDENT ans = list ( ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans = Sort ( ans , arr [ i ] ) ; NEW_LINE for j in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 1 , 7 , 6 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE sortStream ( arr , N ) ; NEW_LINE DEDENT