zoukankan      html  css  js  c++  java
  • 冒泡排序

    介绍

    冒泡排序是最典型的排序算法了。这里就是为了留一个纪念。正在学习算法。

    实现

    #include <iostream>
    using namespace std;
    
    /************************************************************************
    	@ 冒泡排序
    	@ nums -- 数组
    	@ length -- 数组长度
    ************************************************************************/
    void pop_sort(int* nums,int length)
    {
    	int temp=0;
    	for(int hidx=0;hidx<length-1;hidx++)  //处理最大
    	{
    		for(int lidx=hidx+1;lidx<length;lidx++) //最大和其余比較
    		{
    			if (nums[lidx] > nums[hidx]) //交换
    			{
    				temp=nums[hidx];
    				nums[hidx]=nums[lidx];
    				nums[lidx]=temp;
    			}
    		}
    	}
    }
    
    /************************************************************************
    	@ test
    ************************************************************************/
    int main()
    {
    	int a[10]={10,20,30,40,15,12,13,14,84,21};
    
    	pop_sort(a,10);
    
    	for (int idx=0;idx<10;idx++)
    	{
    		cout<<a[idx]<<"  ";
    	}
    
    	return 0;
    }

    结果


  • 相关阅读:
    几种常用类的学习
    类,接口
    方法
    数组使用
    条件控制与循环
    类型转换,运算符
    Java基本类型
    SVN基本使用
    【转】MySQL的btree索引和hash索引的区别
    常用命令(java、linux)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4554995.html
Copyright © 2011-2022 走看看