refactor qsort and changed the algorithm