Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by thuan ( 5 years ago )
#include<iostream>
#include<fstream>
#include<time.h>
#include<stdlib.h>
#include<string>
using namespace std;

void HoanVi(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
}

void SelectionSort(int *&a, int n)
{
	for (int i = 0; i < n - 1; ++i)
	{
		int min = i;
		for (int j = i + 1; j < n; ++j)
		{
			if (a[j] < a[min])
			{
				min = j;
			}
		}
		HoanVi(a[i], a[min]);
	}
}

void Random(int a, int b, int n,string ten)
{
	ofstream fileout;
	fileout.open(ten, ios_base::out);
	srand(time(0));
	for (int i = 0; i <= n; ++i)
	{
		int x = a + rand() % (b - a + 1);
		fileout << x << "\n";
	}
	cout << "Da random xong!";
	fileout.close();
}


//CÁCH NÀY BỊ LỖI KHI DÙNG SỐ > 10.000 HUHUHU!
//void DocFile(int*& a, int &n, string ten)
//{
//	n = 0;
//	ifstream filein;
//	filein.open(ten, ios_base::in);
//	if (!filein)
//	{
//		cout << "\nfile khong ton tai. xin vui long kiem tra lai!";
//		system("pause");
//		exit(0);
//	}
//	int* temp = new int[n];
//	while (!filein.eof())
//	{
//		int x;
//		filein >> x;
//
//		bool check = false;
//		if (n != 0)
//		{
//			check = true;
//			temp = new int[n];
//			for (int i = 0; i < n; i++)
//			{
//				temp[i] = a[i];
//			}
//		}
//		n++;
//		a = new int[n];
//		for (int i = 0; i < n - 1; i++)
//		{
//			a[i] = temp[i];
//		}
//		if (check == true)
//		{
//			delete[] temp;
//		}
//		a[n - 1] = x;
//	}
//	filein.close();
//}
/*-------------------------------------------------------------------------------------------*/

bool DocDuLieuTuTapTin(string tentaptin, int*& a, int n)
{
	ifstream f;
	f.open(tentaptin, ios_base::in);
	if (!f.eof())
	{
		a = new int[n];

		for (int i = 0; i < n; ++i)
		{
			f >> a[i];
		}

		return true;
		f.close();
	}

	cout << "\nKhong tim thay!";
	return false;
}

void Xuat(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << "\n";
	}
}

int main()
{
	
	ofstream f;
	/*ĐỐI SỐ APP LÀ CHO PHEP GHI CHÈN VÀO SAU*/
	f.open("DoThoiGianCacThuatToanSapXep.txt", ios_base::app);
	/*f << "\t\t\t\tSelection Sort";
	f << "\nTap du lieu\tLan chay 1\tLan chay 2\tLan chay 3\tTrung binh";*/
	f << "\n100.000";
	double tong = 0;
	for (int i = 1; i <= 3; i++)
	{
		int* a;
		int n = 100000;
		DocDuLieuTuTapTin("100.000.txt", a, n);
		clock_t start = clock();
		SelectionSort(a, n);
		clock_t end = clock();
		double thoigian = (double)(end - start) / CLOCKS_PER_SEC;
		f << "\t\t" << thoigian ;
		tong += thoigian;
		delete[]a;
	}
	f << "\t\t" << (double)tong / 3;
	f.close();

	cout << "\nFile da duoc ghi!";

	cout << endl;
	system("pause");
	return 0;
}

 

Revise this Paste

Your Name: Code Language: