When I was reading the complexity of the merge sort space, I found the complexity of the place which is O (N + logn) . O (Logan) is calculated when we consider the stack frame size of recurring processes.
But the heaport also uses the recursive process, which is the Hepipi process, why the complexity of the heapsort space O (1) ?
and the code I am reading
`Java ' HeapSort {Public Zero Buildheap (int array []) {int length = Array.length; Int heapsize = length; Int nonleaf = length / 2 - 1; For (int i = nonleaf; i> = 0; i -) {heapify (array, i, heapsize); }} Public zero heapify (int array [], int i, int heapsize) {int minorest = i; Int Left = 2 * I + 1; Int correct = 2 * i + 2; If (left & lieutenant; pile) {if (array [i]> array [left]) {small = left; } And small = i; } If (Right and Lieutenant BP) {if (array [the smallest]> array [right]) {smallest = right; }} If (smallest! = I) {int temp; Temp = array [i]; Array [i] = array [smallest]; Array [smallest] = floating; Heapify (array, short, heapsize); }} Public Zero Heapsort (int array []) {int heapsize = array.length; Buildheap (array); (Int i = 0; i & lt; array.lambi-1; i ++) {// swap first and last integer temporary; Temp = array [0]; Array [0] = array [heapsize-1]; Array [heapsize-1] = temporary; // array heapsize = heapsize - 1; Heapify (array, 0, heapsize); }} ``
this heapify ( ) Function can be implemented in tail-recursively several functional languages are guaranteed that the re-recursive functions use stack space of the stationary location.
No comments:
Post a Comment