0% found this document useful (0 votes)
129 views10 pages

Sorting (Part 1) - Attempt Review

Uploaded by

Jo Ch
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)
129 views10 pages

Sorting (Part 1) - Attempt Review

Uploaded by

Jo Ch
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/ 10

22:27 22/10/2023 Sorting (Part 1): Attempt review

Đã bắt đầu vào Thứ bảy, 21 Tháng mười 2023, 4:14 PM


lúc
Tình trạng Đã hoàn thành
Hoàn thành vào Chủ nhật, 22 Tháng mười 2023, 10:27 PM
lúc
Thời gian thực 1 ngày 6 giờ
hiện
Điểm 3,00/3,00
Điểm 10,00 của 10,00 (100%)

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 1/10
22:27 22/10/2023 Sorting (Part 1): Attempt review

Câu hỏi 1
Chính xác

Điểm 1,00 của 1,00

Implement method bubbleSort() in class SLinkedList to sort this list in ascending order. After each bubble, we will print out a list to check
(using printList).

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 2/10
22:27 22/10/2023 Sorting (Part 1): Attempt review

#include <iostream>
#include <sstream>
using namespace std;

template <class T>


class SLinkedList {
public:
class Node; // Forward declaration
protected:
Node* head;
Node* tail;
int count;
public:
SLinkedList()
{
this->head = nullptr;
this->tail = nullptr;
this->count = 0;
}
~SLinkedList(){};
void add(T e)
{
Node *pNew = new Node(e);

if (this->count == 0)
{
this->head = this->tail = pNew;
}
else
{
this->tail->next = pNew;
this->tail = pNew;
}

this->count++;
}
int size()
{
return this->count;
}
void printList()
{
stringstream ss;
ss << "[";
Node *ptr = head;
while (ptr != tail)
{
ss << ptr->data << ",";
ptr = ptr->next;
}

if (count > 0)
ss << ptr->data << "]";
else
ss << "]";
cout << ss.str() << endl;
}
public:
class Node {
private:
T data;
Node* next;
friend class SLinkedList<T>;
public:
Node() {

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 3/10
22:27 22/10/2023 Sorting (Part 1): Attempt review
next = 0;
}
Node(T data) {
this->data = data;
this->next = nullptr;
}
};

void bubbleSort();
};

For example:

Test Result

int arr[] = {9, 2, 8, 4, 1}; [2,8,4,1,9]


SLinkedList<int> list; [2,4,1,8,9]
for(int i = 0; i <int(sizeof(arr))/4;i++) [2,1,4,8,9]
list.add(arr[i]); [1,2,4,8,9]
list.bubbleSort();

Answer: (penalty regime: 0 %)

Reset answer

1 template <class T>


2 void SLinkedList<T>::bubbleSort()
3 ▼ {
4
5 if (count <= 1) return;
6 bool swapped;
7 Node* current;
8 Node* lptr = nullptr;
9 int iteration = 1;
10
11 ▼ do {
12 swapped = false;
13 current = head;
14
15 ▼ while (current->next != lptr) {
16 ▼ if (current->data > current->next->data) {
17 T temp = current->data;
18 current->data = current->next->data;
19 current->next->data = temp;
20 swapped = true;
21 }
22 current = current->next;
23 }
24 lptr = current;
25 ▼ if (swapped) {
26
27 printList();
28 iteration++;
29 }
30 } while (swapped);
31
32 }

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 4/10
22:27 22/10/2023 Sorting (Part 1): Attempt review

Test Expected Got

 int arr[] = {9, 2, 8, 4, 1}; [2,8,4,1,9] [2,8,4,1,9] 


SLinkedList<int> list; [2,4,1,8,9] [2,4,1,8,9]
for(int i = 0; i <int(sizeof(arr))/4;i++) [2,1,4,8,9] [2,1,4,8,9]
list.add(arr[i]); [1,2,4,8,9] [1,2,4,8,9]
list.bubbleSort();

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 5/10
22:27 22/10/2023 Sorting (Part 1): Attempt review

Câu hỏi 2
Chính xác

Điểm 1,00 của 1,00

Implement static method selectionSort in class Sorting to sort an array in ascending order. After each selection, we will print out a list to check (using
printArray).

#include <iostream>
using namespace std;

template <class T>


class Sorting
{
public:
/* Function to print an array */
static void printArray(T *start, T *end)
{
int size = end - start;
for (int i = 0; i < size - 1; i++)
cout << start[i] << ", ";
cout << start[size - 1];
cout << endl;
}

static void selectionSort(T *start, T *end);


};

For example:

Test Result

int arr[] = {9, 2, 8, 1, 0, -2}; -2, 2, 8, 1, 0, 9


Sorting<int>::selectionSort(&arr[0], &arr[6]); -2, 0, 8, 1, 2, 9
-2, 0, 1, 8, 2, 9
-2, 0, 1, 2, 8, 9
-2, 0, 1, 2, 8, 9

Answer: (penalty regime: 0 %)

Reset answer

1 template <class T>


2 ▼ void Sorting<T>::selectionSort(T *start, T *end) {
3 int n = end - start;
4 ▼ for (int i = 0; i < n - 1; i++) {
5 int minIndex = i;
6 ▼ for (int j = i + 1; j < n; j++) {
7 ▼ if (start[j] < start[minIndex]) {
8 minIndex = j;
9 }
10 }
11 ▼ if (minIndex != i) {
12 T temp = start[i];
13 start[i] = start[minIndex];
14 start[minIndex] = temp;
15 }
16
17 printArray(start, end);
18 }
19 }
20
21
22
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 6/10
22:27 22/10/2023 Sorting (Part 1): Attempt review
23
24
25
26

Test Expected Got

 int arr[] = {9, 2, 8, 1, 0, -2}; -2, 2, 8, 1, 0, 9 -2, 2, 8, 1, 0, 9 


Sorting<int>::selectionSort(&arr[0], &arr[6]); -2, 0, 8, 1, 2, 9 -2, 0, 8, 1, 2, 9
-2, 0, 1, 8, 2, 9 -2, 0, 1, 8, 2, 9
-2, 0, 1, 2, 8, 9 -2, 0, 1, 2, 8, 9
-2, 0, 1, 2, 8, 9 -2, 0, 1, 2, 8, 9

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 7/10
22:27 22/10/2023 Sorting (Part 1): Attempt review

Câu hỏi 3
Chính xác

Điểm 1,00 của 1,00

Implement static methods sortSegment and ShellSort in class Sorting to sort an array in ascending order.
#ifndef SORTING_H
#define SORTING_H

#include <sstream>
#include <iostream>
#include <type_traits>
using namespace std;

template <class T>


class Sorting {
private:
static void printArray(T* start, T* end)
{
int size = end - start;
for (int i = 0; i < size; i++)
cout << start[i] << " ";
cout << endl;
}

public:
// TODO: Write your code here
static void sortSegment(T* start, T* end, int segment_idx, int cur_segment_total);
static void ShellSort(T* start, T* end, int* num_segment_list, int num_phases);
};

#endif /* SORTING_H */

For example:

Test Result

int num_segment_list[] = {1, 3, 5}; 5 segments: 5 4 3 2 1 10 9 8 7 6


int num_phases = 3; 3 segments: 2 1 3 5 4 7 6 8 10 9
int array[] = { 10, 9, 8 , 7 , 6, 5, 4, 3, 2, 1 }; 1 segments: 1 2 3 4 5 6 7 8 9 10

Sorting<int>::ShellSort(&array[0], &array[10], &num_segment_list[0], num_phases);

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ static void sortSegment(T* start, T* end, int segment_idx, int cur_segment_total


2 // TODO
3 int size = end - start;
4 int a, j;
5 ▼ for (int i = 1; i * cur_segment_total + segment_idx < size; ++i){
6 a = start[i * cur_segment_total + segment_idx];
7 j = i - 1;
8
9 ▼ while (j >= 0 && start[j * cur_segment_total + segment_idx] > a){
10 start[(j + 1) * cur_segment_total + segment_idx] = start[j * cur_seg
11 j = j - 1;
12 }
13
14 start[(j + 1) * cur_segment_total + segment_idx] = a;
15 }
16 }
17
18
i id h ll ( * * d i * li i h
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 8/10
22:27 22/10/2023 Sorting (Part 1): Attempt review
19 ▼ static void ShellSort(T* start, T* end, int* num_segment_list, int num_phases
20 // TODO
21 // Note: You must print out the array after sorting segments to check whethe
22 ▼ for (int i = num_phases - 1; i >= 0; i--){
23 ▼ for (int segment = 0; segment < num_segment_list[i]; ++segment){
24 sortSegment(start, end, segment, num_segment_list[i]);
25 }
26 cout << num_segment_list[i] << " segments: ";
27 printArray(start, end);
28 }
29 }

Test Expected Got

 int num_segment_list[] = {1, 3, 5}; 5 segments: 5 4 3 2 1 10 9 5 segments: 5 4 3 2 1 10 9 


int num_phases = 3; 8 7 6 8 7 6
int array[] = { 10, 9, 8 , 7 , 6, 5, 4, 3, 2, 1 }; 3 segments: 2 1 3 5 4 7 6 8 3 segments: 2 1 3 5 4 7 6 8
10 9 10 9
Sorting<int>::ShellSort(&array[0], &array[10], 1 segments: 1 2 3 4 5 6 7 8 1 segments: 1 2 3 4 5 6 7 8
&num_segment_list[0], num_phases); 9 10 9 10

 int num_segment_list[] = { 1, 2, 6 }; 6 segments: 4 3 2 1 6 5 10 6 segments: 4 3 2 1 6 5 10 


int num_phases = 3; 9 8 7 9 8 7
int array[] = { 10, 9, 8 , 7 , 6, 5, 4, 3, 2, 1 }; 2 segments: 2 1 4 3 6 5 8 7 2 segments: 2 1 4 3 6 5 8 7
10 9 10 9
Sorting<int>::ShellSort(&array[0], &array[10], 1 segments: 1 2 3 4 5 6 7 8 1 segments: 1 2 3 4 5 6 7 8
&num_segment_list[0], num_phases); 9 10 9 10

 int num_segment_list[] = { 1, 2, 5 }; 5 segments: 5 4 3 2 1 10 9 5 segments: 5 4 3 2 1 10 9 


int num_phases = 3; 8 7 6 8 7 6
int array[] = { 10, 9, 8 , 7 , 6, 5, 4, 3, 2, 1 }; 2 segments: 1 2 3 4 5 6 7 8 2 segments: 1 2 3 4 5 6 7 8
9 10 9 10
Sorting<int>::ShellSort(&array[0], &array[10], 1 segments: 1 2 3 4 5 6 7 8 1 segments: 1 2 3 4 5 6 7 8
&num_segment_list[0], num_phases); 9 10 9 10

 int num_segment_list[] = { 1, 2, 3 }; 3 segments: 1 3 2 4 6 5 7 9 3 segments: 1 3 2 4 6 5 7 9 


int num_phases = 3; 8 10 8 10
int array[] = { 10, 9, 8 , 7 , 6, 5, 4, 3, 2, 1 }; 2 segments: 1 3 2 4 6 5 7 9 2 segments: 1 3 2 4 6 5 7 9
8 10 8 10
Sorting<int>::ShellSort(&array[0], &array[10], 1 segments: 1 2 3 4 5 6 7 8 1 segments: 1 2 3 4 5 6 7 8
&num_segment_list[0], num_phases); 9 10 9 10

 int num_segment_list[] = { 1, 5, 8, 10 }; 10 segments: 3 5 4 8 11 14 10 segments: 3 5 4 8 11 14 


int num_phases = 4; 15 13 1 2 9 6 7 10 12 15 13 1 2 9 6 7 10 12
int array[] = { 3, 5, 7, 10 ,12, 14, 15, 13, 1, 2, 9, 6, 8 segments: 1 2 4 6 7 10 12 8 segments: 1 2 4 6 7 10 12
4, 8, 11 }; 13 3 5 9 8 11 14 15 13 3 5 9 8 11 14 15
5 segments: 1 2 4 3 5 9 8 5 segments: 1 2 4 3 5 9 8
Sorting<int>::ShellSort(&array[0], &array[15], 11 6 7 10 12 13 14 15 11 6 7 10 12 13 14 15
&num_segment_list[0], num_phases); 1 segments: 1 2 3 4 5 6 7 8 1 segments: 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 9 10 11 12 13 14 15

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 9/10
22:27 22/10/2023 Sorting (Part 1): Attempt review

BÁCH KHOA E-LEARNING

WEBSITE

HCMUT
MyBK
BKSI

LIÊN HỆ

 268 Lý Thường Kiệt, P.14, Q.10, TP.HCM

 (028) 38 651 670 - (028) 38 647 256 (Ext: 5258, 5234)

[email protected]

Copyright 2007-2022 BKEL - Phát triển dựa trên Moodle

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500885&cmid=188436 10/10

You might also like