- // test.cpp :
- //
- #include "stdafx.h"
- #include "iostream"
- #include "ctime"
- #include "stdexcept"
- #include "windows.h"
- using namespace std;
- inline double random(double start, double end) {
- return start + (end - start) * rand() / (RAND_MAX + 1.0);
- }
- inline void bsort(int* list,int maxIndex){
- bool hasChange = false;
- for (int i = 0,l = maxIndex;i <= l;i++) {
- hasChange = false;
- for (int j = 0;j < maxIndex; j++) {
- if (*(list+j) > *(list+j+1)) {
- hasChange = true;
- *(list+j) = *(list+j) ^ *(list+j+1);
- *(list+j+1) = *(list+j) ^ *(list+j+1);
- *(list+j) = *(list+j) ^ *(list+j+1);
- }
- }
- if (!hasChange) {
- break;
- }
- maxIndex--;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int minListLen = 50,maxListLen = 120,maxValueInList = 1000;
- int* list;
- srand(unsigned(time(0)));
- int i = 0;
- while (1) {
- int listLen = int(random(minListLen,maxListLen));
- list = new int[listLen];
- cout<<":";
- for (int j = 0; j < listLen; j++) {
- *(list+j) = int(random(1, maxValueInList));
- cout<<*(list+j)<<",";
- }
- cout<<endl;
- bsort(list,listLen-1);
- cout<<":";
- for (int j = 0; j < listLen; j++) {
- if (j>0 && *(list+j-1)>*(list+j)) {
- cout<<""<<endl;
- }
- cout<<*(list+j)<<",";
- }
- cout<<endl;
- delete[] list;
- cout<<"times:"<<i<<endl;
- i++;
- Sleep(1000);
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/070120148456.html
来源: http://www.codesnippet.cn/detail/070120148456.html