[Java] QuickSort na stosie

Witam, mam mały problemik z wynikami jakie wyrzuca mi program. Otóż kiedy napisałem go używając wbudowanego stosu, działał poprawnie. Jednak kiedy napisałem swój własny stos program wypisuje złą odpowiedź. Brak jest jakichkolwiek błędów wypisywanych przez kompilator.

Dla wejścia:

1 - ilość zestawów danych

5 - ilość elementów

11 - elementy

8

7

6

5

Wyrzuca: 5 7 8 6 11 Zamiast: 5 6 7 8 11 Kod:

import java.util.Random;

import java.util.Scanner;


public class Source1 

{

    public static Scanner scan = new Scanner(System.in);


    public static void main(String[] args) 

    {

        int l = scan.nextInt();

        while (l > 0) 

        {

            int n = scan.nextInt();

            Source1 tab = new Source1(n);


            for (int i = 0; i < n; i++) 

            {

                tab.t[i] = scan.nextLong();

            }

            tab.quickSort();


            for (int i = 0; i < n; i++) 

            {

                System.out.print(tab.t[i] + " ");

            }

            System.out.println("");


            l--;

        }

    }


    private long[] t;

    private int size;

    java.util.Random random;


    public Source1(int size) 

    {

        this.t = new long[size];

        this.size = size;

        random = new Random();

    }


    private int partition(int l, int r) 

    {

        int pivot = random.nextInt(r - l) + l;

        long v = t[pivot];

        int i = l - 1;

        int j = r + 1;

        do 

        {

            do 

            {

                i++;

            } while (i < r && t[i] < v);


            do 

            {

                j--;

            } while (j >= l && t[j] > v);


            if (i < j) 

            {

                long tmp = t[i];

                t[i] = t[j];

                t[j] = tmp;

            }

        } while (i < j);

        return j;

    }


    public void quickSort() 

    {

        Stos stos = new Stos(50);

        stos.push(new Pair(0, size - 1));


        do 

        {

            Pair p = stos.pop();

            int l = p.l;

            int r = p.r;

            while (l < r) 

            {

                int j = partition(l, r);

                if (j - l > r - j) 

                {

                    stos.push(new Pair(l, j - 1));

                    l = j + 1;

                } else {

                    stos.push(new Pair(j + 1, r));

                    r = j - 1;

                }

            }

        } while (!stos.pusty());

    }


    private class Pair 

    {

        int l, r;


        public Pair(int l, int r) 

        {

            this.l = l;

            this.r = r;

        }

    }


    public static class Stos

    {

	private Pair[] Elem;

	private int szczyt;


	public Stos(int rozmiar)

	{

            Elem = new Pair[rozmiar];

            szczyt = -1;

	}


	public void push(Pair x)

	{

            if(szczyt + 1 == Elem.length)

            {

                powieksz();

            }


            Elem[++szczyt] = x;

	}


	public Pair pop() 

        {

            return Elem[szczyt--];

        }


        public void powieksz()

        {

            Pair[] nElem = new Pair[Elem.length * 2];


            for(int i = 0; i < Elem.length; i++)

            {

                nElem[i] = Elem[i];

            }


            Elem = nElem;

        }


        public boolean pusty()

        {

            if(szczyt == -1)

            {

                return true;

            } else {

                return false;

            }

        }

    }

}

A jest jakiś konkretny cel, dla którego chcesz stosować własny stos?