zoukankan      html  css  js  c++  java
  • 数据结构基础代码实现(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;
    }
    
  • 相关阅读:
    Android在代码中获取应用签名
    Android之密码的显示与隐藏
    如何把本地项目上传到Github
    Android之移动热修复
    Android之apk优化
    Android之内存泄漏
    Java类加载顺序
    mysql系列-⼀条SQL查询语句是如何执⾏的?
    java操作redis(jedis)常用方法示例
    mybatis拦截器实现通用权限字段添加
  • 原文地址:https://www.cnblogs.com/Wind-Flies/p/12601996.html
Copyright © 2011-2022 走看看