2021-11-07

算法–冒泡排序

冒泡排序C++

算法描述

算法描述

//冒泡排序 
#include<iostream>
using namespace std;
void bubbleSort(int f[],int n){
    
    
	int temp;
for(int i=0;i<n-1;i++){
    
    
	for(int j=0;j<n-i-1;j++){
    
    
		if(f[j]<f[j+1]){
    
    
			temp=f[j];
			f[j]=f[j+1];
			f[j+1]=temp;
		}
	}
}	
}
int main(){
    
    
	int n;
	cin>>n;
	int f[n];
	for(int j=0;j<n;j++){
    
    
		cin>>f[j];
	}
	bubbleSort(f,n);
	for(int i=0;i<n;i++)
	cout<<f[i]<<endl;
	
} 

猜你喜欢

转载自blog.csdn.net/weixin_44423938/article/details/121189724