数据结构基础代码实现(C++版)(一)
作者:互联网
基础算法
目录
交换
/*
* author:起风了_Zoe
* date:2020.03.30
*/
#include <iostream>
#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t)) // 宏函数
using namespace std;
void swap_value(int x, int y); // 值传递
void swap_pointer(int *px, int *py); // 指针传递
void swap_quote(int &x, int &y); // 传引用
int main()
{
int a,b;
a = 1; b = 10;
cout <<"a = "<< a <<";b = "<< b << endl;
// 使用swap_value函数值传递
swap_value(a,b);
cout <<"值传递的结果:";
cout <<"a = "<< a <<";b = "<< b << endl;
a = 1; b = 10;
// 使用swap_pointer函数指针传递
swap_pointer(&a,&b); // 这里必须要进行取址
cout <<"指针传递的结果:";
cout <<"a = "<< a <<";b = "<< b << endl;
a = 1; b = 10;
// 使用宏
int temp_Macro;
SWAP(a,b,temp_Macro);
cout <<"使用宏函数的结果:";
cout <<"a = "<< a <<";b = "<< b << endl;
a = 1; b = 10;
// 传引用
swap_quote(a,b);
cout <<"使用传引用的结果:";
cout <<"a = "<< a <<";b = "<< b << endl;
a = 1; b = 10;
// 使用std::swap函数
std::swap(a,b);
cout <<"使用std::swap函数的结果:";
cout <<"a = "<< a <<";b = "<< b << endl;
return 0;
}
void swap_value(int x, int y)
{
int temp;
temp = x;
y = temp;
x = y;
}
void swap_pointer(int *px, int *py) //指针用来保存地址
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
void swap_quote(int &rx, int &ry)
{
int temp;
temp = rx;
rx = ry;
ry = temp;
}
冒泡排序
/*
* author:起风了_Zoe
* date:2020.03.30
*/
// 冒泡排序:n个元素,从左至右,相邻两个比较,大的放右边,第一遍下来,
// 最大的在最右,再对n-1个数据进行操作
#include <iostream>
using namespace std;
void BubbleSort(int list[], int n);
int main()
{
int a[] = {2, 4, 6, 8, 0, 1, 3, 5, 7, 9};
BubbleSort(a, 10);
for (int i = 0; i < 10; i++)
{
cout << a[i] <<" ";
}
return 0;
}
void BubbleSort(int list[], int n)
{
for(int i = 0; i < n-1; i++) // 扫描次数为数据个数减1
{
for (int j = 0; j < n-i-1; j++) // 所需要扫描的数据位置
{
if (list[j] > list[j+1])
{
swap(list[j],list[j+1]);
}
}
}
}
选择排序
/*
* author:起风了_Zoe
* date:2020.03.30
*/
// 选择排序:选择排序选择最小的放左边
#include <iostream>
using namespace std;
void SelectSort(int *list, const int n);
int main()
{
int a[] = {2, 4, 6, 8, 0, 1, 3, 5, 7, 9};
SelectSort(a, 10);
for (int i = 0; i < 10; i++)
{
cout << a[i] <<" ";
}
return 0;
}
void SelectSort(int *list, const int n)
{
for (int i = 0; i < n-1; i++) // 扫描n-1次即可
{
int min = i; // min是毛巾,即下标
for (int j = i+1; j < n; j++) // 扫描是从i+1开始的
{
if (list[j] < list[min])
{
min = j; // 移动毛巾
}
}
swap(list[i], list[min]);
}
}
顺序查找
/*
* author:起风了_Zoe
* date:2020.03.30
*/
// 顺序查找:对没有排序的数据
#include <iostream>
using namespace std;
int SequentialSearch(int *a, const int n, const int x);
int main()
{
int a[] = {2, 4, 6, 8, 0, 1, 3, 5, 7, 9};
int index, num = 1;
index = SequentialSearch(a, 10, num);
if(index < 0)
cout << "没有找到。" <<endl;
else
cout << "在a[" << index << "]里找到" << num << endl;
return 0;
}
int SequentialSearch(int *a, const int n, const int x)
{
int i;
for(i = 0; i < n; i++) // 遍历数组
{
if(a[i] == x)
return i;
}
if (i == n) // 没找到最后i会加1
return -1;
}
递归的折半查找
/*
* author:起风了_Zoe
* date:2020.03.30
*/
// 递归的折半查找
#include <iostream>
using namespace std;
int BinarySearch(int *a, const int x, const int left, const int right);
int main()
{
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int num = 7, index;
index = BinarySearch(a, num, 0, 10);
if(index < 0)
cout << "没有找到" << endl;
else
cout << "在a[" << index << "]中找到" << num;
return 0;
}
int BinarySearch(int *a, const int x, const int left, const int right)
{
if (left <= right)
{
int middle = (left + right)/2;
if(x < a[middle])
return BinarySearch(a, x, left, middle-1);
else if(x > a[middle])
return BinarySearch(a, x, middle+1, right);
else
return middle;
}
return -1;
}
递归排列组合
/*
* author:起风了_Zoe
* date:2020.03.30
*/
// 递归的排列组合
#include <iostream>
using namespace std;
// int num = 0;
void Permutations(char *p, const int k, const int m)
{
// cout << ++num <<endl;
if(k == m) // 停止递归,开始输出,p是一个整体
{
for (int i = 0; i <= m; i++)
cout << p[i];
cout << endl;
}
else
{
for (int i = k; i <= m; i++)
{
swap(p[k], p[i]);
Permutations(p, k+1, m);
swap(p[k], p[i]);
}
}
}
int main()
{
char w[] = "abc";
Permutations(w, 0, 2);
return 0;
}
标签:10,const,Zoe,int,代码,namespace,C++,数据结构,cout 来源: https://www.cnblogs.com/Wind-Flies/p/12601996.html