使用10个随机整数的数组在c ++中进行冒泡排序是行不通的

时间:2017-03-20 04:15:36

标签: c++

Run code here

这是我在c ++中对冒泡排序的代码,由于某些原因,只有泡泡排序最后几个而不是数组中的前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();
}

1 个答案:

答案 0 :(得分:0)

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

您可能希望排序到位而不是我在下面做的事情。如果是这样,您会发现std::swap(v[i],v[j]) from the header <algorithm> helpful.您还希望将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;
}