堆排序 Java 代码


堆排序 代码

/*
* @(#)HeapSortAlgorithm.   1.0 95/06/23 Jason Harrison
*
* Copyright (c) 1995 University of British Columbia
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* UBC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UBC SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
/**
* A heap sort demonstration algorithm
* SortAlgorithm., Thu Oct 27 10:32:35 1994
*
* @author Jason Harrison@cs.ubc.ca
* @version     1.0, 23 Jun 1995
*/
class HeapSortAlgorithm extends SortAlgorithm {
    void sort(int a[]) throws Exception {
    int N = a.length;
    for (int k = N/2; k > 0; k--) {
        downheap(a, k, N);
        pause();
        }
    do {
            int T = a[0];
            a[0] = a[N - 1];
            a[N - 1] = T;
        N = N - 1;
        pause(N);
            downheap(a, 1, N);
    } while (N > 1);
    }
    void downheap(int a[], int k, int N) throws Exception {
    int T = a[k - 1];

            int j = k + k;

            j++;
        }
        if (T >= a[j - 1]) {
        break;
        } else {
                a[k - 1] = a[j - 1];
                k = j;
        pause();
            }
    }
        a[k - 1] = T;
    pause();
    }
}

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《堆排序 Java 代码
本文地址:https://www.zhiletu.com/archives-7831.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!