Skip to content

Instantly share code, notes, and snippets.

@Poplava
Created May 6, 2015 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Poplava/9cbe50372077fd6dc709 to your computer and use it in GitHub Desktop.
Save Poplava/9cbe50372077fd6dc709 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class Sort {
private:
bool isSorted = false;
char* sortedBy;
int* data;
int n, c = 0, m = 0;
void quickRecursive(int l, int r);
public:
Sort();
Sort(Sort &s);
~Sort();
void printData();
void printStats();
void sortInstertion();
void sortBubble();
void sortSelection();
void sortShaker();
void sortQuick();
int getN() {
return n;
}
int getI(int i) {
return data[i];
}
int getC() {
return c;
}
int getM() {
return m;
}
};
Sort::Sort() {
cout << "Enter n (size of the array): ";
cin >> n;
data = new int[n];
for (int i = 0; i < n; i++) {
cout << "Enter data[" << i << "]: ";
cin >> data[i];
}
}
Sort::Sort(Sort &s) {
n = s.getN();
data = new int[n];
for (int i = 0; i < n; i++) {
data[i] = s.getI(i);
}
}
Sort::~Sort() {
delete data;
}
void Sort::printData() {
cout << "Array[" << n << "] : [";
for (int i = 0; i < n; i++) {
cout << data[i];
if (i != n - 1) {
cout << ", ";
}
}
cout << "];";
if (isSorted) {
cout << " is sorted";
}
cout << endl;
}
void Sort::printStats() {
cout << sortedBy << "\t" << "m: " << m << "\t" << "c: " << c << endl;
}
void Sort::sortInstertion() {
int i, j, x;
isSorted = true;
sortedBy = "Instertion";
for (i = 1; i < n; i++) {
x = data[i], j = i;
while (j > 0 && x < data[j - 1]) {
data[j] = data[j - 1];
j--;
c++;
m++;
}
c++;
data[j] = x;
}
}
void Sort::sortBubble() {
int i, j, x;
isSorted = true;
sortedBy = "Bubble\t";
for (i = 1; i < n; i++) {
for (j = n - 1; j >= i; j--) {
if (data[j - 1] > data[j]) {
x = data[j - 1];
data[j - 1] = data[j];
data[j] = x;
m++;
}
c++;
}
}
}
void Sort::sortSelection() {
int i, j, k, x;
isSorted = true;
sortedBy = "Selection";
for (i = 0; i < n; i++) {
x = data[i], k = i;
for (j = i + 1; j < n; j++) {
if (data[j] < x) {
k = j;
x = data[j];
}
c++;
}
data[k] = data[i];
data[i] = x;
m++;
}
}
void Sort::sortShaker() {
int i, j, x, l, r;
isSorted = true;
sortedBy = "Shaker\t";
i = 0;
l = 1;
r = n - 1;
do {
for (j = r; j >= l; j--) {
if (data[j - 1] > data[j]) {
x = data[j - 1];
data[j - 1] = data[j];
data[j] = x;
i = j;
m++;
}
c++;
}
l = i + 1;
for (j = l; j <= r; j++) {
if (data[j - 1] > data[j]) {
x = data[j - 1];
data[j - 1] = data[j];
data[j] = x;
i = j;
m++;
}
c++;
}
r = i - 1;
} while (l <= r);
}
void Sort::sortQuick() {
isSorted = true;
sortedBy = "Quick\t";
quickRecursive(0, n - 1);
}
void Sort::quickRecursive(int l, int r) {
int i, j, x, w;
i = l;
j = r;
x = data[(l + r) / 2];
do {
while (data[i] < x) {
i++;
c++;
}
c++;
while (data[j] > x) {
j--;
c++;
}
c++;
if (i <= j) {
w = data[i];
data[i] = data[j];
data[j] = w;
i++;
j--;
m++;
}
} while (i <= j);
if (l < j) {
quickRecursive(l, j);
}
if (i < r) {
quickRecursive(i, r);
}
}
int main() {
Sort s1;
Sort s2(s1);
Sort s3(s1);
Sort s4(s1);
Sort s5(s1);
cout << endl << "Before sort: " << endl;
cout << "s1: ";
s1.printData();
cout << "s2: ";
s2.printData();
cout << "s3: ";
s3.printData();
cout << "s4: ";
s4.printData();
cout << "s5: ";
s5.printData();
cout << endl << "After sort: " << endl;
s1.sortInstertion();
cout << "s1: ";
s1.printData();
s2.sortBubble();
cout << "s2: ";
s2.printData();
s3.sortSelection();
cout << "s3: ";
s3.printData();
s4.sortShaker();
cout << "s4: ";
s4.printData();
s5.sortQuick();
cout << "s5: ";
s5.printData();
cout << endl << "Sort stats: " << endl;
s1.printStats();
s2.printStats();
s3.printStats();
s4.printStats();
s5.printStats();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment