Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include "sorterhelper.h"
- using namespace std;
- //prints content of vector
- void printMyVector(vector<int>myVector)
- {
- for(int i = 0; i < myVector.size(); i++)
- {
- cout << "index " << i << ": " << myVector[i] << endl;
- }
- }
- int main()
- {
- cout << endl;
- //seeds random numbers
- srand(time(NULL));
- //my testing data- size from 1-20
- vector<int>myVector;
- int a = (rand() % 10) + 1; //1 to 20
- cout << "UNSORTED DATA: " << endl;
- //pushes back a ran num from 1-20
- for (int i =0; i < a; i++)
- {
- int b = (rand() % 1000) + 1; //1 to 20
- myVector.push_back(b);
- cout << "index " << i << ": " << myVector[i] << endl;
- }
- cout << endl;
- //calling bubbleSort
- cout << "bubble sort: " << endl;
- //bubbleSort(myVector);
- //printMyVector(myVector);
- cout << endl;
- //calling insertionSort
- cout << "insertion sort: " << endl;
- //insertionSort(myVector);
- //printMyVector(myVector);
- cout << endl;
- //calling mergeSort
- cout << "merge sort:" << endl;
- //mergeSort(myVector);
- //printMyVector( myVector);
- cout << endl;
- //calling quickSort
- cout << "quick sort:" << endl;
- //quickSort(myVector);
- //printMyVector(myVector);
- cout << endl;
- //calling selectionSort
- cout << "selection sort:" << endl;
- //selectionSort(myVector);
- //printMyVector(myVector);
- cout << endl;
- return 0;
- }
- ///////////////////////
- #include <iostream>
- #include <vector>
- #ifndef SORTINGFUN_SORTER_H
- #define SORTINGFUN_SORTER_H
- template <typename T>
- class Sorter {
- protected:
- std::vector<T> data;
- public:
- Sorter() {}
- Sorter(T& x):data(x) {}
- void setData(const std::vector<T>& x){
- data = x;
- }
- virtual void sort() = 0;
- };
- //bubble sort stackoverflow
- template <typename T>
- class MysterySorterA : public Sorter<T> {
- public:
- virtual void sort()
- {/*
- //vector < int > myV;
- //myV = this - > data;
- bool swapp = true;
- while(swapp)
- {
- swapp = false;
- for (int i = 0; i < myVector.size()-1; i++)
- {
- if (myVector[i]>myVector[i+1] )
- {
- myVector[i] += myVector[i+1];
- myVector[i+1] = myVector[i] - myVector[i+1];
- myVector[i] -=myVector[i+1];
- swapp = true;
- }
- }
- }*/
- }
- };
- //insertion from stanford
- template <typename T>
- class MysterySorterB : public Sorter<T> {
- public:
- virtual void sort()
- {/*
- std::cout << "Mystery Sorter B" << std::endl;
- for (int i = 1; i < myVector.size(); i++)
- {
- int temp = myVector[i];
- int j = i;
- while (j >= 1 && myVector[j - 1] > temp)
- {
- myVector[j] = myVector[j - 1];
- j--;
- }
- myVector[j] = temp;
- }*/
- }
- };
- //merge & mergeSort from stanford
- template <typename T>
- class MysterySorterC : public Sorter<T> {
- public:
- virtual void sort()
- {/*
- std::cout << "Mystery Sorter C" << std::endl;
- void merge(vector<int> &myVector, vector<int> &v1, vector<int> &v2)
- {
- int n1 = v1.size();
- int n2 = v2.size();
- int p1 = 0;
- int p2 = 0;
- while (p1 < n1 && p2 < n2)
- {
- if (v1[p1] < v2[p2])
- {
- myVector.push_back(v1[p1++]);
- }
- else
- {
- myVector.push_back(v2[p2++]);
- }
- }
- while (p1 < n1)
- {
- myVector.push_back(v1[p1++]);
- }
- while (p2 < n2)
- {
- myVector.push_back(v2[p2++]);
- }
- }
- void mergeSort(vector<int> &myVector)
- {
- int n = myVector.size();
- if (n <= 1) return;
- vector<int> v1;
- vector<int> v2;
- for (int i=0; i < n; i++)
- {
- if (i < n / 2)
- {
- v1.push_back(myVector[i]);
- }
- else
- {
- v2.push_back(myVector[i]);
- }
- }
- mergeSort(v1);
- mergeSort(v2);
- myVector.clear();
- merge(myVector, v1, v2);
- }*/
- }
- };
- //quickSort from stanford
- template <typename T>
- class MysterySorterD : public Sorter<T> {
- public:
- virtual void sort()
- {/*
- std::cout << "Mystery Sorter D" << std::endl;
- //quickSort from stanford.edu
- int partition(vector<int> &myVector, int start, int finish)
- {
- int pivot = myVector[start];
- int lh = start + 1;
- int rh = finish;
- while (true)
- {
- while (lh < rh && myVector[rh] >= pivot) rh--;
- while (lh < rh && myVector[lh] < pivot) lh++;
- if (lh == rh) break;
- // swap
- int tmp = myVector[lh];
- myVector[lh] = myVector[rh];
- myVector[rh] = tmp;
- }
- if (myVector[lh] >= pivot) return start;
- myVector[start] = myVector[lh];
- myVector[lh] = pivot;
- return lh;
- }
- void quickSort(vector<int> &myVector, int start, int finish)
- {
- if (start >= finish) return;
- int boundary = partition(myVector, start, finish);
- quickSort(myVector, start, boundary - 1);
- quickSort(myVector, boundary + 1, finish);
- }
- void quickSort(vector<int> &myVector)
- {
- quickSort(myVector, 0, myVector.size() - 1);
- }*/
- }
- };
- //selectionSort from stanford
- template <typename T>
- class MysterySorterE : public Sorter<T> {
- public:
- virtual void sort()
- {/*
- std::cout << "Mystery Sorter E" << std::endl;
- for (int i = 0; i < v.size(); i++)
- {
- int smallestIndex = i;
- for (int j = i+1; j < v.size(); j++)
- {
- if (v[j] < v[smallestIndex])
- {
- smallestIndex = j;
- }
- }
- swap(v[i],v[smallestIndex]);
- }*/
- }
- };
- #endif //SORTINGFUN_SORTER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement