一、分治算法实现和时间复杂性验证

1、  实验内容
1)编程实现归并排序算法和快速排序(交换类)算法。
2)程序中使用一个变量对元素的比较运算进行计数,将实际计数和理论分析的时间复杂度比较。
2、  实验要求
1)  通过实例,加深对分治法基本策略的理解,和分治算法的设计方法。
分治法是建基于多项分支递归的一种很重要的算法范式。字面上的解释是“分而治之”,就是把一个复杂的问题分成两个或更多的相同或相似的子问题,直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合并。
2)  用2-3个实例验证算法和时间复杂度,要求有不同的n和数据分布。
3)  比较归并排序和快速排序的相同点和不同点。
归并排序C++算法:
#include <iostream>
#include<string>
using namespace std;

template<class T>//能使用多种类型数据
void Merge(T c[], T d[], int l, int m, int r)
{//把c[l:m]和c[m:r]归并到d[l:r]
int i, j, k;
i = l;    //第一段游标
j = m + 1;   //第二段游标
k = l;      //结束游标
while ((i <= m) && (j <= r))
if (c[i] <= c[j])
d[k++] = c[i++];
else d[k++] = c[j++];
if (i>m)for (int q = j; q <= r; q++)
d[k++] = c[q];
else for (int q = i; q <= m; q++)
d[k++] = c[q];
}

template<class T>
void MergePass(T x[], T y[], int s, int n){
//归并大小为s的相邻的段
int i = 0;
while (i <= n - 2 * s){
//归并两个大小为s的相邻段
Merge(x, y, i, i + s - 1, n - 1);
i = i + 2 * s;
}
//剩下不足两个元素
if (i + s<n)Merge(x, y, i, i + s - 1, n - 1);
else for (int j = i; j <= n - 1; j++)
//把最后一段复制到y
y[j] = x[j];
}

template<class T>
void MergeSort(T a[], int n)
{
T* b = new T[n];
int s = 1;
while (s<n){
MergePass(a, b, s, n);
s += s;
MergePass(b, a, s, n);
s += s;
}
}

void main()
{
int n, i, j = 0;
double *Input;
cout << "Please enter the number of elements: " << endl;
cin >> n;
Input = new double[100000];//定义排序数组
cout << "Please enter the number : " << endl;
for (i = 0; i<n; i++)
cin >> *(Input + i);
MergeSort(Input, n);//调用排序函数
cout << "The answer is: " << endl;
for (i = 0; i<n; i++)
{
cout << Input[i] << "  ";
j++;
if (j % 10 == 0)
cout << endl;
}
system("pause");
}
归并排序java算法:

public class merge {
public static int count = 0;
public static void main(String[] args){
Comparable []list = {14,43,25,62,46,22,75,34,87,97,32};
allPrint(list);

Comparable []list1 = {15,9,4,20,7,11,33};
allPrint(list1);
} 

public static void allPrint(Comparable []a){//将整个数组排序前后的情况显示出来
System.out.println("数组大小: " + a.length);
System.out.print("排序前数组: ");
print(a);
mergeSort(a);
System.out.print("\n排序后数组: ");
print(a);
System.out.println("\n比较次数: " + count);
System.out.println();
}
public static void print(Comparable []a){//打印数组
for(int i = 0;i < a.length;i++){
System.out.print(a[i]+" ");
}
}
public static void mergeSort(Comparable []a){
mergeSort(a,0,a.length - 1);
}

public static void mergeSort(Comparable []a,int left,int right){//归并排序算法
if(left < right){//若left小于right 对左右两段数组进行归并排序,并递归
int i = (left + right)/2;
mergeSort(a,left,i);
mergeSort(a,i + 1,right);
merge(a,left,i,right);
}
}

public static void merge(Comparable []a,int left,int m,int right){//合并两段数组
Comparable []b = new Comparable[a.length];
for(int x = left;x <= right;x++){
b[x] = 0;
}
int i = left,j = m + 1,k = left;

while((i <= m) && (j <= right)){
if(a[i].compareTo(a[j]) <= 0){
b[k++] = a[i++];
count++;
}
else{
count++;
b[k++] = a[j++];
}
}

if(i > m){
for(int x = j;x <= right;x++){
b[k++] = a[x];
}
}
else{
for(int x = i;x <= m;x++){
b[k++] = a[x];
}
}

for(int x = left;x <= right;x++){
a[x] = b[x];
}
}
}

快速排序C++算法:
#include<iostream>
using namespace std;
int num;

//change position 
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

void printArray(int *list)
{
for (int i = 1; i <= num; ++i)
cout << list[i] << "  ";
cout << endl;
}

int Partition(int *list, int p, int r)
{
int x = list[r];
int i = p - 1;
int j;
for ( j = p; j <= r - 1; j++)
{
if (list[j] <= x)
{
i = i + 1;
swap(list[i], list[j]);
}
}
swap(list[i + 1], list[j]);
return i + 1;
}

void quickSort(int *list, int p, int r)
{
if (p < r)
{
int q = Partition(list, p, r);
quickSort(list, p, q - 1);//左半段排序
quickSort(list, q + 1, r);//右半段排序
}
}

void main()
{
int list[100];
cout << "Please enter the number of elements:  \n";
cin >> num;
cout << "Please enter the numbers :  \n";
for (int i = 1; i <= num; i++)
cin >> list[i];
quickSort(list, 1, num);
cout << "The answer is : \n";
printArray(list);
system("pause");
}
快速排序java算法:
package exercise01;
public class quick {
public static int  count = 0;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Comparable []list = {14,43,25,62,46,22,75,34,87,97,32};
allPrint(list);

Comparable []list1 = {15,9,4,20,7,11,33};
allPrint(list1);
}

public static void allPrint(Comparable []a){//将整个数组排序前后的情况显示出来
System.out.println("数组大小: " + a.length);
System.out.print("排序前数组: ");
print(a);
quickSort(a);
System.out.print("\n排序后数组: ");
print(a);
System.out.println("\n比较次数: " + count);
System.out.println();
}

public static void print(Comparable []a){//打印数组
for(int i = 0;i < a.length;i++){
System.out.print(a[i]+" ");
}
}

public static void quickSort(Comparable []a){
quickSort(a,0,a.length - 1);
}

public static void quickSort(Comparable []a,int left,int right){//快速排序算法
if(left < right){
int i = left,j = right;
Comparable x = a[left];
while (i < j)  
       {  
           while(i < j && a[j].compareTo(x) >= 0){
            j--;
            count++;
           }
                   
           if(i < j)   
               a[i++] = a[j];  
             
           while(i < j && a[i].compareTo(x) < 0){
            i++;
            count++;
           }
                  
           if(i < j)   
               a[j--] = a[i];  
       }  
       a[i] = x;  
       quickSort(a, left, i - 1);  //递归调用   
       quickSort(a, i + 1, right); //递归调用

}
}
}









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值