バブル ソート関数のスワッピング プロセスとソート実行時間の合計数を計算する必要があります。実行時間については、成功しました。しかし、スワッププロセスの総数については、何をすべきかよくわかりませんでした。「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)
}
「カウント」は、メイン関数に呼び出されたときに値を表示しないようです。これでスワッピングプロセスの総数を取得できるように、何を試すべきかについてのヒントはありますか?
編集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 に変更した後、何らかの理由で、コードが「テキスト」ファイルに書き込まれるときにデータをアルファベット順にソートしなくなります。これどうしたの?