1. 程式人生 > 其它 >演算法設計基礎

演算法設計基礎

  1. 分別用窮舉法和歐幾里德演算法實現求兩個整數的最大公約數,並比較演算法的效率。
#include <iostream>
using namespace std;
int qiongjufa(int x, int y)
{
	int i, n;
	n = x;
	if (n > y)
		n = y;
	for (i = n; i > 0; i--)
	{
		if (x%i == 0 && y%i == 0)
			break;
	}
	return i;
}
int oumm(int a, int b)
{
	int r,t;
	if (a < b)
	{
t = a; a = b; b = t; } while (a%b) { r = a % b; a = b; b = r; r = a % b; } return b; } int main() { int m, n; cin >> m; cin >> n; cout << "窮舉法求得" <<m<<","<<n<<"的最大公約數為:"<< qiongjufa(m, n) << endl;
cout << "歐幾里得演算法求得" << m << "," << n << "的最大公約數為:" << oumm(m, n) << endl; return 0; }

執行截圖:
在這裡插入圖片描述

  1. 排序演算法效率比較。程式設計實現以下幾種不同的排序演算法(以升序為例):氣泡排序、選擇排序、 希爾排序、快速排序,比較不同的排序過程的執行時間。具體要求:(1)為了消除資料之間差異導致排序效果的影響,使用相同的陣列進行排序,方法為:首先建立一個數組,陣列長度至少為100000,陣列元素取值範圍在[0, 100000]之間的隨機正整數,並將這個陣列複製4份,分別用不同的排序演算法進行排序。(2)記錄不同排序演算法的執行時間。(3)對完全逆序的情況進行測試,將待排序陣列賦值為逆序,即與最終排序要求完全相反。
#include <iostream>
#include <ctime>
#define N 100000
using namespace std;
void maopao(int a[])
{
	int i, j,t;
	for (i = 0; i < N-1; i++)
	{
		for (j = 0; j < N-i; j++)
		{
			if (a[i] > a[i+1])
			{
				t = a[i];
				a[i]= a[i+1];
				a[i+1] = t;
			}
		}
	}
}
void choose(int a[])
{
	int i,t,j;
	for (i = 0; i < N-1; i++)
	{
		for (j = i + 1; j < N; j++)
		{
			if (a[i]>a[j])
			{
				t=a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}
	}
}
void QuickSort(int array[], int start, int last)
{
	int i = start;
	int j = last;
	int temp = array[i];
	if (i < j)
	{
		while (i < j)
		{
			//
			while (i < j &&  array[j] >= temp)
				j--;
			if (i < j)
			{
				array[i] = array[j];
				i++;
			}

			while (i < j && temp > array[i])
				i++;
			if (i < j)
			{
				array[j] = array[i];
				j--;
			}

		}
		//把基準數放到i位置
		array[i] = temp;
		//遞迴方法
		QuickSort(array, start, i - 1);
		QuickSort(array, i + 1, last);
	}
}

//希爾排序
void shellSort(int a[], int len)
{
	int insertNum = 0;
	int INCRGAP = 2;
	unsigned gap = len / INCRGAP ; // 步長初始化,注意如果當len<INCRGAP時,gap為0,所以為了保證進入迴圈,gap至少為1!!!
	while (gap) // while gap>=1
	{
		for (unsigned i = gap; i < len; ++i) // 分組,在每個子序列中進行插入排序
		{
			insertNum = a[i];//將當前的元素值先存起來方便後面插入
			unsigned j = i;
			while (j >= gap && insertNum < a[j - gap])//尋找插入位置
			{
				a[j] = a[j - gap];
				j -= gap;
			}
			a[j] = insertNum;
		}
		gap = gap / INCRGAP;
	}
}

int main()
{
	int a[N];
	int i,count=0;
	clock_t startTime_m, endTime_m,startTime_c,endTime_c,startTime_x,endTime_x,startTime_f,endTime_f;
	for (i = 0; i < N; i++)
	{
		 a[i]=rand()%100000+0;
		 //cin >> a[i];
	}
	/*cout << "產生的隨機數為:" << endl;
	for (i = 0; i < N; i++)
	{
		cout << a[i] << " ";
		count++;
		if (count % 1000 == 0)
			cout << a[i] << " ";
	}*/
	startTime_m = clock();
	maopao(a);
	endTime_m = clock();
	cout <<"氣泡排序法的執行時間為:"<< endTime_m - startTime_m << endl;
	startTime_c = clock();
	choose(a);
	endTime_c = clock();
	cout << "選擇排序法的執行時間為:" << endTime_c - startTime_c << endl;
	/*startTime_f = clock();
	QuickSort(a,0,N-1);
	endTime_f = clock();
	cout << "快速排序法的執行時間為:" << endTime_f - startTime_f << endl;
	*/
	startTime_x = clock();
	shellSort(a,N);
	endTime_x = clock();
	cout << "希爾排序法的執行時間為:" << endTime_x - startTime_x << endl;
	return 0;
}

執行截圖

在這裡插入圖片描述