java sort -- heap sort

1. Introduction

       The last one talked about quick sort , and this one talks about heap sort. Heaps can be understood with the help of a complete binary tree, where the numerical ordering is given in the order of the outputs of the breadth-first algorithm. Heap sort is divided into large heap sort and small heap sort. Heap sort is done in one pass, and the largest value is at the top; while sorting by small heap is done in one pass, and the smallest value is at the top.

       The sorting process is to sort the record sequence according to the key non-decreasing order. In the heap sorting algorithm, a "big top heap" is first built, that is, a record with the largest key is selected first and is combined with the last record in the sequence. Swap, then sift through the first n-1 records of the sequence, resize it into a "big top heap", and so on until the sort ends.

Sorting process:


Since the data structure selected in advance is an array, the sorting process of the array is given.

Suppose the array value is

5,2,6,3,7,9,10,4,8

then its result after the first big heap sort is

10,8,9,4,7,5,6,2,3

2. Code implementation

public class HeapSort {
    public static void main(String[] args) {
        int a[] = {5, 2, 6, 3, 7, 9, 10, 4, 8};
        heapSort(a);
        print(a);
    }

    /**
     * 打印数组
     *
     * @param a
*/
private static void print(int[] a) {
        for (int i = 0; i < a.length; i++) {         
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }

    /**
      * Sort by heap
      *
      * @param a       array
      * * @param s sort index
      * * @param length array length
      */
 static void heapAdjust( int [] a, int s, int length) {
         int key = a[s];
         for ( int j = 2 * s + 1 ; j <= length; j = j * 2 + 1 ) {
             if (j < length && (j + 1 ) < length && a[j] < a[j + 1 ])      //If there are left and right nodes, you need to find the index of the largest value
                 j++;
             if (key > a[j]) {   //If the comparison keyword is greater than the subsequent array value, jump out of the need directly. To put it bluntly, what is recorded at this moment is still The current index value is the maximum value
 break ;                
            }
            a[s] = a[j];   //You need to assign the largest value to the previous index
             s = j;         //Update the current index
         }
        a[s] = key;   //Assign the key to the last changed index
     }

    /**
      * heap sort
      *
      * @param a array
      */
 static void heapSort( int [] a) {
         for ( int i = a. length / 2 - 1 ; i >= 0 ; --i) {   //The first HeapAdjust
 (a, i, a.length );   //sort by
 heap }
         for ( int i = a.length - 1 ; i > 0 ; i--) {
             int temp = a[ 0 ];                        
            a[0] = a[i];
            a[i] = temp;   //Because it was sorted by a large heap before, so swap it to get the order from small to large
 heapAdjust (a, 0 , i - 1 );   //Every time the sorting is completed, the length needs to be reduced 1
 }                    
    }
}

The comment code has been written very clearly, the result:

2 3 4 5 6 7 8 9 10 

3. Summary

Heapsort is also nlog(n) in terms of average time. The heap sort method is not recommended for files with a small number of records, but it is still very effective for files with relatively large n. Also it is sort of unstable.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644809&siteId=291194637