we add the condition i <= sortedIndex because we don 't want to traverse  on the sorted part of the queue,  which is the right part.
if ( curr <= min_val && i <= sortedIndex ) { min_index = i ; min_val = curr ; }