Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by anonymous125132 ( 6 years ago )
public class MySort {

    public static void sort(MyArray myarray) {
        int n = myarray.length;
        heapSort(myarray, n);
    }

    public static void buildMaxHeap(MyArray myarray, int n) {
        for (int i = 1; i < n; i++)
            if (myarray.comp(i, (i - 1) / 2) == 1) {
                int j = i;
                while (myarray.comp(j, (j - 1) / 2) == 1) {
                    myarray.swap(j, (j - 1) / 2);
                    j = (j - 1) / 2;
                }
            }

    }

    public static void heapSort(MyArray myarray, int n) {
        buildMaxHeap(myarray, n);
        for (int i = n - 1; i > 0; i--) {
            myarray.swap(0, i);
            int j = 0, index;
            do {
                index = (2 * j + 1);
                if (index < (i - 1) && (myarray.comp(index, index + 1) == -1))
                    index++;
                if (index < i && (myarray.comp(j, index) == -1))
                    myarray.swap(j, index);
                j = index;
            } while (index < i);
        }
    }
}

 

Revise this Paste

Children: 113464 113465
Your Name: Code Language: