Function - Sort

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

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).

#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() {
next = 0;
}
Node(T data) {
this->data = data;
this->next = nullptr;
}
};

void bubbleSort();
};
----------
template <class T>
void SLinkedList<T>::bubbleSort()
{
for (int i = this->count-1; i > 0; i--){
for (int j = 0; j <= i; j++){
Node* node = this->head;
for (int l = 0; l < j-1; l++) node = node->next;
if (node->data > node->next->data) {
T temp = node->data;
node->data = node->next->data;
node->next->data = temp;
}
}
this->printList();
}
}
==========
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;
}
----------
template <class T>
void Sorting<T>::selectionSort(T *start, T *end)
{
int size = (end - start);
for (int i =0; i<size-1 ; i++) {
// Tìm phần tử min trong dãy unsort
int kmin = i+1;
for (int j = i+1; j<size; j++){
if (*(start+kmin) > *(start+j)) kmin = j;
}
if (*(start+i) > *(start+kmin)){
T temp = *(start+i);
*(start+i) = *(start+kmin);
*(start+kmin) = temp;
}
printArray (start, end);
}
}
==========
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 */
----------
// TODO: Write your code here

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


{
for (int i = segment_idx; i<cur_segment_total ; i++){
int x = *(start + i);
int j = i-segment_idx;
bool cont = true;
while ((j >= 0) && cont)
if (*(start + j) > x){
*(start +j +segment_idx) = *(start + j);
j = j-segment_idx;
}
else cont = false;
*(start +j +segment_idx) = x;
}
}

static void ShellSort(T* start, T* end, int* num_segment_list, int num_phases) {


int size = end-start;
for (int r = num_phases-1; r >= 0; r--){
int k = *(num_segment_list+r);
sortSegment (start, end, k, size);
cout << k << " segments: ";
printArray(start, end);
}
}
==========
mplement static methods Partition and QuickSort 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 T* Partition(T* start, T* end) ;
public:
static void QuickSort(T* start, T* end) ;
};
#endif /* SORTING_H */
----------
static T* Partition(T* start, T* end) {
T pivot = *start;
int i = 0;
int j = end-start+1;
do {
// i qua phải đến khi a[i] >= pivot
do i++; while (*(start+i) < pivot);
// j qua trái đến khi a[j] <= pivot
do j--; while (*(start+j) > pivot);
T temp = *(start+i);
*(start+i) = *(start+j);
*(start+j) = temp;
} while (i<j);
T temp = *(start+i);
*(start+i) = *(start+j);
*(start+j) = temp;
temp = *(start);
*(start) = *(start+j);
*(start+j) = temp;
cout << j << ' ';
return (start+j); // Index của phần tử pivot
}
static void QuickSort(T* start, T* end) {
if (end <= start) return;
T* q = Partition(start, end);
QuickSort(start,q);
QuickSort(q+1,end);
}

You might also like