Advertisement
believe_me

Untitled

May 23rd, 2022
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.17 KB | None | 0 0
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils;
  9.  
  10. type
  11.     TNode = ^SingleList;
  12.     TNodePointer = ^TNode;
  13.  
  14.     SingleList = Record
  15.         data: integer;
  16.         next: TNode;
  17.     End;
  18.  
  19. procedure push(var Header: TNode; Data: integer);
  20.  
  21. var
  22.     NewNode: TNode;
  23.  
  24. begin
  25.     new(NewNode);
  26.     NewNode^.data := Data;
  27.     NewNode^.next := Header^.next;
  28.     Header^.next := newNode;
  29. end;
  30.  
  31. procedure printList(Node: TNode);
  32.  
  33. begin
  34.     while (Node <> nil ) do
  35.     begin
  36.         write(Node^.data, ' ');
  37.         Node := Node^.next;
  38.     end;
  39.     writeln;
  40. end;
  41.  
  42. function getTail(Node: TNode): TNode;
  43.  
  44. begin
  45.     while (Node <> nil) and (Node^.next <> nil) do
  46.         Node := Node^.next;
  47.     Result := Node;
  48.  
  49. end;
  50.  
  51. // Partitions the list taking the last element as the pivot
  52. function partition(head: TNode; fin: TNode;
  53.                        var newHead: TNode;
  54.                        var newEnd: TNode): TNode;
  55.  
  56. var
  57.     pivot: TNode;
  58.     prev: TNode;
  59.     cur: TNode;
  60.     tail: TNode;
  61.     tmp: TNode;
  62.  
  63.  
  64. begin
  65.     pivot := fin;
  66.     prev := nil;
  67.     cur := head;
  68.     tail := pivot;
  69.  
  70.     // During partition, both the head and end of the list
  71.     // might change which is updated in the newHead and
  72.     // newEnd variables
  73.     while (cur <> pivot) do
  74.     begin
  75.         if (cur^.data < pivot^.data) then
  76.         begin
  77.             // First node that has a value less than the
  78.             // pivot - becomes the new head
  79.             if (newHead = nil) then
  80.                 newHead := cur;
  81.  
  82.             prev := cur;
  83.             cur := cur^.next;
  84.         end
  85.         else // If cur node is greater than pivot
  86.         begin
  87.             // Move cur node to next of tail, and change
  88.             // tail
  89.             if (prev <> nil) then
  90.                 prev^.next := cur^.next;
  91.             tmp := cur^.next;
  92.             cur^.next := nil;
  93.             tail^.next := cur;
  94.             tail := cur;
  95.             cur := tmp;
  96.         end;
  97.     end;
  98.  
  99.     // If the pivot data is the smallest element in the
  100.     // current list, pivot becomes the head
  101.     if (newHead = nil) then
  102.         newHead := pivot;
  103.  
  104.     // Update newEnd to the current last node
  105.     newEnd := tail;
  106.  
  107.     // Return the pivot node
  108.     Result := pivot;
  109. end;
  110.  
  111. // here the sorting happens exclusive of the end node
  112. function quickSortRecur(head: TNode; fin: TNode): TNode;
  113.  
  114. var
  115.     newHead: TNode;
  116.     newEnd: TNode;
  117.     tmp: TNode;
  118.     pivot: TNode;
  119.  
  120.  
  121.  
  122. begin
  123.     // base condition
  124.     if ((not (head <> nil)) or (head = fin)) then
  125.         Result := head
  126.     else
  127.     begin
  128.         newHead := nil;
  129.         newEnd := nil;
  130.  
  131.     // Partition the list, newHead and newEnd will be
  132.     // updated by the partition function
  133.         pivot := partition(head, fin, newHead, newEnd);
  134.  
  135.     // If pivot is the smallest element - no need to recur
  136.     // for the left part.
  137.         if (newHead <> pivot) then
  138.         begin
  139.             // Set the node before the pivot node as nullptr
  140.             tmp := newHead;
  141.             while (tmp^.next <> pivot) do
  142.                 tmp := tmp^.next;
  143.             tmp^.next := nil;
  144.  
  145.             // Recur for the list before pivot
  146.             newHead := quickSortRecur(newHead, tmp);
  147.  
  148.             // Change next of last node of the left half to
  149.             // pivot
  150.             tmp := getTail(newHead);
  151.             tmp^.next := pivot;
  152.         end;
  153.  
  154.     // Recur for the list after the pivot element
  155.     pivot^.next := quickSortRecur(pivot^.next, newEnd);
  156.  
  157.     Result := newHead;
  158.     end;
  159. end;
  160.  
  161. procedure quickSort(var headRef: TNode);
  162.  
  163. var
  164.     Tail: TNode;
  165.  
  166. begin
  167.     Tail := getTail(headRef);
  168.     headRef := quickSortRecur(headRef, Tail);
  169. end;
  170.  
  171. var
  172.     Header: TNode;
  173.  
  174. begin
  175.     new(Header);
  176.     Header^.next := nil;
  177.  
  178.     push(Header, 5);
  179.   {  push(Header, 20);
  180.     push(Header, 4);
  181.     push(Header, 3);
  182.     push(Header, 30);
  183.     push(Header, 7);
  184.     push(Header, 4);
  185.     push(Header, 34);
  186.     push(Header, 1);  }
  187.     writeln('Linked List before sorting');
  188.     printList(Header^.next);
  189.     quickSort(Header^.next);
  190.     writeln('Linked List after sorting');
  191.     printList(Header^.next);
  192.     Readln;
  193. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement