Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. heap_sort(List, SortedList):-list_heap(List, Heap),heap_list(Heap, SortedList).
  2. list_heap(List, Heap):-list_heap_1(List, void, Heap).
  3. list_heap_1([X|Xs], Heap0, Heap):-insert_heap(X, Heap0, Heap1),list_heap_1(Xs, Heap1, Heap).
  4. list_heap_1([], Heap, Heap).
  5. heap_list(void, []):-!.
  6. heap_list(Heap, [X|Xs]):-remove_heap(X, Heap, Heap1),heap_list(Heap1, Xs).
  7. insert_heap(Value, heap(Top, Left, Right), heap(Top, Right, Left1)):-Value < Top, !,insert_heap(Value, Left, Left1).
  8. insert_heap(Value, heap(Top, Left, Right), heap(Value, Right, Left1)):-insert_heap(Top, Left, Left1).
  9. insert_heap(Value, void, heap(Value, void, void)).
  10. remove_heap(Top, heap(Top, void, Right), Right):-!.
  11. remove_heap(Top, heap(Top, Left, Right), NewHeap):-remove_heap_1(Value1, Right, Right1),heap(heap(Value1, Right1, Left), NewHeap).
  12. remove_heap_1(Top, heap(Top, void, Right), Right):-!.
  13. remove_heap_1(Value, heap(Top, Left, Right), heap(Top, Left1, Left)):-remove_heap_1(Value, Right, Left1).
  14. heap(heap(Top, heap(LeftValue, LeftLeft, LeftRight),heap(RightValue, RightLeft, RightRight)),heap(LeftValue, Left, heap(RightValue, RightLeft, RightRight))):-RightValue < LeftValue, Top < LeftValue, !,
  15. heap(heap(Top, LeftLeft, LeftRight), Left).
  16. heap(heap(Top, Left, heap(RightValue, RightLeft, RightRight)),heap(RightValue, Left, Right)):-Top < RightValue, !,heap(heap(Top, RightLeft, RightRight), Right).
  17. heap(Heap, Heap).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement