当前位置: 首页 > news >正文

C++ 算法教程

 归并排序
#include<iostream>
using namespace std;
template <class T>
void Merge(T data[],int start,int mid,int end)
{int len1 = mid - start + 1, len2 = end - mid;int i, j, k;T* left = new int[len1];T* right = new int[len2];for (i = 0; i < len1; i++)left[i] = data[i + start];for (i = 0; i < len2; i++)right[i] = data[i + mid + 1];i = 0, j = 0;for (k = start; k < end; k++){if (i == len1 || j == len2)break;if (left[i] <= right[j])data[k] = left[i++];elsedata[k] = right[j++];}while (i < len1)data[k++] = left[i++];while (j < len2)data[k++] = right[j++];delete[] left;delete[] right;
}
template <class T>
void MergeSort(T data[], int start, int end)
{if (start < end){int mid = (start + end) / 2;MergeSort(data, start,mid);MergeSort(data, mid + 1, end);Merge(data, start, mid, end);}
}
void show(int*temp,int n)
{for (int i = 0; i < n; i++)cout << temp[i] << "  ";
}
void main()
{int temp[8];for (int i = 0; i < 8; i++)cin >> temp[i];MergeSort<int>(temp, 0,7);show(temp, 8);
}

冒泡排序_相邻交换

#include <stdio.h>  int main()  
{  int i,p,temp;  int array[10] = {2,6,1,9,4,7,5,8,3,0};  printf("Display this array:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  for(i = 1;i < 10; ++i)
{for(p = 0; p < 10; ++p){if(array[i]>array[p]){array[i] = 	array[i]^array[p];array[p] = array[i]^array[p];	array[i] = array[i]^array[p];}}
}printf("\n");  printf("After sorting,this array is:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }     printf("\n");  return 0;  
}  
判断循环链表

1.建立set集合,每次遍历存储元素,当集合大小不变,但循环仍在继续时说明存在循环,并得出该位置
2.定义双指针遍历:一个指针每次移动一个节点,一个指针每次移动2个节点,当2个节点指针在一个节点指针后面时,此链表存在循环。
3.链表反向
4.构造双向链表

swap

a:1001
b:1100
a=a^b;  a:0101
b=a^b;  b:1001
a=a^b;  a:1100

a = a + b;
b = a - b;
a = a - b;

直插排序
#include <stdio.h>  int main()  
{  int i,p,temp;  int array[10] = {2,6,1,9,4,7,5,8,3,0};  printf("Display this array:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  //选择第一个数做为起始排序for(i = 1; i < 10; ++i){temp = array[i];	//待插入排序数for(p = i-1; p >=0 && array[p]>temp; --p){//遍历已排序数列表{//如果当前数大小在已排序范围中,开始向右位移一个数让出空间array[p+1] = array[p];	}}//找到待排序数的位置,在让出的空间直接插入		array[p+1]=temp;			}printf("\n");  printf("After sorting,this array is:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }     printf("\n");  return 0;  
}  

#include <stdio.h>  int main()  
{  int i,j,t;  int array[10]={2,7,1,8,5,9,3,4,0,6};  printf("\nDisplay this array:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  printf("\n");  for(i=1;i<=9;i++)  {  //遍历int t = i-1;  //假设当前数为最小数for(j=i;j<10;j++)  {  if(array[j]<array[t])  {  //遍历找到最小的数,保存最小数索引t=j;  }  }  if(t!=(i-1))  {  //交换最小数与假设最小数int temp = 0;  temp=array[i-1];  array[i-1]=array[t];  array[t]=temp;  }  }  printf("After sorting,this array is:\n");  for(i=0;i<10;i++)  {  printf("%d ",array[i]);  }  printf("\n");  return 0;  
}  

https://github.com/sashafierce/100-days-of-Algorithm-Challenge

GitHub - hackerkid/Awesome-Data-Structures: C++ implementation of basic data structures and algorithms

GitHub - hackerkid/LightOJ-Solutions: :sparkles: LightOJ Solutions with hints

GitHub - mmc-maodun/Data-Structure-And-Algorithm: Data Structure And Algorithm(常用数据结构与算法C/C++实现)

https://leetcode.com/

Codewars - Achieve mastery through coding practice and developer mentorship

Khan Academy | Free Online Courses, Lessons & Practice

https://github.com/sashafierce/Algo_Ds_Notes

位操作基础篇之位操作全面总结_c++位操作-CSDN博客

https://www.cnblogs.com/findumars/p/5180528.html


创作不易,小小的支持一下吧!

http://www.lryc.cn/news/374121.html

相关文章:

  • 【支持向量机】问题梳理
  • 车载网络安全指南 网络安全框架(二)
  • 元数据、数据元、数据字典、数据模型及元模型的区别详解
  • 【百度智能体】零代码创建职场高情商话术助手智能体
  • 实战项目: 负载均衡
  • 运维监控系统
  • 第3章 Unity 3D着色器系统
  • Qt项目天气预报(1) - ui界面搭建
  • 一、从C语言到C++(一)
  • MySQL(5)
  • 区块链之快照
  • 自学前端第一天
  • SQL Server几种琐
  • redis 一些笔记1
  • 【计网复习】应用层总结(不含HTTP和错题重点解析)
  • carbondata连接数优化
  • 云和运维(SRE)的半生缘-深读实证02
  • java基础操作5——java自定义获取任意年、月、日的起始和结束时间
  • 【Java04】引用变量数组初始化的内存机制
  • 基于JSP的足球赛会管理系统
  • 博客摘录「 AXI三种接口及DMA DDR XDMA介绍(应用于vivado中的ip调用)」2024年6月10日
  • Bigtable: A Distributed Storage System for Structured Data
  • RAG下的prompt编写探索
  • 【计算机组成原理】指令系统考研真题详解之拓展操作码!
  • 北航第六次数据结构与程序设计作业(查找与排序)选填题
  • Optional详解和常用API
  • Unity 3D 物体的Inspector面板
  • 闪烁与常亮的符号状态判断机制(状态机算法)
  • Hyper-V如何将文件复制到虚拟机?教您3个简单的方法!
  • Vue主要使用-03