• 数据结构-bubble sort


    #gcc version 4.5.3 (GCC)
    
    #include <iostream>
    #include <algorithm>
    
    template <typename ARRAY_TYPE, typename INDEX_TYPE>
    void bubble_sort(ARRAY_TYPE array[], INDEX_TYPE size)
    {
    	bool done = false;
    	
    	while(!done)
    	{
    		done = true;
    		for(INDEX_TYPE i = 0; i < size - 1; i++)
    		{
    			if(array[i] > array[i+1])
    			{
    				done = false;
    				std::swap(array[i], array[i+1]);
    			}
    		}
    		size--;
    	}
    }
    
    template <typename TYPE>
    void print(TYPE val)
    {
    	std::cout << val << " ";
    }
    
    int main()
    {
    	int array[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    	bubble_sort(array, 10);
    	std::for_each(&array[0], &array[10], print<int>);
    	std::cout << std::endl;
    	
    	int data[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    	std::sort(data, data+10);
    	std::for_each(data, data+10, print<int>);
    	std::cout << std::endl;
    }
    

      

  • 相关阅读:
    Day6
    Day6
    Day6
    Day6
    Day6
    Day6
    LeetCode "The Skyline Problem"
    HackerRank
    HackerRank
    HackerRank
  • 原文地址:https://www.cnblogs.com/dracohan/p/3144482.html
Copyright © 2020-2023  润新知