oaktree

Quick Sort - C++

Mar 19th, 2016
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void quicksort(vector<char>& vec, const int pivot_point) {
  10.     if (vec.size() <= 1) return;
  11.  
  12.     char pivot_char = vec[pivot_point];
  13.  
  14.     vector<char> left, right;
  15.  
  16.     for (int i = 0, n = vec.size(); i < n; i++) {
  17.         if (i == pivot_point) continue;
  18.  
  19.         if (vec[i] < pivot_char) {
  20.             left.push_back(vec[i]);
  21.         } else {
  22.             right.push_back(vec[i]);
  23.         }
  24.     }
  25.  
  26.     if (!left.empty()) quicksort(left, rand() % left.size());
  27.     if (!right.empty()) quicksort(right, rand() % right.size());
  28.  
  29.     vec = left;
  30.     vec.push_back(pivot_char);
  31.     vec.insert(vec.end(), right.begin(), right.end());
  32. }
  33.  
  34.  
  35. int main() {
  36.  
  37.     cout << "give me a string" << endl;
  38.     string s; getline(cin, s);
  39.  
  40.     vector<char> vec(s.begin(), s.end());
  41.  
  42.     if (!vec.empty()) quicksort(vec, rand() % vec.size());
  43.  
  44.     string str(vec.begin(), vec.end());
  45.  
  46.     cout << str << endl;
  47.     return 0;
  48. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×