• 直接选择排序(C++模版技术实现)


    下面代码仅供本人复习数据结构所用,实用性N低,各位飘过吧~~哈哈:>

    //
    // C++ 模版技术实现直接选择排序. 
    // 
    
    #include <cstdlib>
    #include <cstring> 
    #include <iostream>
    
    //
    // 首先在未排序序列中找到最小元素,存放到排序序列的起始位置,
    // 然后,再从剩余未排序元素中继续寻找最小元素,
    // 然后放到排序序列末尾. 以此类推,直到所有元素均排序完毕.
    //
    template <typename T>
    void selectionSort(T *array, const size_t count)
    {
    	T temp;
    	
    	for (size_t i = 0, j, mini; i < count; ++i)
    	{
    		mini = i;
    		for (j = i + 1; j < count; ++j) {
    			if (array[j] < array[mini]) {
    				mini = j;
    			}
    		}
    		if (mini != i)
    		{
    			temp = array[i];
    			array[i] = array[mini];
    			array[mini] = temp;
    		}
    	}
    }
    
    //
    // 测试 
    //
    int main(void)
    {
    	char szTest[] = "Selection sort algorithm test case !"; 
    	int iarrTest[] = {23, 12, 2, 123, 72, 35, 49, 51, 83, 94, 65}; 
    	const size_t INT_ARR_SIZE = sizeof(iarrTest) / sizeof(iarrTest[0]);
    	
    	selectionSort(szTest, strlen(szTest));
    	selectionSort(iarrTest, INT_ARR_SIZE);
    	
    	std::cout << szTest << std::endl;
    	
    	for (size_t i = 0; i < INT_ARR_SIZE; ++i)
    	{
    		std::cout << iarrTest[i] << " "; 
    	}
    	std::cout << std::endl;
    	
    	return EXIT_SUCCESS; 
    } 
    
  • 相关阅读:
    python之路---类
    python之路---走台阶(递归)
    python之路---递归函数
    python之路---filter、map、lambda函数
    python之路---封装
    python07--抽象数据类型和python类(P34)
    python06--计算机内存结构与存储管理(P27)
    匹配算法大纲
    并查集及其优化
    Hash技术初涉
  • 原文地址:https://www.cnblogs.com/wxxweb/p/2060379.html
Copyright © 2020-2023  润新知