lab05.cpp
Problem Statement
lab05.cpp
Write a program in C++ to implement Quick Sort using class, constructor, member variables, member functions, and destructor.
Source Code
cpp
#include<iostream>
using namespace std;
template <class T>
class QuickSort {
private:
T *arr;
int n;
void swapElements(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int partition(int low, int high) {
T pivot = arr[high];
int i = low - 1;
for(int j = low; j < high; j++) {
if(arr[j] <= pivot) {
i++;
swapElements(arr[i], arr[j]);
}
}
swapElements(arr[i + 1], arr[high]);
return i + 1;
}
void sortHelper(int low, int high) {
if(low < high) {
int pi = partition(low, high);
sortHelper(low, pi - 1);
sortHelper(pi + 1, high);
}
}
public:
QuickSort(int size) {
n = size;
arr = new T[n];
cout << "Array created successfully with capacity: " << n << "\n";
}
void inputElements() {
cout << "Enter " << n << " elements: ";
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
}
void sort() {
sortHelper(0, n - 1);
cout << "Array sorted successfully.";
}
void display() {
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
~QuickSort() {
delete[] arr;
cout << "\nMemory cleared.";
}
};
int main() {
int size;
cout << "Enter the size: ";
cin >> size;
QuickSort<int> obj(size);
obj.inputElements();
cout << "\nOriginal array: ";
obj.display();
cout << "\n";
obj.sort();
cout << "\nSorted array: ";
obj.display();
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87