Advertisement

冒泡排序是C++实现的交换排序

阅读量:
复制代码
    #include<stdio.h>
    #include<stdlib.h>
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<limits>
    #include<algorithm>
    #include<math.h>
    #pragma warning(disable:4996)
    using namespace std;
    
    //从前往后两两比较相邻元素的值,若为逆序,则交换它们,直到序列比较完,
    //称这样过程为“一趟”冒泡排序,最多只需n-1趟排序。
    //每一趟排序都可以使一个元素移动到最终位置,已经确定最终位置的元素在之后的处理中无需再对比,
    //如果某一趟排序过程中未发生“交换”,则算法可提前结束。
    
    //冒泡排序
    void swap(int& a, int& b)//传引用。C++提供了swap函数,可不写
    {
    	int temp = a;
    	a = b;
    	b = temp;
    }
    void BubbleSort(int A[], int n)
    {
    	for 

全部评论 (0)

还没有任何评论哟~