1. 程式人生 > 其它 >演算法實驗一

演算法實驗一

文章目錄


實驗記錄

演算法設計與分析課程實驗


一、實驗目的:

掌握演算法設計的基本步驟,並能進行時間複雜度分析。
掌握不同演算法時間複雜度比較方法。

二、實驗內容:

1.最大公約數

分別用窮舉法和歐幾里德演算法實現求兩個整數的最大公約數,並比較演算法的效率

import java.util.Scanner;
public class Exhaustive {
		public static void main(String[] args) {
		long  a,b;
		long teg;
		Scanner scan =
new Scanner(System.in); a=scan.nextLong(); b=scan.nextLong(); long startTime=System.currentTimeMillis(); if(a>b) { teg=a; a=b; b=teg; } teg=0; for(int i=2;i<=a;i++) if(a%i==0&&b%i==0) if(i>teg) teg=i; long endTime=System.currentTimeMillis(); long
totalTime=endTime-startTime; System.out.println("最大公約數為:"+teg); System.out.println("窮舉法所用時間為:"+totalTime/1000.0+"秒"); } }
#include <iostream>
#include <time.h>
using namespace std;
long Gcd(unsigned long M, unsigned long N);
clock_t start, stop;  
double
duration; int main() { long m, n; long gcd; cin >> m; cin >> n; start = clock(); gcd = Gcd(m, n); stop = clock(); duration = (double)(stop-start) ; cout << "最大公約數為:" << gcd << endl; cout<<"歐幾里得演算法所用時間為:"<<duration<<endl; return 0; } long Gcd(unsigned long M, unsigned long N) { unsigned long temp; while(N > 0) { temp = M % N; M = N; N = temp; } return M; }

2.排序演算法效率比較

程式設計實現以下幾種不同的排序演算法(以升序為例):氣泡排序、選擇排序、 希爾排序、快速排序,比較不同的排序過程的執行時間。

#include <iostream>
#include<time.h>
using namespace std;
#define N 100000
clock_t start0,end0,start1,end1,start2,end2,start3,end3;
void Bubble_sort(int *b){//氣泡排序
	int i, j, t;
	for (i = 0; i<N- 1; i++)
		for (j = 0; j<N - i - 1; j++){
		if (b[j]>b[j + 1])
		{
			t = b[j];
			b[j] = b[j + 1];
			b[j + 1] = t;
		}
		}
}
void selection_sort(int *b){//選擇排序
	int i, j, t;
	for (i = 0; i<N; i++){
		int k = i;
		for (j = i; j<N; j++){
			if (b[j]<b[k]){
				k = j;
			}
		}
		t = b[i];
		b[i] = b[k];
		b[k] = t;
	}
}
void Diminishing_Increment_Sort(int *b){//希爾排序
	int i, j, t;
	int flag, gap = N;
	while (gap>1)
	{
		gap = gap / 2;              
		flag = 1;
		while (flag)
		{
			flag = 0;
			for (i = 0; i<N - gap; i++)
			{
				j = i + gap;
				if (b[i]>b[j])
				{
					t = b[i];
					b[i] = b[j];
					b[j] = t;
					flag = 1;
				}
			}
		}
	}
}
void Quick_sort(int *a,int start,int end){//快速排序
	if (start >= end)
	{
		return;
	}
	int  i = start, j = end;
	int key = a[i];
	while (i < j)
	{
		while (i < j && a[j] >= key)
		{
			j--;
		}
		a[i] = a[j];
		while (i < j && a[i] <= key)
		{
			i++;

		}
		a[j] = a[i];
	} 
	a[i] = key;
	Quick_sort(a, start, i - 1);
	Quick_sort(a, i + 1, end);
}int main()
{
	int a[N];
	for(int i=0;i<N;i++)
		a[i] = rand();
	double total;
	start0=clock();
	Bubble_sort(a);
	end0=clock();
    total = ((double)(end0-start0))/ CLOCKS_PER_SEC;
	cout<<"氣泡排序所用時間為: "<<total<< "秒"<<endl;
	start1=clock();
	selection_sort(a);
	start1=clock();
	total = ((double)(end0-start0))/ CLOCKS_PER_SEC;
	cout<<"選擇排序所用時間為: "<<total<< "秒"<<endl;
	start2=clock();
	Diminishing_Increment_Sort(a);
	start2=clock();
	total = ((double)(end0-start0))/ CLOCKS_PER_SEC;
	cout<<"希爾排序所用時間為: "<<total<< "秒"<<endl;
	start3=clock();
	Quick_sort(a,0,N-1);
	start3=clock();
	total = ((double)(end0-start0))/ CLOCKS_PER_SEC;
	cout<<"快速排序所用時間為: "<<total<< "秒"<<endl;
	return 0;
}