@margaret
Вот пример реализации сортировки Хоара для связанного списка в C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
using System; class LinkedListNode { public int Value; public LinkedListNode Next; } class LinkedList { public LinkedListNode Head; public void Add(int value) { var newNode = new LinkedListNode { Value = value }; if (Head == null) { Head = newNode; } else { var current = Head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void Sort() { if (Head == null || Head.Next == null) { return; // Already sorted or empty list } Head = Sort(Head); } private LinkedListNode Sort(LinkedListNode head) { if (head == null || head.Next == null) { return head; // Sorted list } LinkedListNode pivot = head; LinkedListNode lesserHead = null; LinkedListNode lesserTail = null; LinkedListNode greaterHead = null; LinkedListNode greaterTail = null; LinkedListNode current = head.Next; while (current != null) { LinkedListNode next = current.Next; if (current.Value < pivot.Value) { if (lesserHead == null) { lesserHead = current; lesserTail = current; } else { lesserTail.Next = current; lesserTail = current; } } else { if (greaterHead == null) { greaterHead = current; greaterTail = current; } else { greaterTail.Next = current; greaterTail = current; } } current = next; } if (lesserTail != null) { lesserTail.Next = null; } if (greaterTail != null) { greaterTail.Next = null; } LinkedListNode sortedLesser = Sort(lesserHead); LinkedListNode sortedGreater = Sort(greaterHead); if (sortedLesser == null) { head = pivot; } else { head = sortedLesser; LinkedListNode tail = head; while (tail.Next != null) { tail = tail.Next; } tail.Next = pivot; } pivot.Next = sortedGreater; return head; } } class Program { static void Main() { var list = new LinkedList(); list.Add(10); list.Add(5); list.Add(7); list.Add(3); list.Add(8); Console.WriteLine("Before sorting:"); PrintList(list); list.Sort(); Console.WriteLine("After sorting:"); PrintList(list); } static void PrintList(LinkedList list) { var current = list.Head; while (current != null) { Console.Write(current.Value + " "); current = current.Next; } Console.WriteLine(); } } |
В данном примере класс LinkedList
представляет связанный список, а класс LinkedListNode
представляет узел списка. Метод Sort
реализует сортировку Хоара (быструю сортировку) для связанного списка.
Пример вывода:
1 2 3 4 |
Before sorting: 10 5 7 3 8 After sorting: 3 5 7 8 10 |
@margaret
Вот пример реализации сортировки Хоара для связанного списка в C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
using System; class LinkedListNode { public int Value; public LinkedListNode Next; } class LinkedList { public LinkedListNode Head; public void Add(int value) { var newNode = new LinkedListNode { Value = value }; if (Head == null) { Head = newNode; } else { var current = Head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void Sort() { if (Head == null || Head.Next == null) { return; // Already sorted or empty list } Head = Sort(Head); } private LinkedListNode Sort(LinkedListNode head) { if (head == null || head.Next == null) { return head; // Sorted list } LinkedListNode pivot = head; LinkedListNode lesserHead = null; LinkedListNode lesserTail = null; LinkedListNode greaterHead = null; LinkedListNode greaterTail = null; LinkedListNode current = head.Next; while (current != null) { LinkedListNode next = current.Next; if (current.Value < pivot.Value) { if (lesserHead == null) { lesserHead = current; lesserTail = current; } else { lesserTail.Next = current; lesserTail = current; } } else { if (greaterHead == null) { greaterHead = current; greaterTail = current; } else { greaterTail.Next = current; greaterTail = current; } } current = next; } if (lesserTail != null) { lesserTail.Next = null; } if (greaterTail != null) { greaterTail.Next = null; } LinkedListNode sortedLesser = Sort(lesserHead); LinkedListNode sortedGreater = Sort(greaterHead); if (sortedLesser == null) { head = pivot; } else { head = sortedLesser; LinkedListNode tail = head; while (tail.Next != null) { tail = tail.Next; } tail.Next = pivot; } pivot.Next = sortedGreater; return head; } } class Program { static void Main() { var list = new LinkedList(); list.Add(10); list.Add(5); list.Add(7); list.Add(3); list.Add(8); Console.WriteLine("Before sorting:"); PrintList(list); list.Sort(); Console.WriteLine("After sorting:"); PrintList(list); } static void PrintList(LinkedList list) { var current = list.Head; while (current != null) { Console.Write(current.Value + " "); current = current.Next; } Console.WriteLine(); } } |
Этот код реализует сортировку Хоара для связанного списка в C#. Вы можете запустить этот код в среде разработки C# для увидения результата.