Sort a stream of integers | Java program for the above approach ; 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
import java . util . * ; class GFG { static void Sort ( Vector < Integer > ans , int num ) { int pos = - 1 ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans . get ( i ) >= num ) { pos = i ; break ; } } if ( pos == - 1 ) ans . add ( num ) ; else ans . add ( pos , num ) ; } static void sortStream ( int arr [ ] , int N ) { Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Sort ( ans , arr [ i ] ) ; for ( int j = 0 ; j < ans . size ( ) ; j ++ ) { System . out . print ( ans . get ( j ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 1 , 7 , 6 , 2 } ; int N = arr . length ; sortStream ( arr , N ) ; } }