Sort a stream of integers | C # 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
using System ; using System . Collections . Generic ; class GFG { static void Sort ( List < int > ans , int num ) { int pos = - 1 ; for ( int i = 0 ; i < ans . Count ; i ++ ) { if ( ans [ i ] >= num ) { pos = i ; break ; } } if ( pos == - 1 ) ans . Add ( num ) ; else ans . Insert ( pos , num ) ; } static void sortStream ( int [ ] arr , int N ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Sort ( ans , arr [ i ] ) ; for ( int j = 0 ; j < ans . Count ; j ++ ) { Console . Write ( ans [ j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 7 , 6 , 2 } ; int N = arr . Length ; sortStream ( arr , N ) ; } }