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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Sort ( vector < int > & ans , int num ) { int pos = -1 ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans [ i ] >= num ) { pos = i ; break ; } } if ( pos == -1 ) ans . push_back ( num ) ; else ans . insert ( ans . begin ( ) + pos , num ) ; } void sortStream ( int arr [ ] , int N ) { vector < int > ans ; for ( int i = 0 ; i < N ; i ++ ) { Sort ( ans , arr [ i ] ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] << " ▁ " ; } cout << endl ; } } int main ( ) { int arr [ ] = { 4 , 1 , 7 , 6 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortStream ( arr , N ) ; return 0 ; }