Input:
: an unsorted list of length
FirstIndex: the first index of to be sorted
LastIndex: the last index of to be sorted
: top- elements of are to be sorted
Output:
: Sorted top- elements of ranging from FirstIndex to LastIndex
: a stack of pivot positons useful for the afterwards selection sorts
(1)if FirstIndex < LastIndex then
(2)  if !isempty( ) & top( ) < LastIndex then  /* the stack is available */
(3)   pivotpos = pop( )  /* use the top element of the stack as the pivot */
(4)  else
(5)   /* Partition without using pivots from the stack */
(6)   pivotpos=Partition(L, FirstIndex, LastIndex)
(7)  if  pivotpos− FirstIndex +1>   then /* The pivot falls after position  */
(8)   /* The pivot may be useful for the afterwards steps */
(9)   push(pivotpos, )
(10)   /* QuickSort for top-k in the upper part */
(11)   QuickSortTopK(L, FirstIndex, pivotpos −1, k)
(12) /* The pivot is located exactly at position  */
(13) elseif(pivotpos− FirstIndex +1== )
(14)   /* QuickSort for top- in the upper part */
(15)   QuickSortTopK(L, FirstIndex, pivotpos −1, )
(16)   else  /* The pivot falls prior to position k */
(17)    /* QuickSort the upper part */
(18)    QuickSortTopK(L, FirstIndex, pivotpos −1, pivotpos− FirstIndex)
(19)    /* QuickSort for the residual elements in the lower part */
(20)    QuickSortTopK( , pivotpos +1, FirstIndex, -pivotpos+ FirstIndex −1)
Algorithm 1: QuickSortTopK( , FirstIndex, LastIndex, ).