⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex4_3.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#pragma hdrstop

#include "random.h"
#include "ticks.h"

// sort an array of integers using the exchange sort
void ExchangeSort(int a[], int n)
{
	int i, j, tmp;
   
	// place the final value in a[0], a[1], ..., a[n-2]
	for (i = 0; i < n-1; i++)
		// place smallest of a[i],...,a[n-1] in a[i]
		for (j = i+1; j < n; j++)
			if (a[j] < a[i])
			{
				// exchange a[i] and a[j]
				tmp = a[i];
 				a[i] = a[j];
				a[j] = tmp;
			}
}

void main(void)
{
	RandomNumber rnd;
	int *a, n;
	long ticks;
	
	cout << "Enter the number of elements to sort: ";
	cin >> n;

	// creat a dynamic integer array with n elements
	a = new int[n];
	
	// initialize a with random numbers in range 0..999
	for(int i=0;i < n;i++)
		a[i] = rnd.Random(1000);
		
	// number of 1/60 th secs. since system started
	ticks = TickCount();
	// sort
	ExchangeSort(a,n);
	// compute number 1/60 th secs the sort took
	ticks = TickCount() - ticks;
	
	cout << "Time: " << ticks/60.0 << " seconds" << endl;
}

/*
<Run #1>

Enter the number of elements to sort: 50
Time: 0 seconds

<Run #2>

Enter the number of elements to sort: 500
Time: 0.05 seconds

<Run #3>

Enter the number of elements to sort: 1000
Time: 0.383333 seconds

<Run #4>

Enter the number of elements to sort: 10000
Time: 28.8833 seconds
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -