测试外壳,插入和快速的程序问题
Issues with program to test Shell, Insertion, and Quick sorts
因此,该程序应该用3个不同的整数文本文件测试运行插入,外壳和快速分类,但是出于某种原因,除了我的理解之外,任何结果都没有任何结果正在显示项目数。它应该显示使用时钟()运行每种类型所需的秒和时钟周期的数量。拜托,谁能告诉我为什么它不起作用?我很难过!
#include "targetver.h"
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdio.h>
#include <tchar.h>
#include <queue>
#include <stack>
#include <vector>
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
using namespace std;
// insertion sort function
void insertionSort(vector<int> arr, int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// shell sort function
void shellSort(vector<int> arr, int n)
{
// Start with a big gap, then reduce the gap
for (int gap = n / 2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
// keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
int temp = arr[i];
// shift earlier gap-sorted elements up until the correct
// location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
// put temp (the original a[i]) in its correct location
arr[j] = temp;
}
}
}
// function that swaps two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition(vector<int> arr, int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(vector<int> arr, int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// print array function
void printArray(vector<int> arr)
{
int z = arr.size();
int i;
for (i = 0; i < z; i++)
printf("%d ", arr[i]);
printf("n");
}
int main()
{
int max = 10000000;
vector<int> arr;
arr.reserve(max);
double start, end, elapsed_clock, elapsed_time;
int low, high, n;
string name;//holds first file name entered by user
ifstream fin;
cout << "Please enter the file name you wish to read from: ";//asks user for file name
getline(cin, name);//gets file name
fin.open(name);//opens file with set file name
if (fin.fail())//run if file name is incorrect or fails to load
{
cout << "Error opening " << name << "n";//print error and file name
}
else
{
cout << "nFile opened successfully, please wait." << endl;
// holds data read from file
int theData;
do // loop reads file till end
{
fin >> theData;
if (fin.good())
{
arr.push_back(theData);
}
//if read failed, check to see if file end was cause, otherwise print message and close
else if (!fin.eof())
{
cout << "nThe file could not be read" << endl;
}
//runs while data being read
} while (!fin.eof());
}
fin.close();
n = sizeof(arr) / sizeof(arr[0]);
start = clock();
insertionSort(arr, n);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Insertion Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
start = clock();
shellSort(arr, n);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Shell Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Quick Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
printArray(arr);
cout << endl;//space
cout << "Please enter the file name you wish to read from: ";//asks user for file name
getline(cin, name);//gets file name
fin.open(name);//opens file with set file name
if (fin.fail())//run if file name is incorrect or fails to load
{
cout << "Error opening " << name << "n";//print error and file name
}
else
{
cout << "nFile opened successfully, please wait." << endl;
// holds data read from file
int theData;
do // loop reads file till end
{
fin >> theData;
if (fin.good())
{
arr.push_back(theData);
}
//if read failed, check to see if file end was cause, otherwise print message and close
else if (!fin.eof())
{
cout << "nThe file could not be read" << endl;
}
//runs while data being read
} while (!fin.eof());
}
fin.close();
n = sizeof(arr) / sizeof(arr[0]);
start = clock();
insertionSort(arr, n);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Insertion Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
start = clock();
shellSort(arr, n);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Shell Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Quick Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
printArray(arr);
cout << endl;//space
cout << "Please enter the file name you wish to read from: ";//asks user for file name
getline(cin, name);//gets file name
fin.open(name);//opens file with set file name
if (fin.fail())//run if file name is incorrect or fails to load
{
cout << "Error opening " << name << "n";//print error and file name
}
else
{
cout << "nFile opened successfully, please wait." << endl;
// holds data read from file
int theData;
do // loop reads file till end
{
fin >> theData;
if (fin.good())
{
arr.push_back(theData);
}
//if read failed, check to see if file end was cause, otherwise print message and close
else if (!fin.eof())
{
cout << "nThe file could not be read" << endl;
}
//runs while data being read
} while (!fin.eof());
}
fin.close();
n = sizeof(arr) / sizeof(arr[0]);
start = clock();
insertionSort(arr, n);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Insertion Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
start = clock();
shellSort(arr, n);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Shell Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
elapsed_clock = end - start;
elapsed_time = ((end - start) / CLK_TCK);
cout << "Quick Sortt: " << arr.size() << " items" << " " << elapsed_clock << " ticks" << " " << elapsed_time << " secn";
printArray(arr);
cout << endl;//space
system("Pause");//waits for user input
return 0;
}
您应该将所有向量传递给参考,而不是通过值。
相关文章:
- C++程序问题:抛出'std::invalid_argument'实例后终止调用
- 测试外壳,插入和快速的程序问题
- Mongodb c ++驱动程序问题
- 输出中的特定递归反向程序问题
- C++ std::endl 的多线程程序 I/O 问题
- 我如何利用核心文件在C/C 中找到应用程序问题
- 套接字 TCP 服务器程序问题
- C++,结构数据库程序问题
- visual c++程序问题
- C++多个程序问题(rand、转换、崩溃)
- 非常基本的C++程序问题 - 二进制表达式的操作数无效
- C++练习车辆程序问题 - 初学者编程
- 关于堆和堆栈应用程序问题
- Boost asio:io服务在销毁步骤中被阻止了更多的处理程序问题
- 使用链接linux编译GLFW应用程序问题
- C++计算器程序.问题
- 基于WIndows - manifest的ETW提供程序问题
- 求1到100之间质数的程序问题
- 文件加载程序问题
- mfcc++托盘应用程序问题