zoukankan      html  css  js  c++  java
  • 起泡法排序

    起泡法的思路是:将相邻的两个数比较,将小的调到前头。

    可以推知,如果有 n 个数,则要进行 (n-1) 轮比较和交换。在第一轮要进行 (n-1) 次两两比较,在 j 轮中要进行 (n-j) 次两两比较。

    下面将10个数从小到大进行排序:

    #include<iostream>
    using namespace std;
    int main() {
    	int a[11];
    	int i, j, t;
    	cout<<"input 10 numbers:\n";
    	for (i=1; i<11; i++) {
    		cin>>a[i];
    	}
    	cout<<endl;
    	for (j=1; j<=9; j++) {
    		for(i=1; i<=10-j; i++) {
    			if (a[i] > a[i+1]) {
    				t = a[i];
    				a[i] = a[i+1];
    				a[i+1] = t;
    			}
    		}
    	}
    	cout<<"the sorted numbers: \n";
    	for (i=1; i<11; i++) {
    		cout<<a[i]<<endl;
    	}
    	cout<<endl;
    	return 0;
    }
    
  • 相关阅读:
    IfcAxis2Placement3D
    IfcAxis2Placement2D
    IfcAxis1Placement
    realsense 深度数据
    realsense 深度数据
    realsense 深度数据
    sudo fdisk -l
    temviewer历史版本
    100/9801
    IfcPlacement
  • 原文地址:https://www.cnblogs.com/humingx/p/4128311.html
Copyright © 2011-2022 走看看