title: How to make sorting work with a large vector size?
body: <p>I need to get the running time of several sorts.Here, for example, are 2 of them:</p>
  <pre><code>void bubble_sort() {
                 
          auto start = getCurrentTime();
  
          for (int i = container.size(); i &gt; 0; --i) {
              for (int j = 0; j &lt; i - 1; ++j) {
                  if (container[j] &gt; container[j + 1])
                      std::swap(container[j], container[j + 1]);
              }
          }
  
          auto end = getCurrentTime();
          time = duration_cast&lt;milliseconds&gt;(end - start);
          //auto result = duration_cast&lt;milliseconds&gt;(end - start);
          //std::cout &lt;&lt; result.count();
         
  
          
      }
  
      void cocktail_sort() {
  
          auto starting = getCurrentTime();
  
          bool swapped{ true };
          int start = 0;
          int end = std::size(container) - 1;
          while (swapped) {
              swapped = false;
              for (int i = start; i &lt; end; ++i)
                  if (container[i] &gt; container[i + 1]) {
                      swapped = true;
                      std::swap(container[i], container[i + 1]);
                  }
              --end;
              swapped = false;
              for (int i = end; i &gt; start; --i)
                  if (container[i] &lt; container[i - 1]) {
                      swapped = true;
                      std::swap(container[i], container[i - 1]);
                  }
              ++start;
  
          }
          auto ending = getCurrentTime();
          time = duration_cast&lt;milliseconds&gt;(ending - starting);
      }
  </code></pre>
  <p>Everything works when the array size is a couple of thousand and random values are also from 0 to several thousand.</p>
  <p>But next I need to sort an array with a size of 200 000(buble sort) and random numbers in the same range.And then the program just works for a very long time, I waited a few minutes(I have a good processor and the result should be no more than 30 seconds) and finished, tried with another(cocktail sort) and the same result.I work in VS and I see that as if the processor is not working at full capacity, through the task manager, too, a couple of percent on VS.What can I do to make sorting work normally?</p>
  <p><strong>algClass.h</strong></p>
  <pre><code>#pragma once
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;random&gt;
  #include &lt;algorithm&gt;
  #include &lt;bitset&gt;
  #include &lt;set&gt;
  #include &lt;queue&gt;
  #include &lt;iterator&gt;
  #include &quot;contain.h&quot;
  #include &lt;chrono&gt;
  #include &lt;fstream&gt;
  #include &quot;random_helper.h&quot;
  
  
  
  using std::chrono::high_resolution_clock;
  using std::chrono::duration_cast;
  using std::chrono::duration;
  using std::chrono::milliseconds;
  using std::chrono::nanoseconds;
  
  
  class allSorting {
  
  private:    
      std::size_t size;
      std::vector&lt;int&gt; container;
      std::chrono::milliseconds time;
  
      
  
  
  
      /*
      template&lt; typename T &gt;
      void print_container(T&amp;&amp; container) {
          for (auto&amp;&amp; curr : container) {
              std::cout &lt;&lt; curr &lt;&lt; &quot; &quot;;
          }
          //std::copy(std::begin(container), std::end(container), std::ostream_iterator&lt;typename T::value_type&gt;(std::cout, &quot; &quot;));
      }
      */
      template&lt;typename Iter &gt;
      void Q_sort_help(Iter left, Iter right) {
          if (right - left &lt;= 1) return;
          auto separator = *(left + (right - left) / 2);
          auto t_left = left, t_right = right - 1;
          while (t_left &lt; t_right) {
              while (*t_left &lt; separator) ++t_left;
              while (*t_right &gt; separator) --t_right;
              if (t_left &lt;= t_right) {
                  std::swap(*t_left, *t_right);
                  ++t_left;
                  --t_right;
              }
              if (left &lt; t_right) Q_sort_help(left, t_right + 1);
              if (t_left &lt; right) Q_sort_help(t_left, right);
          }
      }
  
  
      
  
  
  public:
  
  
      allSorting(std::size_t new_size) {
          size = new_size;
          container.resize(size);
      }
  
      allSorting() {
          size = 0;
          container.resize(size);
      }
  
      void fill_random_number(int left_b = 0, int right_b = 100) {
          fill_container(container, left_b, right_b);
      }
      
  
      void resize(int newSize) {
          container.resize(newSize);
      
      }
  
      int sizeOfVector() {
          return container.size();
      }
  
      void addValue(int value) {
          container.push_back(value);
      }
  
  
      void bubble_sort() {
                 
          auto start = getCurrentTime();
  
          for (int i = container.size(); i &gt; 0; --i) {
              for (int j = 0; j &lt; i - 1; ++j) {
                  if (container[j] &gt; container[j + 1])
                      std::swap(container[j], container[j + 1]);
              }
          }
  
          auto end = getCurrentTime();
          time = duration_cast&lt;milliseconds&gt;(end - start);
          //auto result = duration_cast&lt;milliseconds&gt;(end - start);
          //std::cout &lt;&lt; result.count();
         
  
          
      }
  
      void cocktail_sort() {
  
          auto starting = getCurrentTime();
  
          bool swapped{ true };
          int start = 0;
          int end = std::size(container) - 1;
          while (swapped) {
              swapped = false;
              for (int i = start; i &lt; end; ++i)
                  if (container[i] &gt; container[i + 1]) {
                      swapped = true;
                      std::swap(container[i], container[i + 1]);
                  }
              --end;
              swapped = false;
              for (int i = end; i &gt; start; --i)
                  if (container[i] &lt; container[i - 1]) {
                      swapped = true;
                      std::swap(container[i], container[i - 1]);
                  }
              ++start;
  
          }
          auto ending = getCurrentTime();
          time = duration_cast&lt;milliseconds&gt;(ending - starting);
      }
  
      void Q_sort() {
          auto starting = getCurrentTime();
  
          Q_sort_help(container.begin(), container.end());
  
          auto ending = getCurrentTime();
          time = duration_cast&lt;milliseconds&gt;(ending - starting);
      }
  
      void Counting_sort() {
          auto starting = getCurrentTime();
  
          auto max = *std::max_element(std::begin(container), std::end(container));
          auto min = *std::min_element(std::begin(container), std::end(container));
          auto new_size{ max - min + 1 };
          std::vector&lt;int&gt; t_v(new_size);
          for (auto&amp; curr : container)
              ++t_v[curr - min];
          auto iter = std::begin(container);
          for (std::size_t i = 0; i &lt; new_size; ++i) {
              std::size_t shift = t_v[i];
              if (shift != 0) {
                  std::fill_n(iter, shift, i + min);
                  std::advance(iter, shift);
              }
          }
  
          auto ending = getCurrentTime();
          time = duration_cast&lt;milliseconds&gt;(ending - starting);
      }
  
  
      std::chrono::high_resolution_clock::time_point getCurrentTime() {
          using std::chrono::high_resolution_clock;
          using std::chrono::duration_cast;
          using std::chrono::duration;
          using std::chrono::milliseconds;
          return high_resolution_clock::now();
  
      }
  
      long long getresultTime(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end) {
          using std::chrono::high_resolution_clock;
          using std::chrono::duration_cast;
          using std::chrono::duration;
          using std::chrono::milliseconds;
  
          auto result = duration_cast&lt;milliseconds&gt;(end - start);
  
              //return duration_cast&lt;milliseconds&gt;(end - start).count();
          return 0;
      }
  
      
      
  
  
      
  
      void print_container() {
          for (auto&amp; curr : container) {
              std::cout &lt;&lt; curr &lt;&lt; &quot; &quot;;
          }
          //std::copy(std::begin(container), std::end(container), std::ostream_iterator&lt;typename T::value_type&gt;(std::cout, &quot; &quot;));
      }
  
     
      
      long long getLastSortingTime() {
          return time.count();
      }
  
  
      void printTime() {
          std::cout &lt;&lt; &quot;The time: &quot; &lt;&lt; time.count() &lt;&lt; &quot; ms\n&quot;;
      }
  
  
      void writeTofile() {
          std::ofstream file(&quot;result.txt&quot;) ;
          
          for (const auto&amp; e : container) {
              file &lt;&lt; e &lt;&lt; &quot;\n&quot;;
          }
      }
  
  };
  
  </code></pre>
  <p><strong>Main</strong></p>
  <pre><code>
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;random&gt;
  #include &lt;algorithm&gt;
  #include &lt;bitset&gt;
  #include &lt;set&gt;
  #include &lt;queue&gt;
  #include &lt;iterator&gt;
  #include &quot;contain.h&quot;
  
  
  #include &quot;algClass.h&quot;
  
  
  
  
  
  int main()
  {
  
      
      int menu{ -1 };
      allSorting testSorting = allSorting();
  
      constexpr std::string_view str_menu[]{
          
  
  
      };
  
      auto print_menu = [&amp;]() constexpr {
          for (auto&amp;&amp; element : str_menu) {
              std::cout &lt;&lt; element &lt;&lt; std::endl;
  
          }
      };
  
      auto get_file_path = []() {
          std::string file_path;
          std::cout &lt;&lt; &quot;file name: &quot;;
          std::cin &gt;&gt; file_path;
          return file_path;
      };
  
      auto enter_numb = [&amp;](auto number) {
          std::cout &lt;&lt; &quot;input number:&quot;;
          std::cin &gt;&gt; number;
          if (std::cin.fail()) {
              std::cin.clear();
              std::cin.ignore((std::numeric_limits&lt;std::streamsize&gt;::max)(), '\n');
              throw std::exception(&quot;error &quot;);
          }
          else {
              return number;
          }
      };
  
  
  
  
      print_menu();
  
      while (menu) {
          std::cout &lt;&lt; &quot;choose state: &quot;; std::cin &gt;&gt; menu;
          switch (menu)
          {
  
          case 1:
          {
  
              int size{};
              size = enter_numb(size);
              testSorting.resize(size);
          }
  
          break;
          case 2:
          {
              int rightBorder{}, leftBorder{};
              std::cout &lt;&lt; &quot;input left and right border&quot; &lt;&lt; std::endl;
              std::cin &gt;&gt; leftBorder &gt;&gt; rightBorder;
              testSorting.fill_random_number(leftBorder, rightBorder);
          }
          break;
          case 3:
          {
  
              testSorting.print_container();
              std::cout &lt;&lt; std::endl;
          }
          break;
          case 4:
          {
              std::cout &lt;&lt; &quot;input sieze: &quot;;
              int size{  };
              std::cin &gt;&gt; size;
              std::cout &lt;&lt; &quot;input sizeа: &quot;;
              std::cout &lt;&lt; std::endl;
              for (size_t i = 0; i &lt; size; i++)
              {
                  int number{  };
                  std::cin &gt;&gt; number;
                  testSorting.addValue(number);
              }
  
          }
          break;
          case 5:
          {
              testSorting.bubble_sort();
  
          }
          break;
          case 6:
          {
              testSorting.cocktail_sort();
  
          }
          break;
  
          case 7:
          {
              testSorting.Q_sort();
  
          }
          break;
  
          case 8:
          {
              testSorting.Counting_sort();
  
          }
          break;
  
          case 9:
          {
             auto time = testSorting.getLastSortingTime();
             std::cout &lt;&lt; time &lt;&lt; std::endl;
  
          }
          break;
  
          case 10:
          {
              testSorting.writeTofile();
  
          }
          break;
  
          case 0:
              break;
  
          default:
              std::cout &lt;&lt; &quot;error&quot; &lt;&lt; std::endl;
          }
  
      }
  
  
  }
  
  
  </code></pre>
  <p><strong>random_helper.h</strong></p>
  <pre><code>
  #include &lt;iostream&gt;
  #include &lt;random&gt;
  #include &lt;algorithm&gt;
  
  namespace detail {
  
      template&lt;typename T, typename Always = void&gt;
      struct uniform_distribution_impl {
          static_assert(sizeof(T) == 0, &quot;T must be integral of floating point&quot;);
      };
      template &lt;typename T&gt;
      struct uniform_distribution_impl&lt;
          T, std::enable_if_t&lt;std::is_integral_v&lt;T&gt;&gt;&gt;
      {
          using type = std::uniform_int_distribution&lt;T&gt;;
      };
  
      template &lt;typename T&gt;
      struct uniform_distribution_impl&lt;
          T, std::enable_if_t&lt;std::is_floating_point_v&lt;T&gt;&gt;&gt;
      {
          using type = std::uniform_real_distribution&lt;T&gt;;
      };
  
  }
  template &lt;typename T&gt;
  using uniform_distribution = typename detail::uniform_distribution_impl&lt;T&gt;::type;
  
  template&lt;typename t, typename cont_type = typename t::value_type&gt;
  void fill_container(t&amp; container, int left_b = 0, int right_b = 100) {
  
      auto randomnumberbetween = [](int low, int high)
      {
          auto randomfunc = [distribution_ = uniform_distribution&lt;cont_type&gt;(low, high),
              random_engine_ = std::mt19937{ std::random_device{}() }]() mutable
          {
              return distribution_(random_engine_);
          };
          return randomfunc;
      };
  
      std::generate(std::begin(container), std::end(container), randomnumberbetween(left_b, right_b));
  
  
  
  }
  
  </code></pre>