Ten sorting algorithms -- detailed explanation of heap sorting (Java source code)

Keywords: less

Heap sorting is a sort algorithm designed by using the data structure of heap. Stack is a structure of nearly complete binary tree, which satisfies the properties of stack at the same time: the key value or index total number of the child node is less than (or greater than) its parent node.

  1. Algorithm description
    1. The initial sequence to be sorted is constructed into a large top heap, which is the initial disordered area;
    2. Exchange the top element R[1] with the last element R [n], and a new unordered region (R1,R2,...) is obtained Rn-1) and a new ordered region (Rn), which satisfy R[1,2 N-1]<=R[n];
    3. Since the new top of the heap may violate the nature of the heap after exchange, the current unordered area (R1,R2,...) needs to be changed Rn-1) is adjusted to a new heap, and then R[1] is adjusted again

By exchanging with the last element of the disordered region, a new disordered region (R1,R2 Rn-2) and a new ordered region (Rn-1,Rn). Repeat this process until the number of elements in the ordered area is n-1, then the whole sorting process is completed.

  1. Code implementation: the complete binary tree has a feature: the left child node position = twice the current parent node + 1, the right child node position = twice the current parent node + 2.
  2. Code implementation:
public class DuiPaiXu2 {



       private static void daDingDui(int[] arrays, int start, int end) {



              // Parent node

              int curr = start;

              // Left and right nodes

              int left = curr * 2 + 1;

//           int right = curr * 2 + 2;



              // Father's day value

              int temp = arrays[curr];



              for (; left < end; curr = left, left = curr * 2 + 1) {// Change its left child node into a parent node, and get the left child node according to its transformed parent node

                    

                     if (left < end && arrays[left] < arrays[left + 1]) {// Determine which child node is the largest

                            left++;// Change to the right child node as the parent node,

                     }



                     if (arrays[left] > temp) {// Determine the maximum value of the left and right child nodes and the value of the current parent node

                            arrays[curr] = arrays[left];

                            arrays[left] = temp;

                     }



              }

       }



       private static void jiaoHuan(int[] arrays, int index1, int index2) {

              int temp = arrays[index1];

              arrays[index1] = arrays[index2];

              arrays[index2] = temp;

       }



       private static void duiPaiXu(int[] arrays) {

              int length = arrays.length;

              int left;



              // Traverse from (n/2-1) -- > 0 to gradually generate a large top heap for the sequence to be sorted

              for (left = length / 2 - 1; left > 0; left--) {

                     daDingDui(arrays, left, length - 1);

              }



              // Adjust the sequence from the last element, and continuously narrow the adjustment range until it contains only the first element

              for (left = length - 1; left > 0; left--) {

//After exchanging the first and left child node elements, the left child node is the largest element in the sequence.

                     jiaoHuan(arrays, 0, left);



                     // Adjust the remaining heap sequence to ensure that the right child node is the maximum value in the remaining heap sequence

                     daDingDui(arrays, 0, left - 1);

              }



       }



       public static void main(String[] args) {

              int arrays[] = { 20, 30, 90, 40, 70, 110, 60, 10, 100, 50, 80 };



              System.out.println(Arrays.toString(arrays));

              duiPaiXu(arrays);

              System.out.println(Arrays.toString(arrays));



       }

}

 

Posted by Pie on Mon, 02 Dec 2019 20:35:16 -0800