在 C++ 中使用 10 个随机整数的数组进行气泡排序不起作用

bubble sort in c++ with an array of 10 random ints isnt working

本文关键字:数组 气泡 不起作用 排序 整数 随机 C++      更新时间:2023-10-16

在此处运行代码

这是我在 C++ 中进行气泡排序的代码,由于某种原因,只有 bubble 对数组中的最后几个整数进行排序,而不是对前 5 个整数进行排序。请帮忙

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
/* Tom Dresner
*  3/17/17
*/
int tom[10];
int b;
int x;

void sort()
{
    cout << "n";
    for(int b=0;b<10;b++)
    {
        for(int x=0;x<9;x++)
        {
            if(tom[x]>tom[x + 1])
            {
                int t = tom[x];
                tom[x] = tom[x+1];
                tom[x+1] = t;
            }
        }
        cout<< tom[b] << endl;
    }
}
int main()
{
    cout << "n";
    for(int i=0;i<10;i++)
    {
        tom[i] = rand()%10;
        cout<< tom[i] << endl;
    }
    sort();
}

不是一个完整的答案,因为这看起来像家庭作业,但这里有一个相关问题的解决方案,显示了您可以使用哪种技术。 特别是,一种不使用全局变量生成和返回数据的方法,以及一种打印数据的方法。

您可能希望就地排序,而不是我在下面所做的。 如果是这样,您会发现标题中的std::swap(v[i],v[j]) <algorithm>有帮助的。您还希望将std::vector<int>&引用作为参数并返回相同的引用,就像我对std::ostream& os所做的那样。

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::size_t;
std::vector<int> merge( const std::vector<int>& a, const std::vector<int>& b )
/* Given two sorted vectors, a and b, returns a sorted vector that merges the
 * elements of a and b.  Maybe you can figure out how this would be useful
 * for sorting faster than bubble sort?  Or you can ask your professor.
 */
{
  std::vector<int> output; // Will hold the results;
 /* If you haven't seen iterators yet, they're like indices inside the array.
  * The expression *it++ returns the value at position it, then increments
  * it.
  */
  std::vector<int>::const_iterator it = a.begin();
  std::vector<int>::const_iterator jt = b.begin();
  // Reserve as much memory as we will need, because resizing vectors is slow.
  output.reserve( a.size() + b.size() );
  while ( it < a.end() || jt < b.end() ) {
    if ( it == a.end() ) // We've reached the end of a, but not b.
      while ( jt < b.end() ) // So we can stop checking a.
        output.push_back(*jt++); // Add each element to the end.
    else if ( jt == b.end() ) // We've reached the end of b, but not a.
      while ( it < a.end() )
        output.push_back(*it++);
    else if ( *jt >= *it ) // Always add the lowest remaining element.
      output.push_back(*it++);
    else // What is the only remaining case?
      output.push_back(*jt++);
  } // end while
 /* Do we know for sure that we've added each element from a or b to output
  * exactly once, and in the right order?  We assume a and be were sorted
  * already.  On each invocation of the loops, we copied exactly one element
  * from either a or b to output, and stepped past it.  The element we added
  * was the smallest remaining element of a, unless there were no more in a
  * or the smallest remaining element of b was smaller.  Otherwise, it was
  * the smallest remaining element of b.  We stopped when we ran out of
  * elements in both a and b.
  */
  output.shrink_to_fit(); // Maybe save a few bytes of memory.
  // Check that we have the correct number of elements:
  assert( output.size() == a.size() + b.size() );
  return output;
}
bool is_sorted( const std::vector<int>& v )
// Returns true if and only if v is sorted.
{
  // Check that the elements are sorted.
  for ( size_t i = 1; i < v.size(); ++i )
    if( v[i] < v[i-1] )
      return false;
 /* If we escape the loop, we tested each pair of consecutive elements of v,
  * and none were out of order.
  */
  return true;
}
std::ostream& operator<< ( std::ostream& os, const std::vector<int>& v )
// Boilerplate to serialize and print a vector to a stream.
{
  const size_t size = v.size();
  os << '[';
  if (size > 0)
    os << v[0];
  for ( size_t i = 1; i < size; ++i )
    os << ',' << v[i];
  os << ']';
  return os;
}
int main(void)
{
  // Our sorted input lists:
  const std::vector<int> a = {0, 2, 4, 6, 8};
  const std::vector<int> b = {-3, -2, -1, 0, 1, 2, 3};
  assert(is_sorted(a)); // Input validation.
  assert(is_sorted(b));
  //Our output:
  const std::vector<int> sorted = merge(a, b);
  assert(is_sorted(sorted)); // Output validation.
  cout << sorted << endl;
  return EXIT_SUCCESS;
}