Wu Yuxiong - born natural data structure: Ten classic sort algorithm - HEAPSORT

Heap sort (Heapsort) refers to a sorting algorithm such a data structure designed for use heap. Accumulation is a complete binary tree structure of approximation, while meeting the bulk properties: i.e. the key or index sub-node is always less than (or greater than) its parent node. Heap sort it can be said is a use of the concept of heap to sort choose Sort. Divided into two methods: 

the large top stack: the value of each node is equal to or greater than the value of the child node, in ascending order for heap sort algorithm; 
minor vertex stack: the value of each node is less than or equal to its sub value of the node, in descending order for heap sort algorithm; 
average time complexity for hEAPSORT Ο (nlogn). 

1 . Algorithm steps 
to create a stack of H [n-0 ...... -1 ]; 

the first stack (maximum) tail and a stack exchange; 

the downsizing stack 1 , and call shift_down (0), the aim of the new array adjusting data corresponding to the top position; 

repeat step 2 until the size of a stack.
Code for 
the JavaScript 
var len;     // because the function declaration of the plurality of data are required length, to be set to the global variable len 

function buildMaxHeap (ARR) {    // build large top stack 
    len = arr.length;
     for (var I = the Math .floor (len / 2); I> = 0; i-- ) { 
        heapify (ARR, I); 
    } 
} 

function heapify (ARR, I) {      // stack adjusted 
    var left = 2 * I +. 1 , 
        right = * I 2 + 2 , 
        Largest = I; 

    IF (left <len && ARR [left]> ARR [Largest]) { 
        Largest = left; 
    } 

    IF (right < len && arr[right] > arr[largest]) {
        largest = right;
    }

    if (largest != i) {
        swap(arr, i, largest);
        heapify(arr, largest);
    }
}

function swap(arr, i, j) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

function heapSort(arr) {
    buildMaxHeap(arr);

    for (var i = arr.length-1; i > 0; i--) {
        swap(arr, 0, i);
        len--;
        heapify(arr, 0);
    }
    return arr;
}
Python
def buildMaxHeap(arr):
    import math
    for i in range(math.floor(len(arr)/2),-1,-1):
        heapify(arr,i)

def heapify(arr, i):
    left = 2*i+1
    right = 2*i+2
    largest = i
    if left < arrLen and arr[left] > arr[largest]:
        largest = left
    if right < arrLen and arr[right] > arr[largest]:
        largest = right

    if largest != i:
        swap(arr, i, largest)
        heapify(arr, largest)

def swap(arr, i, j):
    arr[i], arr[j] = arr[j], arr[i]

def heapSort(arr):
    global arrLen
    arrLen = len(arr)
    buildMaxHeap(arr)
    for i in range(len(arr)-1,0,-1):
        swap(arr,0,i)
        arrLen -=1
        heapify(arr, 0)
    return arr
Go
func heapSort(arr []int) []int {
        arrLen := len(arr)
        buildMaxHeap(arr, arrLen)
        for i := arrLen - 1; i >= 0; i-- {
                swap(arr, 0, i)
                arrLen -= 1
                heapify(arr, 0, arrLen)
        }
        return arr
}

func buildMaxHeap(arr []int, arrLen int) {
        for i := arrLen / 2; i >= 0; i-- {
                heapify(arr, i, arrLen)
        }
}

func heapify(arr []int, i, arrLen int) {
        left := 2*i + 1
        right := 2*i + 2
        largest := i
        if left < arrLen && arr[left] > arr[largest] {
                largest = left
        }
        if right < arrLen && arr[right] > arr[largest] {
                largest = right
        }
        if largest != i {
                swap(arr, i, largest)
                heapify(arr, largest, arrLen)
        }
}

func swap(arr []int, i, j int) {
        arr[i], arr[j] = arr[j], arr[i]
}
Java
public class HeapSort implements IArraySort {

    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝,不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        int len = arr.length;

        buildMaxHeap(arr, len);

        for (int i = len - 1; i > 0; i--) {
            swap(arr, 0, i);
            len--;
            heapify(arr, 0, len);
        }
        return arr;
    }

    private void buildMaxHeap(int[] arr, int len) {
        for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
            heapify(arr, i, len);
        }
    }

    private void heapify(int[] arr, int i, int len) {
        int left = 2 * i + 1;
        int right = 2 * i + 2;
        int largest = i;

        if (left < len && arr[left] > arr[largest]) {
            largest = left;
        }

        if (right < len && arr[right] > arr[largest]) {
            largest = right;
        }

        if (largest != i) {
            swap(arr, i, largest);
            heapify(arr, largest, len);
        }
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
PHP
function buildMaxHeap(&$arr)
{
    global $len;
    for ($i = floor($len/2); $i >= 0; $i--) {
        heapify($arr, $i);
    }
}

function heapify(&$arr, $i)
{
    global $len;
    $left = 2 * $i + 1;
    $right = 2 * $i + 2;
    $largest = $i;

    if ($left < $len && $arr[$left] > $arr[$largest]) {
        $largest = $left;
    }

    if ($right < $len && $arr[$right] > $arr[$largest]) {
        $largest = $right;
    }

    if ($largest != $i) {
        swap($arr, $i, $largest);
        heapify($arr, $largest);
    }
}

function swap(&$arr, $i, $j)
{
    $temp = $arr[$i];
    $arr[$i] = $arr[$j];
    $arr[$j] = $temp;
}

function heapSort($arr) {
    global $len;
    $len = count($arr);
    buildMaxHeap($arr);
    for ($i = count($arr) - 1; $i > 0; $i--) {
        swap($arr, 0, $i);
        $len--;
        heapify($arr, 0);
    }
    return $arr;
}
C
 # the include <stdio.h> 
# the include <stdlib.h> 

void the swap (int * A, * int B) { 
    int TEMP = * B;
     * B * = A;
     * A = TEMP; 
} 

void max_heapify (int ARR [], int Start, int End) {
     // build parent index and the child node index 
    int DAD = Start; 
    int son = DAD * 2 +. 1 ;
     the while (son <= End) {// If the child node index was in the range of compared
         IF (son +. 1 <= End && ARR [son] <ARR [son +. 1]) // first compares the size of the two child nodes, select the maximum 
            son ++ ;
         IF(arr [dad]> arr [ son]) // If the child node representing the parent node is greater than the adjustment is completed, directly out function
             return ;
         the else {// else continue exchanging content Sons child node and a grandchild node comparing 
            the swap ( & ARR [DAD] , & ARR [Son]); 
            DAD = Son; 
            Son = 2 * + DAD. 1 ; 
        } 
    } 
} 

void heap_sort (ARR int [], int len) { 
    int I;
     // initialize, i began to adjust from the last parent node
     for (I = len / 2 -. 1; I> = 0; i-- ) 
        max_heapify (ARR, I, len -. 1 );
     // first element and a first row is done well before the exchange element, then re-adjust until sorted
     for (i = len - 1; i > 0; i--) {
        swap(&arr[0], &arr[i]);
        max_heapify(arr, 0, i - 1);
    }
}

int main() {
    int arr[] = { 3, 5, 3, 0, 8, 6, 1, 5, 8, 6, 2, 4, 9, 4, 7, 0, 1, 8, 9, 7, 3, 1, 2, 5, 9, 7, 4, 0, 2, 6 };
    int len = (int) sizeof(arr) / sizeof(*arr);
    heap_sort(arr, len);
    int i;
    for (i = 0; i < len; i++)
        printf("%d ", arr[i]);
    printf("\n");
    return 0;
}
C ++
 # the include <the iostream> 
# the include <algorithm> 
the using namespace STD; 

void max_heapify (int ARR [], int Start, int End) {
     // build parent index and the child node index 
    int DAD = Start; 
    int Son = DAD * 2 . 1 + ;
     the while (son <= End) {// If the child node index in the range of only compared
         IF (son +. 1 <= End && ARR [son] <ARR [son +. 1]) // first compares two sub node size, select the maximum 
            son ++ ;
         IF (ARR [DAD]> ARR [son]) // If the child node representing the parent node is greater than the adjustment is completed, directly out function
             return ;
         the else {// else continue exchanging content Sons sub Compare node and a grandchild node
            the swap (ARR [DAD], ARR [Son]); 
            DAD = Son; 
            Son = 2 * + DAD. 1 ; 
        } 
    } 
} 

void heap_sort (ARR int [], int len) {
     // initialization, i from a parent node of the last began to adjust
     for (int len = I / 2 -. 1; I> = 0; i-- ) 
        max_heapify (ARR, I, len -. 1 );
     // first element and the first element of a front had been properly arranged in exchange, then the new adjustment (adjustment element immediately before the element) until the sorted
     for (int len = I -. 1; I> 0; i-- ) { 
        the swap (ARR [0], ARR [I]); 
        max_heapify (ARR, 0, I -. 1 ); 
    } 
} 

int main () {
    int arr[] = { 3, 5, 3, 0, 8, 6, 1, 5, 8, 6, 2, 4, 9, 4, 7, 0, 1, 8, 9, 7, 3, 1, 2, 5, 9, 7, 4, 0, 2, 6 };
    int len = (int) sizeof(arr) / sizeof(*arr);
    heap_sort(arr, len);
    for (int i = 0; i < len; i++)
        cout << arr[i] << ' ';
    cout << endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/11973808.html