课程作业——数据结构与算法C++(1)

2023-05-16

课程作业6(归并排序 冒泡排序 插入排序 选择排序)

/*归并排序 冒泡排序 插入排序 选择排序*/
//主程序
#include<iostream>
#include<vector>
using std::vector;
using std::cout;
using std::endl;
using std::swap;
#include"bubble_sort.h"
#include"insert_sort.h"
#include"select_sort.h"
#include"merge_sort.h"

template<typename T>
void print(vector<T>& a)
{
	for (int i = 0; i != a.size(); i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

int main()
{
	//vector<int> v = { 34,8,64,51,32,21 };
	//vector<int> v = { 6,5,4,3,2,1 };
	vector<int> v = { 34,8,64,51,32,21,67,34,8,64,51,32,21,12,34,8,64,51,32,21,34 };
	//vector<int> v = { 34,34,34,34,34,34,34 };
	//vector<char> v = { 'd','s','A','P','k','a','T' };
	//int a[] = { 34,8,23,51,32,23,21 };
	//vector<int> v(&a[0],&a[0]+7);

    cout << "原始排序:";
	print(v);

	cout << "归并排序:";
	merge_sort(v);

	print(v);


    cout << "冒泡排序:";
	bubble_sort(v); 
	print(v);
	cout << "插入排序:";
	insert_sort(v);
	print(v);
	cout << "选择排序:";
	select_sort(v);
	print(v);
	
	
	system("pause");
	return 0;
}


/********************bubble_sort.h*******************/
#pragma once
//冒泡排序算法(全部元素)
template<typename T>
void bubble_sort(vector<T>& a)
{
	bubble_srt(a, 0, a.size());
	
}
//冒泡
template<typename T>
void bubble(vector<T>& a, int first, int last)
{
	for (int i = first + 1; i != last; i++)if (a[i - 1] > a[i]) swap(a[i - 1], a[i]);
}
//冒泡排序算法
template<typename T>
void bubble_srt(vector<T>& a, int first, int last)
{
	for (int i = last; i != first; i--)bubble(a, first, i);
}


/********************merge_sort.h*******************/
#pragma once
#include<deque>
using std::deque;
#include<algorithm>
//合并排序算法a[first,last)
template<typename T>
void merge_sort(vector<T>& a, int first, int last)
{
	if (first<last-1)
	{
		int mid = (first + last) / 2;
		merge_sort(a, first, mid);
		merge_sort(a, mid, last);
		merge(a, first, mid, last);
	}
}
//合并a[first,mid)和a[mid,last)
template<typename T>
void merge(vector<T>& a, int first, int mid, int last)
{
	deque<T> dq;
	int k = first;
	for (int i = first; i < mid; i++)dq.push_back(a[i]);
	for (int i = last-1; i >= mid; i--)dq.push_back(a[i]);
	while (!dq.empty())
	{
		if (dq.front()<dq.back())
		{
			a[k++] = dq.front();
			dq.pop_front();
		}
		else
		{
			a[k++] = dq.back();
			dq.pop_back();
		}
	}

}
//归并排序算法(全部元素)
template<typename T>
void merge_sort(vector<T>& a)
{
	merge_sort1(a, 0, a.size());

}



//自底向上合并排序
template<typename T>
void merge_sort1(vector<T>& a, int first, int last)
{
	for (int i = 1; i <= last-first; i=i+i)//子区间长度倍增
	{
		for (int j = first; j <= last-i; j+=i+i)
		{
			merge(a, j, j + i, std::min(j + i + i, last));
		}
	}
}
//自然合并排序
/********************insert_sort.h*******************/
#pragma once
//插入元素a[last]到有序区间[first,last)
template<typename T>
void insert(vector<T>& a, int first, int last)
{
	T x = a[last];
	int i = last;
	while ((i != first) && (x < a[i - 1]))//注意前后顺序
	{
		a[i] = a[i - 1]; i--;
	}
	a[i] = x;
}
//插入排序算法
template<typename T>
void insert_sort(vector<T>& a, int first, int last)
{
	for (int i = first+1; i != last; i++)
	{
		insert(a, first, i);
	}
}
//插入排序算法(全部元素)
template<typename T>
void insert_sort(vector<T>& a)
{
	insert_sort(a, 0, a.size());
	
}
/********************select_sort.h*******************/
#pragma once
//确定[first,last)中最小元素位置
template<typename T>
int min_elem(vector<T>& a, int first, int last)
{
	int pos = first;
	for (int i = first + 1; i != last; i++)if (a[i] < a[pos])pos = i;
	return pos;
}
//选择排序算法
template<typename T>
void select_sort(vector<T>& a, int first, int last)
{
	for (int i = first; i != last-1; i++)
	{
		int j = min_elem(a, i, last);
		if(i!=j)swap(a[i], a[j]);
	}
}
//选择排序算法(全部元素)
template<typename T>
void select_sort(vector<T>& a)
{
	select_sort(a, 0, a.size());
	
}

课程作业7(快速排序 随机快速排序)

/* 快速排序 随机快速排序*/
//主程序
#include<iostream>
#include<vector>
using std::vector;
using std::cout;
using std::endl;
using std::swap;
#include"insert_sort.h"

#include"qsort.h"
#include"rand_qsort.h"

template<typename T>
void print(vector<T>& a)
{
for (int i = 0; i != a.size(); i++)
{
cout << a[i] << " ";
}
cout << endl;
}


//快速排序
int main()
{
vector<int> v = { 34,8,64,51,32,21,67,34,8,64,51,32,21,12,34,8,64,51,32,21,34 };
cout << "原始排序:";
print(v);
cout << "快速排序:";
rand_qsort(v);
print(v);

vector<int> v1 = { 34,8,64,51,32,21,67,34,8,64,51,32,21,12,34,8,64,51,32,21,34 };
cout << "原始排序:";
print(v1);
cout << "随机快速排序:";
rand_qsort(v1);
print(v1);

system("pause");
return 0;
}

/********************qsort.h*******************/
#pragma once
const int threshold =1;
//区间分割
template<typename T>
int partition(vector<T>& a, int first, int last,T pivot)
{
	while (true)
	{
		last--;
		while (a[first] < pivot)first++;
		//while ((a[first] <= pivot) && (first<last))first++;//error
		
		while (pivot < a[last])last--;
		//while ((pivot < a[last])&&(first<last))last--;//error
		if (!(first < last))
			return first;
		swap(a[first], a[last]);
		first++;
	}
	
}
/*template<typename T>
int partition(vector<T>& a, int first, int last, T pivot)
{
	last--;
	while (last >first)
	{
		while ((a[first] < pivot))first++;//error && (first<last)
		
		
		while ((pivot < a[last]))last--;//error&&(first<last)
		swap(a[first], a[last]);
		
	}
return first;
}*/
//递归快速排序ooooooo
template<typename T>
void qsort(vector<T>& a, int first, int last)
{
	while (last - first > threshold)
	{
		T pivot = median(a[first], a[(first + last) / 2], a[last - 1]);
		//T pivot = a[last-1];
		//T pivot = a[first];//error
		int cut = partition(a, first, last, pivot);
		qsort(a, cut, last);
		last = cut;
	}
	insert_sort(a, first, last);
}
//取三点的中位数
template<typename T>
const T& median(const T& a, const T& b, const T& c)
{
	if (a < b)
		if (b < c)return b;
		else if (a < c)return c;
		     else return a;
	else if (a < c)return c;
	     else if (b < c)return c;
		      else return b;
}

template<typename T>
void qsort(vector<T>& a)
{
	qsort(a, 0, a.size());
	
}

/********************rand_qsort.h*******************/
#pragma once
#include <ctime>
using std::rand; 

template<typename T>
void rand_qsort(vector<T>& a, int first, int last)
{
	while (last - first > threshold)
	{
		T pivot = a[random(first,last)];
		
		int cut = partition(a, first, last, pivot);
		rand_qsort(a, cut, last);
		last = cut;
	}
	insert_sort(a, first, last);
}
template<typename T>
void rand_qsort(vector<T>& a)//随机化快速排序
{
	rand_qsort(a, 0, a.size());
	
}

int random(int first, int last)
{
	//根据系统时间设置随机数种子
	srand(time(NULL));//(unsigned)

	return first+rand()%(last-first);
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

课程作业——数据结构与算法C++(1) 的相关文章

  • C#集合

    文章目录 C 集合简介C ArrayList类 xff1a 动态数组C Queue类 xff1a 队列C Stack类 xff1a 堆栈C Hashtable类 xff1a 哈希表 xff08 散列表 xff09 C SortedList类
  • C#泛型

    文章目录 C 泛型简介C 可空类型 xff1a NullableC 泛型方法的定义及使用C 泛型类的定义及使用C 泛型集合定义及使用C IComparable IComparer接口 xff1a 比较两个对象的值 泛型是在 System C
  • C#文件操作

    在前面操作变量和常量时这些值都是存放到内存中的 xff0c 当程序运行结束后使用的数据全部被删除 若需要长久保存应用程序中的数据 xff0c 可以选用文件或数据库来存储 文件通常存放到计算机磁盘上的指定位置 xff0c 可以是记事本 Wor
  • C#委托和事件

    C 语言中的委托和事件是其一大特色 xff0c 委托和事件在 Windows 窗体应用程序 ASP NET 应用程序 WPF 应用程序等应用中是最为普遍的应用 通过定义委托和事件可以方便方法重用 xff0c 并提高程序的编写效率 C 中的委
  • C#异常与调试

    在 C 语言中 xff0c 异常也称为运行时异常 xff0c 它是在程序运行过程中出现的错误 对于异常的处理需要程序员积累经验 xff0c 在可能出现异常的位置加入异常处理语句 C Exception xff1a 异常类 NET Frame
  • C#进程与线程

    在操作系统中 xff0c 每运行一个程序都会开启一个进程 xff0c 一个进程由多个线程构成 线程是程序执行流中最小的单元 在应用程序中分为单线程程序和多线程程序 单线程程序是指在一个进程空间中只有一个线程在执行 xff1b 多线程程序是指
  • WPF入门

    文章目录 WPF概述WPF简介WPF 开发环境搭建XAML语言介绍 WPF常用控件WPF常用控件分类及介绍WPF文本类型控件WPF内容控件 WPF概述 WPF简介 Windows Presentation Foundation 新一代图形用
  • 2020-08-20网易互娱一面

    1 a b两数组均升序排列 将b数组所有成员融合到a数组里面 xff08 a数组足够大 xff09 维持两个指针 xff0c 从后往前比较 2 最小生成树 3 判断栈的输出顺序 其他 线程进程 TCP UDP Linux命令
  • 2020-08-20商汤科技笔试A卷

    文章目录 1 查找 Good 字符串2 最长上升子序列 xff0c leetcode原题 3 求删除区间的最小个数 xff0c 可以使得删除后剩下的区间彼此不重叠 1 查找 Good 字符串 题目描述 给定一个字符串 xff0c 在字符串中
  • 使用一组坐标信息拟合圆(matlab)

    详细原理参考MATLAB圆拟合 圆拟合 function span class token punctuation span xc span class token punctuation span yc span class token
  • BH1750( GY-302 )光照传感器

    文章目录 一 产品简介二 IIC通信三 BH1750的使用四 程序源码 这里我先简单的介绍一下BH1750光照传感器模块的基本信息 不多废话 xff0c 我将着重讲解它的使用部分 xff0c 相信对于屏幕前的你也是更关心它是怎么使用的 xf
  • 海康相机SDK

    span class token comment 获取SDK版本号 span span class token keyword static span span class token keyword uint span span clas
  • 2020-09-03百度笔试题

    2 找角色 输入 xff1a span class token number 1 span span class token number 3 span span class token number 6 span span class t
  • 2020-09-08小米笔试

    span class token macro property span class token directive keyword include span span class token string lt iostream gt s
  • Halide: 一种用于优化图像处理管道中的并行性、局部性和重新计算的语言和编译器

    Halide span class token operator span A Language span class token operator and span Compiler span class token keyword fo
  • 现代光学字符识别技术综述

    A survey of modern optical character recognition techniques 文章目录 摘要1 介绍1 1 OCR是模式识别的一个成功分支1 2 两类OCR系统1 3 现代OCR的主要趋势1 4 本
  • OCR研究与发展的历史回顾

    Historical Review of OCR Research span class token operator and span Development 文章目录 摘要1 介绍2 OCR的黎明3 试一试的时代3 1 模板匹配方法3
  • 深度学习时代的文字检测与识别技术

    深度学习时代的文字检测与识别技术
  • C++程序设计实践指导——第一章 简单编程 (1)

    第一章 简单编程 xff08 1 xff09 1 1删除序列中相同的数 有16个数 1 2 2 3 4 4 5 6 6 7 8 8 8 9 xff0c 10 xff0c 10 xff0c 已按由小到大的顺序排好 存储 在数组a中 试建立一个
  • 模板template的使用

    函数模板应用实例 C 43 43 与C的不同点 xff1a 模板template的使用 include lt iostream gt 无需加上 34 h 34 using namespace std 使用命名空间 template lt t

随机推荐

  • 时间复杂度与空间复杂度

    时间复杂度与空间复杂度 1 Running time xff1a 时间复杂度 T n 61 O f n 时间复杂度 xff1a 算法执行时间随规模增大而增长的趋势 以算法中重复执行的次数作为算法时间复杂度的依据 计算机科学中 xff0c 算
  • 对抗样本是怎么产生的?如何避免对抗攻击?

    全文共2637字 xff0c 预计学习时长5分钟 图片来源 xff1a pexels 64 pixabay 随着深度神经网络的出现 xff0c 机器学习领域的安全问题日益突出 人们对神经网络的可解释性提出了质疑 xff0c 也自然对深度学习
  • 数组与指针的区别

    数组与指针的区别 1 指针数组与数组指针 int a1 3 It is an array each element is a pointer int a2 3 It is a pointer It points to an array wi
  • while/do while/for/goto的区别

    Calculate 1 43 2 43 3 43 43 100
  • if/switch的区别

  • Function pointer and pointer function指针函数和函数指针

    指针函数 int f int x int y pointer function f is a function it returns a pointer 函数指针 int f int x int y function pointer f i
  • 类的应用示例+继承

    类的应用示例 include lt iostream gt using namespace std 使用命名空间 class student constructor initializer list 构造函数初始化列表 public stu
  • Structure and Class (结构体与类)

  • STL中vector的使用

    STL中vector vector的使用 include lt iostream gt include lt vector gt using namespace std 使用命名空间 int main vector lt int gt v1
  • Fibonacci的四种求解方法

    Fibonacci Sequence的使用 include lt iostream gt using namespace std 使用命名空间 T N 61 O N 2 61 T N 1 43 T N 2 43 2 int fib2 int
  • 最大子序列和问题

    最大子序列和问题Maximum Subsequence Sum include lt iostream gt include lt vector gt using namespace std 使用命名空间 T N 61 O N 3 Cubi
  • C++基础之基础

    C 43 43 xff08 容纳了好几种编程范式 xff09 xff1a 面向对象编程 泛型编程 过程化编程 面向对象编程 xff1a 其本质是以建立模型体现出来的抽象思维过程和面向对象的方法 抽象 继承 多态 xff1a 抽象性是指将具有
  • 这五大MySQL在线课程,最适合初学者的你!

    全文共3214字 xff0c 预计学习时长7分钟 图片来源 xff1a pexels com 64 pixabay 过去几年 xff0c 有句话越来越流行 xff1a 人人必须学习如何编码 这值得鼓励 在当今以信息和技术为中心的世界里 xf
  • 课程作业(单链表C++实现)

    单链表的操作C 43 43 实现 include lt iostream gt using namespace std 使用命名空间 template lt typename dataType gt 定义一个数据类型模板 class lin
  • 课程作业(二叉查找树)

    mainTest cpp include lt iostream gt include 34 binarySearchTree h 34 using namespace std int main cout lt lt 34 请按前序输入一棵
  • C++程序设计实践指导——第一章 简单编程 (2)

    第一章 简单编程 xff08 2 xff09 1 9 统计与替换字符串中的关键字 建立一个类WordNum xff0c 统计一个英文字符串中的英文单词个数 字符串中的各英文单i司以一个或多个空格分隔 如字符串 34 I am a stude
  • STL+Python+图像处理-学习资源

    1 C 43 43 STL学习网站 CPlusPlus com CppReference com gcc gnu org 2 Python学习书籍及网站 Python Crash Course Learn Python the Hard W
  • STL----------C++Primer(笔记)

    1 string string word cin gt gt word getline cin word 关系操作符 lt lt 61 gt gt 61 include lt cctype gt 头文件 string s 61 34 Hel
  • 侯捷-STL与泛型编程(GP)笔记

    1 stl体系结构基础介绍 分配器 xff08 allocator xff09 xff1a 主管分配内存 适配器 xff08 adaptor xff09 xff1a 进行一个转换 xff0c 与另一个对象绑定 include lt iost
  • 课程作业——数据结构与算法C++(1)

    课程作业6 xff08 归并排序 冒泡排序 插入排序 选择排序 xff09 归并排序 冒泡排序 插入排序 选择排序 主程序 include lt iostream gt include lt vector gt using std vect