【问题标题】:Adding counter to bubblesort function将计数器添加到冒泡排序功能
【发布时间】:2013-11-06 10:30:51
【问题描述】:

我需要计算冒泡排序函数的交换过程总数和排序运行时间。对于运行时间,我是成功的。但是对于交换进程的总数,我真的不明白该怎么做。我想到了初始化“count”,然后尝试将其调用到主函数中。那是失败的。

如果我的冒泡排序函数:

void bubbleSort(T patient[], int size)
{
  bool noChange = true; // stop when a pass causes no change
    for(int i = size; i > 0; i--)
  {
    noChange = true;
    for(int j = 1; j < i; j++)
    {
      if(patient[j] < patient[j - 1])
      {
        swap(patient[j], patient[j-1]);
        count = count + 1;
        noChange = false;
      } // end if
    } // end for(j)
    if (noChange)
      return; // sorted--no need to continue
  } // end for(i)
}

"count" 在主函数中调用时似乎没有显示任何值。关于我应该尝试什么以便我可以获得交换过程的总数的任何提示?

编辑 3:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>
#include <string>
#include <ctime>

using namespace std;

const int SIZE = 5;
template <class T>
void printArray(T ar[], int sz);
template <class T>
int bubbleSort(T ar[], int sz);

//////////////////////////////////////////////////////////////////////////
// Main Function Implementation
//////////////////////////////////////////////////////////////////////////

int main() {
    int numOfData = 50000;
    string line, temp;
    ofstream resultFile;
    string patient[numOfData];
    ifstream dataFile("shufflePatient.txt");
    int times,i,count;

    cout << "Program to shuffle data" << endl << endl;
    cout << "This program will calculate swapping processes and running time.";


    /*Storing data*/
    cout << "Reading data in process.." << endl;
    if (dataFile.is_open()) {
        i=-1;
        while (dataFile.good()) {
            getline (dataFile, line);
            if (i>=0) patient[i] = line;
            i++;
        }
        dataFile.close();
    }



    double start_s=clock();
    bubbleSort(patient,SIZE);
    double stop_s=clock();


    cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC) << endl;
    count = bubbleSort(patient,SIZE) ;
    cout << "swapping process : " << count ;

    cin.get(); // hold window open

    /*Writing to file*/
    cout << "Writing to file.." << endl;
    resultFile.open ("test.txt");
    for (int i=0 ; i<numOfData ; i++) {
        resultFile << patient[i] << "\n";
    }
    resultFile.close();
    system("pause");
    return 0;
}





//----------------------------------------------------------------------------
// prints array of size size
//----------------------------------------------------------------------------
template <class T>
void printArray(T patient[], int size)
{
  for(int i = 0; i < size; i++)
    cout << patient[i] << " ";
  cout << endl;
}






//----------------------------------------------------------------------------
// sorts array of size size by Bubble Sort method
//----------------------------------------------------------------------------
template <class T>
int bubbleSort(T patient[], int size) //returning an int
{
   int count = 0; //initializing count
   bool noChange = true;
    for(int i = size; i > 0; i--)
  {
    noChange = true;
    for(int j = 1; j < i; j++)
    {
      if(patient[j] < patient[j - 1])
      {
        swap(patient[j], patient[j-1]);
        count = count + 1;
        noChange = false;
      }
    }
    if (noChange)
      return count; // returning count
  }
  return count; // returning count
}

这是我更新的代码。计数值返回0。我不知道我使用的代码是对还是错(我称之为计数的返回值)。有什么想法吗?

PS 此外,在将我的函数从 void 更改为 int 之后,由于某种原因,我的代码在写入“文本”文件时停止按字母顺序对数据进行排序。这是怎么回事?

【问题讨论】:

  • 你在哪里初始化count
  • 不显示值是什么意思?请具体。如果您的预期输出与实际输出不匹配,请同时显示。
  • 你没有使用bubbleSort()的返回值——当然它没有显示任何东西。
  • 另外,错误可能是因为您在顶部定义了void bubbleSort(T ar[], int sz);,但在底部声明了int bubbleSort(T patient[], int size)。以后,请告诉我们错误是什么,而不仅仅是说“它给出了错误”
  • 是的,我真的很抱歉。解决您提到的这个问题会导致我的代码成功构建。但是,我不知道如何将“function”中的“count”调用为“main”。

标签: c++ algorithm bubble-sort


【解决方案1】:

你没有使用函数的返回值,为什么不让函数返回int - 并返回交换次数:

int bubbleSort(T patient[], int size) //returning an int
{
   int count = 0; //initializing count
   bool noChange = true; 
    for(int i = size; i > 0; i--)
  {
    noChange = true;
    for(int j = 1; j < i; j++)
    {
      if(patient[j] < patient[j - 1])
      {
        swap(patient[j], patient[j-1]);
        count = count + 1;
        noChange = false;
      } 
    } 
    if (noChange)
      return count; // returning count
  } 
  return count; // returning count
}

附言
原始代码中的问题可能出在您声明或初始化 count 的位置(代码快照中未显示)。

另外,使用局部变量通常比使用全局更好。

【讨论】:

  • 真的很想了解如何处理代码。请检查我的编辑版本。
  • 代码不再按字母顺序排序。另外,我得到了错误的计数值(可能是因为编码错误)。然后我使用了一个全局“count”变量,将 int 改回 void,现在一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多