improved select algorithm