Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<fstream>
- #include<string>
- #include<sstream>
- using namespace std;
- template <typename T>
- int partition(vector<T>& V, int low, int high){
- T pivot = V[floor((high + low)/2)];
- int i = low;
- int j = high;
- while (true) {
- while (V[i] < pivot) {
- i++;
- }
- while (V[j] > pivot) {
- j--;
- }
- if (i >= j) return j;
- swap(V[i++], V[j--]);
- }
- }
- template <typename T>
- void quicksort(vector<T> &V, int low, int high){
- if (low < high)
- {
- T pivot = partition(V, low, high);
- quicksort(V, low, pivot);
- quicksort(V, pivot+1, high);
- }
- return;
- }
- bool isNumber(const string& str) {
- return str.find_first_not_of("-.,0123456789") == string::npos;
- }
- template <typename T>
- string parsStream(vector<T>& V, ifstream& input ) {
- string delimiter = " ";
- string token;
- T buf;
- while (getline(input, token, ' ')) {
- if (isNumber(token))
- {
- stringstream string_s(token);
- string_s >> buf;
- V.push_back(buf);
- }
- else return token;
- }
- return "";
- }
- template <typename T>
- void printSequence(const vector<T>& V) {
- for (int i = 0; i < V.size(); i++) {
- cout << V[i] << " ";
- }
- cout << endl;
- }
- int main() {
- ifstream FILE;
- //vector<int> V = { 12,3,4,5,6,78,0 };
- vector<int> V;
- string buf;
- FILE.open("input.txt");
- buf = parsStream(V, FILE);
- FILE.close();
- if (V.size() == 0) {
- cout << "File contains no elements" << endl;
- return -1;
- }
- if (buf != "") {
- cout << "File contains characters other than numbers: " << buf << endl;
- return -1;
- }
- cout << "Unsorted sequence from file" << endl;
- printSequence(V);
- quicksort(V, 0, V.size()-1);
- cout << "Sorted sequence from file" << endl;
- printSequence(V);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement