0% found this document useful (0 votes)
15 views4 pages

Sorting (Advanced) (Page 1 of 5)

Uploaded by

ledinhduc1879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Sorting (Advanced) (Page 1 of 5)

Uploaded by

ledinhduc1879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/10/23, 12:45 AM Sorting (Advanced) (page 1 of 5)

https://e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=1579199&cmid=200652 1/4
Question 1
11/10/23, 12:45 AM Sorting (Advanced) (page 1 of 5)

Correct
Marked out of 2.00

The best way to sort a singly linked list given the head pointer is probably using merge sort.
Both Merge sort and Insertion sort can be used for linked lists. The slow random-access performance of a linked list makes other
algorithms (such as quick sort) perform poorly, and others (such as heap sort) completely impossible. Since worst case time
complexity of Merge Sort is O(nLogn) and Insertion sort is O(n^2), merge sort is preferred.
Additionally, Merge Sort for linked list only requires a small constant amount of auxiliary storage.
To gain a deeper understanding about Merge sort on linked lists, let's implement mergeLists and mergeSortList function below
Constraints:
0 <= list.length <= 10^4
0 <= node.val <= 10^6
Use the nodes in the original list and don't modify ListNode's val attribute.
struct ListNode {
int val;
ListNode* next;
ListNode(int _val = 0, ListNode* _next = nullptr) : val(_val), next(_next) { }
};

// Merge two sorted lists


ListNode* mergeSortList(ListNode* head);

// Sort an unsorted list given its head pointer


ListNode* mergeSortList(ListNode* head);

For example:
Test Input Result
int arr1[] = {1, 3, 5, 7, 9}; 1 2 3 4 5 6 7 8 9
int arr2[] = {2, 4, 6, 8};
unordered_map<ListNode*, int> nodeAddr;
ListNode* a = init(arr1, sizeof(arr1) / 4, nodeAddr);
ListNode* b = init(arr2, sizeof(arr2) / 4, nodeAddr);
ListNode* merged = mergeLists(a, b);
try {
printList(merged, nodeAddr);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(merged);

int size; 9 1 2 3 4 5 6 7 8 9
cin >> size; 9 3 8 2 1 6 7 4 5
int* array = new int[size];
for(int i = 0; i < size; i++) cin >> array[i];
unordered_map<ListNode*, int> nodeAddr;
ListNode* head = init(array, size, nodeAddr);
ListNode* sorted = mergeSortList(head);
try {
printList(sorted, nodeAddr);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(sorted);
delete[] array;

https://e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=1579199&cmid=200652 2/4
11/10/23, 12:45 AM Sorting (Advanced) (page 1 of 5)
Answer: (penalty regime: 0 %)
Reset answer
1 ▼ ListNode* mergeLists(ListNode* a, ListNode* b) {
2 ListNode* c = new ListNode(0);
3 ListNode* tmp = c;
4 ▼ while (a && b){
5 ▼ if (a -> val > b -> val){
6 tmp -> next = b;
7 b = b -> next;
8 }
9 ▼ else{
10 tmp -> next = a;
11 a = a -> next;
12 }
13 tmp = tmp -> next;
14 }
15 ▼ while (a){
16 tmp -> next = a;
17 a = a -> next;
18 tmp = tmp -> next;
19 }
20 ▼ while (b){
21 tmp -> next = b;
22 b = b -> next;
23 tmp = tmp -> next;
24 }
25 return c -> next;
26 }
27
28 // Sort and unsorted list given its head pointer
29
30 ▼ ListNode* mergeSortList(ListNode* head){
31 if (head == nullptr || head -> next == nullptr) return head;
32 ListNode *slow_ptr = head;
33 ListNode *fast_ptr = head;
34 ▼ if (fast_ptr -> next -> next != nullptr){
35 ▼ while (fast_ptr != nullptr && fast_ptr->next != nullptr) {
36 fast_ptr = fast_ptr->next->next;
37 slow_ptr = slow_ptr->next;
38 }
39 }
40 ListNode * temp slow ptr > next;

Precheck Check

Test Input Expected Got


 int arr1[] = {1, 3, 5, 7, 9}; 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 
int arr2[] = {2, 4, 6, 8}; 9 9
unordered_map<ListNode*, int> nodeAddr;
ListNode* a = init(arr1, sizeof(arr1) / 4,
nodeAddr);
ListNode* b = init(arr2, sizeof(arr2) / 4,
nodeAddr);
ListNode* merged = mergeLists(a, b);
try {
printList(merged, nodeAddr);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(merged);

https://e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=1579199&cmid=200652 3/4
11/10/23, 12:45 AM Sorting (Advanced) (page 1 of 5)

Test Input Expected Got


 int size; 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 
cin >> size; 9 3 8 2 1 6 7 4 9 9
int* array = new int[size]; 5
for(int i = 0; i < size; i++) cin >> array[i];
unordered_map<ListNode*, int> nodeAddr;
ListNode* head = init(array, size, nodeAddr);
ListNode* sorted = mergeSortList(head);
try {
printList(sorted, nodeAddr);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(sorted);
delete[] array;

Passed all tests! 

BÁCH KHOA E-LEARNING

WEBSITE

HCMUT
MyBK
BKSI
CONTACT
 268 Ly Thuong Kiet Street Ward 14, District 10, Ho Chi Minh City, Vietnam
 (028) 38 651 670 - (028) 38 647 256 (Ext: 5258, 5234)

 elearning@hcmut.edu.vn

Copyright 2007-2022 BKEL - Power by Moodle

https://e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=1579199&cmid=200652 4/4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy