c ++ data structures and algorithms (2) - Bubble Sort

#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;

/*冒泡排序*/
void BubbleSort(int array[], int n); 

int main(){
	clock_t start, end;
	int array[100000];
	int n = 100000;
	cout << "共" << n << "个数据" << endl;
	for(int i = 0; i < n; i++){
		array[i] = rand(); 
	}
	/*
	cout << "排序前:" << endl;
	for(int i = 0; i < n; i++){
		cout << array[i] << endl;
	}
	*/
	start = clock();
	BubbleSort(array, n);
	end = clock();
	/*
	cout << "排序后:" << endl;
	for(int i = 0; i < n; i++){
		cout << array[i] << endl;
	} 
	*/
	cout << "用时:" << (end - start) << "ms" << endl; 
	return 0;
}
void BubbleSort(int array[], int n){
	for(int i = 0; i < n-1; i++){
		for(int j = 0; j < n - i -1; j++){
			if(array[j] > array[j+1]){
				std::swap(array[j], array[j+1]);
			}
		}
	}
	
}

Speed ​​super slow

Guess you like

Origin blog.csdn.net/qq_40238526/article/details/92402631