Professional Documents
Culture Documents
Mục lục
1 Lab 3 - Sort 3
1.1 Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 Câu 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2 Câu 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.3 Câu 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.4 Câu 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1.5 Câu 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.1.6 Câu 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1 Lab 3 - Sort
1.1 Sort
1.1.1 Câu 1
2 #include <iostream>
3 #include <sstream>
4 using namespace std;
5
19 this->count = 0;
20 }
21 ~SLinkedList(){};
22 void add(T e)
23 {
24 Node *pNew = new Node(e);
25
26 if (this->count == 0)
27 {
28 this->head = this->tail = pNew;
29 }
30 else
31 {
32 this->tail->next = pNew;
33 this->tail = pNew;
34 }
35
36 this->count++;
37 }
38 int size()
39 {
40 return this->count;
41 }
42 void printList()
43 {
44 stringstream ss;
45 ss << "[";
46 Node *ptr = head;
47 while (ptr != tail)
48 {
49 ss << ptr->data << ",";
50 ptr = ptr->next;
51 }
52
53 if (count > 0)
54 ss << ptr->data << "]";
55 else
56 ss << "]";
57 cout << ss.str() << endl;
58 }
59 public:
60 class Node {
61 private:
62 T data;
63 Node* next;
64 friend class SLinkedList<T>;
65 public:
66 Node() {
67 next = 0;
68 }
69 Node(T data) {
70 this->data = data;
71 this->next = nullptr;
72 }
73 };
74
75 void bubbleSort();
76 };
77
97 this->printList();
98 i = i->next;
99 }
100 }
101
1.1.2 Câu 2
2 #include <iostream>
3 using namespace std;
4
28 T* min = start;
29 //! Con trỏ tmp dùng để duyệt qua các phần tử còn lại
30 T* tmp = start + 1;
31 while(tmp != end){
32 //! Nếu phần tử tại tmp nhỏ hơn phần tử nhỏ nhất hiện tại, cập nhật min
33 if(*tmp < * min){
34 min = tmp;
35 }
36 tmp++;
37 }
38 //! Hoán đổi phần tử nhỏ nhất tìm được với phần tử đầu tiên trong phạm vi chưa sắp
↪ xếp
39 swap(*start, *min);
40 //! in ra phần tử
41 Sorting<T>::printArray(result,end);
42 start++;
43
44 }
45 }
46
47 int main(){
48 int arr[] = {9, 2, 8, 1, 0, -2};
49 Sorting<int>::selectionSort(&arr[0], &arr[6]);
50 /*
51 *-2, 0, 8, 1, 2, 9
52 *-2, 0, 1, 8, 2, 9
53 *-2, 0, 1, 2, 8, 9
54 *-2, 0, 1, 2, 8, 9
55
56 */
57 }
1.1.3 Câu 3
1 #include <sstream>
2 #include <iostream>
3 #include <type_traits>
4 using namespace std;
5
18 public:
19 // TODO: Write your code here
20 static void sortSegment(T* start, T* end, int segment_idx, int
↪ cur_segment_total);
21 static void ShellSort(T* start, T* end, int* num_segment_list, int num_phases);
22 };
23
28 //! Con trỏ left trỏ vào phần tử bắt đầu của segment hiện tại
29 T* left = start + segment_idx;
30
41 }
42 //! Di chuyển left đến phần tử bắt đầu của segment tiếp theo
43 left += cur_segment_total;
44
45 }
46
47 }
48
53 //! Duyệt qua các pha sắp xếp theo danh sách độ dài segment đã cho
54 for(int i = num_phases-1; i >= 0; i--){
55 //! Duyệt qua các segment trong pha hiện tại
56 for(int j = 0; j < num_segment_list[i];j ++ ){
57
62 }
63 cout << num_segment_list[i] << " segments: ";
64 T* tmp = start;
65 Sorting<T>::printArray(start,end);
66 }
67
68 }
69
70 int main(){
71
79 //!1 segments: 1 2 3 4 5 6 7 8 9 10
80 }
1.1.4 Câu 4
1 #include <sstream>
2 #include <iostream>
3 #include <type_traits>
4
22 do {
23 // Find leftmost element greater than
24 // or equal to pivot
25 //! Tìm phần tử bên trái đầu tiên lớn hơn hoặc bằng pivot
26 do {
27 i++;
28 } while (arr[i] < pivot);
29
59 int main(){
60 int array[] = { 3, 5, 7, 10 ,12, 14, 15, 13, 1, 2, 9, 6, 4, 8, 11, 16, 17, 18,
↪ 20, 19 };
61 cout << "Index of pivots: ";
62 Sorting<int>::QuickSort(&array[0], &array[20]);
63 cout << "\n";
64 cout << "Array after sorting: ";
1.1.5 Câu 5
2 #include <iostream>
3 using namespace std;
4 template <class T>
5 class Sorting {
6 public:
7 /* Function to print an array */
8 static void printArray(T *start, T *end)
9 {
10 long size = end - start + 1;
11 for (int i = 0; i < size - 1; i++)
12 cout << start[i] << ", ";
13 cout << start[size - 1];
14 cout << endl;
15 }
16
46 int j = 0;
47
95 int main(){
96
100 /*
101 *0, 2
102 *0, 2, 4
103 *1, 3
104 *1, 3, 4
105 *0, 1, 2, 3, 4, 4
106 */
107 }
1.1.6 Câu 6
1 #include <iostream>
2 using namespace std;
3 template <class T>
4 class Sorting {
5 public:
6 /* Function to print an array */
7 static void printArray(T *start, T *end)
8 {
9 long size = end - start + 1;
10 for (int i = 0; i < size - 1; i++)
11 cout << start[i] << ", ";
12 cout << start[size - 1];
13 cout << endl;
14 }
15
20
21 // You must use the nodes in the original list and must not modify ListNode's val
↪ attribute.
22 // Hint: You should complete the function mergeLists first and validate it using our
↪ first testcase example
23
74 }
75
76 int main(){
77 int arr1[] = {1, 3, 5, 7, 9};
78 int arr2[] = {2, 4, 6, 8};
79 unordered_map<ListNode*, int> nodeAddr;
80 ListNode* a = init(arr1, sizeof(arr1) / 4, nodeAddr);
81 ListNode* b = init(arr2, sizeof(arr2) / 4, nodeAddr);
82 ListNode* merged = mergeLists(a, b);
83 try {
84 printList(merged, nodeAddr);
85 }
86 catch(char const* err) {
87 cout << err << '\n';
88 }
89 freeMem(merged);
90 }