Bir bog‘lamli ro‘yxatlar ustida amallar va ularning algoritmlari


Download 401.64 Kb.
bet4/4
Sana28.12.2022
Hajmi401.64 Kb.
#1015166
1   2   3   4
Bog'liq
usmonov.mt

singlyLinkedList *getTail(singlyLinkedList *cur)
{
while (cur != NULL && cur->next != NULL) cur = cur->next; return cur;
}
// quick sort uchun tayanch elementni qaytaruvchi metod
singlyLinkedList *pivotion(singlyLinkedList *head, singlyLinkedList *end, singlyLinkedList **newHead, singlyLinkedList **newEnd)
{ singlyLinkedList *pivot = end;
singlyLinkedList *prev = NULL, *cur = head, *tail = pivot; while (cur != pivot)
{ if (cur->data < pivot->data) {




if (*newHead == NULL) *newHead = cur; prev = cur; cur = cur->next;
} else { if (prev) prev->next = cur->next; singlyLinkedList *tmp = cur->next; cur->next = NULL; tail->next = cur; tail = cur; cur = tmp;
} } if (*newHead == NULL)
*newHead = pivot; *newEnd = tail; return pivot;
}
// quick Sort uchun rekrussiv funksiya singlyLinkedList *quickSortRecur(singlyLinkedList *head, singlyLinkedList *end)
{ if (!head || head == end) return head; singlyLinkedList *newHead = NULL, *newEnd = NULL; singlyLinkedList *pivot = pivotion(head, end, &newHead, &newEnd); if (newHead != pivot)
{ singlyLinkedList *tmp = newHead; while (tmp->next != pivot) tmp = tmp->next; tmp->next = NULL; newHead = quickSortRecur(newHead, tmp); tmp = getTail(newHead); tmp->next = pivot;
} pivot->next = quickSortRecur(pivot->next, newEnd); return newHead;
} void quickSort(singlyLinkedList **headRef)
{
*headRef = quickSortRecur(*headRef, getTail(*headRef)); return;
}
int main()
{ srand(time(NULL));

singlyLinkedList *list = NULL; for (int i = 0; i < 10; i++) pushList(&list, rand() % 100); cout << "\n Bir bog'lamli ro'yhatning dastlabki randomda kiritilgan elementlari:\n\n"; showList(list); quickSort(&list); cout << "\n Bir bog'lamli ro'yhatni quick sort saralash algoritimi orqali saraladik:\n\n"; showList(list); system("Pause"); return 0; }
Dastur natijasi:
Bir bog'lamli ro'yhatning dastlabki randomda kiritilgan elementlari:
34, 31, 83, 95, 75, 95, 90, 11, 75, 21
Bir bog'lamli ro'yhatni quick sort saralash algoritimi orqali saraladik: 11, 21, 31, 34, 75, 75, 83, 90, 95, 95
Download 401.64 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling