Top 10 Classic Sorting: Selective Sorting Heap Sorting

Select Sort

Selection Sorting Principle

What is Choice Sorting? Select Sort is to select the smallest one in the first iteration of an array arr[n], swap the position with arr[0], put the smallest number in the first place, then the second iteration, place the next smallest one in the next selection, place the arr[1], then the third, the fourth...iterate over the largest element and place it in the position of arr[n]. This sets of data becomes organized.

Selective sorting is an unstable sorting method. Each ith traversal finds N-i unit time to execute and then N times to execute, so the time complexity is o. n 2 n^{2 } n2), better for sorting smaller columns.

The idea is to start at the first position and move backwards gradually, choosing the smallest value in the sequence that follows to put it there. Simply put, choose the largest number in each group at the end or the top each time.

Algorithm implementation

1. Algorithmic Description

First, the smallest (large) element is found in the unsorted sequence and stored at the beginning of the sequence. Then, the next largest element in the remaining number is placed in the second-to-last position, and so on, until all elements are sorted.

2. Diagram

3. Algorithmic space and time complexity
Time Complexity:

  • Worst: o( n 2 n^{2 } n2)
  • Best: o( n 2 n^{2} n2)
  • Average: o( n 2 n^{2 } n2)

Spatial Complexity (Auxiliary Storage): o(1)
Stability: Unstable

Example

Output the following columns from smallest to largest in selective order: 123,45,6,22,99,1,38,41, -6,0

java code:

public class Test {
    public static void selectSort(int[] arr) {
        if (arr == null || arr.length == 1) return;

            for (int i = 0; i < arr.length - 1; i++) {
                // minIndex is the index of the smallest element.
                int min = i;
                // Find the index value of the smallest element and assign it to minIndex.
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[j] < arr[min]) {
                        min = j;
                    }
                }
                // Exchange Elements
                arr[i] = arr[i] ^ arr[min];
                arr[min] = arr[i] ^ arr[min];
                arr[i]= arr[i] ^ arr[min];

            }
    }


    public static void main(String[] args) {
        int[] arr=new int[]{123,45,6,22,99,1,38,41,-6,0};
        //Select Sort
        selectSort(arr);

        System.out.println("Select the sorted result:");
        for(int i=0;i<arr.length;i++)
            System.out.print(arr[i]+"\t");
    }

}

Heap Sorting

Heap sorting principle

Heap sorting is an optimized sort for selection sorting, which utilizes the nature of the largest (smallest) number of heap tops so that finding an array to find the largest (smallest) elements does not require N-i time as selection sorting does.

A heap is a sequentially stored complete binary tree.

  • The keywords of each node are no larger than those of its child node, and such a heap is called a small root heap.
  • Each of these nodes has no fewer keywords than its child nodes, and such a heap is called the big root heap.

Let's take the big root heap as an example. We swap the top of the heap with the last position, then the rest of the elements continue to adjust the heap, and then swap the top element with the second last position until all the numbers are in order.

Algorithm implementation

1. Algorithmic Description
In the data structure of a heap, the maximum value in the heap is always at the root node (the minimum value in the heap using the heap in the priority queue is at the root node). The following operations are defined in the heap:

  • Max Heapify: Adjusts the end child node of the heap so that the child node is always smaller than the parent node
  • Build Max Heap: Reorder all data in the heap
  • HeapSort: Remove the root node that is positioned at the first data and do a recursive operation for maximum heap adjustment

Steps:

  1. Create a heap H[0...n-1];
  2. Swap the heap head (maximum) with the heap tail;
  3. Reduce heap size by 1 and call shift_down(0), the purpose is to adjust the top data of the new array to the corresponding position;
  4. Repeat step 2 until the heap size is 1.

2. Diagram

3. Algorithmic space and time complexity
Spatial Complexity:

  • Worst: o( n log ⁡ 2 n n\log _{2}n nlog2​n)
  • Best: o( n log ⁡ 2 n n\log _{2}n nlog2​n)
  • Average: o( n log ⁡ 2 n n\log _{2}n nlog2​n)

Time Complexity (Auxiliary Storage): o ( 1 1 1)
Stability: Unstable

Example

Sort by heap to output the following columns in order from smallest to largest:
66,13,51,76,81,26,57,69,23

java code:


public class Test {
    public static void headSort(int[] list) {
        //Construct the initial heap, start with the first non-leaf node, and switch the larger of the left and right child nodes to the parent node
        for (int i = (list.length) / 2 - 1; i >= 0; i--) {
            headAdjust(list, list.length, i);
        }
        //Sort, place the largest node at the end of the heap, and then readjust from the root node
        for (int i = list.length - 1; i >= 1; i--) {
            list[0] = list[0] ^ list[i];
            list[i] = list[0] ^ list[i];
            list[0] = list[0] ^ list[i];
            headAdjust(list, i, 0);
        }
    }

    private static void headAdjust(int[] list, int len, int i) {
        int k = i, temp = list[i], index = 2 * k + 1;
        while (index < len) {
            if (index + 1 < len) {
                if (list[index] < list[index + 1]) {
                    index = index + 1;
                }
            }
            if (list[index] > temp) {
                list[k] = list[index];
                k = index;
                index = 2 * k + 1;
            } else {
                break;
            }
        }
        list[k] = temp;
    }


    public static void main(String[] args) {
        int[] arr=new int[]{66,13,51,76,81,26,57,69,23};
        //Heap Sorting
        headSort(arr);

        System.out.println("The result of the heap sorting is:");
        for(int i=0;i<arr.length;i++)
            System.out.print(arr[i]+"\t");
    }

}

Tags: Java Algorithm

Posted on Fri, 26 Nov 2021 15:16:55 -0500 by nenena