您的位置:首页 > 其它

第三周作业——冒泡排序和归并排序

2014-03-27 22:10 246 查看
Timer.h

#ifndef TIMER_H
#define TIMER_H

#include <time.h>

class Timer
{
private:
	clock_t beginTime, endTime;
	double interval;

public:
	Timer();
	double GetElapsedTime(void);
	void ResetTimer(void);
};

#endif


Timer.cpp

#include "Timer.h"

Timer::Timer()
{
	ResetTimer();
}

void Timer::ResetTimer()
{
	beginTime = clock();
	endTime = beginTime;
	interval = 0;
}

double Timer::GetElapsedTime()
{
	endTime = clock();
	interval = (double)(endTime - beginTime) / CLOCKS_PER_SEC;

	return interval;
}


Main.cpp

#include "Timer.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

long GetNumOfData(char* fileName)
{
	ifstream fin;

	long temp;
	long numOfData = 0;		

	fin.open(fileName);	

	while (!fin.eof())
	{
		numOfData++;
		fin >> temp;		
	}
	cout << "The number of data is: " << numOfData << endl;

	fin.close();

	return numOfData;
}

void LoadDataFromFile(char* fileName, long* data, long num)
{
	ifstream fin;

	fin.open(fileName);
	
	for (int i = 0; i < num; i++)
	{
		fin >> data[i];
	}

	cout << "File loading complete!" << endl;

	fin.close();
}

void WriteDataToFile(char* fileName, long* data, long num)
{
	ofstream fout;
	fout.open(fileName);
	for (int i = 0; i < num; i++)
	{
		fout << data[i] << " ";
	}
	fout.close();
}

void BubbleSort(long* data, long numOfData)
{
	Timer timer;

	long exchanged = numOfData - 1;
	long bound;
	long temp;

	cout << "sorting..." << endl;
	while (exchanged != 0)
	{		
		bound = exchanged;
		exchanged = 0;

		for (int j = 0; j < bound; j++)
		{
			if (data[j] > data[j+1])
			{
				temp = data[j];
				data[j] = data[j+1];
				data[j+1] = temp;

				exchanged = j;
			}
		}		
	}
	cout << "sorting complete!" << endl;
	cout << "\nElapsed time: " << timer.GetElapsedTime() << " seconds!" << endl;

	WriteDataToFile("data_bubble.txt", data, numOfData);	
}

//一次归并排序
//将两个相邻序列data[s]~data[m]和data[m+1]~data[t]归并到辅助数组auxiliary[s]~auxiliary[t]中
void Merge(long* data, long* auxiliary, long s, long m, long t)
{
	long i = s;
	long j = m + 1;
	long k = s;

	while (i <= m && j <= t)
	{
		//取data[i]和data[j]中的较小者放入auxiliary[k]中
		if (data[i] <= data[j])
		{		
			auxiliary[k++] = data[i++];
		}
		else
		{
			auxiliary[k++] = data[j++];
		}
	}

	//对未处理完的序列进行收尾处理
	if (i <= m)
	{
		while (i <= m)
		{
			auxiliary[k++] = data[i++];
		}
	}
	else
	{
		while (j <= t)
		{
			auxiliary[k++] = data[j++];
		}
	}
}

//一趟归并排序,n:待排序数据总个数,h:待排序的有序序列的长度
void MergePass(long* data, long* auxiliary, long n, long h)
{
	int i = 0;

	while (i <= (n - 2*h))
	{
		Merge(data, auxiliary, i, i+h-1, i+2*h-1);

		i += 2 * h;
	}

	if (i < n - h)
	{
		//待归并序列中有一个长度小于h
		Merge(data, auxiliary, i, i+h-1, n-1);
	}
	else
	{
		//待归并序列中只剩下一个长度小于h的序列
		for (int k = i; k < n; k++)
		{
			auxiliary[k] = data[k];
		}
	}
}

void MergeSort(long* data, long* auxiliary, long n)
{
	Timer timer;

	int h = 1;                       //初始时子序列的长度

	cout << "sorting..." << endl;
	while (h < n)
	{
		MergePass(data, auxiliary, n, h);
		h *= 2;

		//若归并的趟数为奇数,排序结果在辅助数组中,所以要将结果传回到原数组中
		MergePass(auxiliary, data, n, h);
		h *= 2;
	}
	cout << "sorting complete!" << endl;
	cout << "\nElapsed time: " << timer.GetElapsedTime() << " seconds!" << endl;

	WriteDataToFile("largeW_merge.txt", data, n);
}

void main()
{	
	long numOfData = GetNumOfData("largeW.txt");
	long* data = new long[numOfData];

	LoadDataFromFile("largeW.txt", data, numOfData);	

	

	//BubbleSort(data, numOfData);
	long* auxiliary = new long[numOfData];
	MergeSort(data, auxiliary, numOfData);
	delete[] auxiliary;
	

	delete[] data;	
}


冒泡排序的运行时间如下:



归并排序的运行时间如下:



有上图可以看出,不同算法的运行效率差距如此之大,真的是算法改变世界啊!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: