zoukankan      html  css  js  c++  java
  • 简单冒泡排序(C++模版技术实现)

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

    //
    // C++模版技术实现冒泡排序. 
    // 
    
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    
    template <typename T>
    void bubbleSort(T *array, size_t count)
    {
    	size_t i, j;
    	bool isSwap; 
    	T temp;
    	
    	//
    	// count 个泡泡共需比较 count - 1 趟.
    	// 每趟从最底层 array[count - 1] 开始比较.
    	// 每趟上层泡泡增加 1 个,即每趟比较上界下降一层. 
    	//
    	for (i = 0, isSwap = true; i < count - 1 && true == isSwap; ++i) 
    	{
    		isSwap = false;
    		for (j = count - 1; j > i; --j) 
    		{
    			if (array[j] < array[j - 1]) 
    			{
    				temp = array[j];
    				array[j] = array[j - 1];
    				array[j - 1] = temp;
    				
    				isSwap = true;
    			}
    		}
    	} 
    }
    
    //
    // 测试 
    //
    int main(void)
    {
    	char szTest[] = "Bubble 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]);
    	
    	bubbleSort(szTest, strlen(szTest));
    	bubbleSort(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; 
    } 
    
  • 相关阅读:
    Key and Certificate Conversion
    openssl
    python http通信实现
    鼠标右键添加cmd
    好文章
    wireshark里无网络接口解决办法
    python垃圾回收
    终于有人把 Docker 讲清楚了
    mongodb的监控与性能优化
    mongodb创建超级用户和普通用户(对应数据库的用户)
  • 原文地址:https://www.cnblogs.com/wxxweb/p/2060243.html
Copyright © 2011-2022 走看看