Refer to others ' own summary. I have a limited level and have trouble pointing out mistakes.
Two methods: (Please ignore bubble sort)
First type:
#include <iostream> #include <vector> #include <time.h> #include <sys/timeb.h> #include <
Windows.h> using namespace std;
void Bubble_sort1 (vector<int> &t) {int length = t.size ();
for (int i = 0; i < length-1. i++) {for (int j = 0; J < length-1-I. j) {if (T[j] > t[j + 1])
{int temp = t[j];
T[J] = t[j + 1];
T[j + 1] = temp;
and/or upgraded version, added flag to determine if a cycle has been exchanged without directly ending.
void Bubble_sort2 (vector<int> &t) {int length = t.size ();
for (int i = 0; i < length-1 i++) {BOOL flag = false;
for (int j = 0; J < length-1-I; j + +) {if (T[j] > t[j + 1]) {int temp = t[j];
T[J] = t[j + 1];
T[j + 1] = temp;
Flag = true;
} if (!flag) {break;
int main () {DWORD start1, end1;
Start1 = GetTickCount ();
Srand ((unsigned) time (0));
Vector<int> v;
for (int i = 0; i < 1700 i++) {V.push_back (rand ()); V.push_back (rand ()% 10);//Generate 0 to 9 random number} bubble_sort1 (v);
End1 = GetTickCount ();
cout << "Cost Time (ms):" << end1-start1 << Endl;
return 0; }
The second type:
#include <iostream> #include <vector> #include <time.h> #include <sys/timeb.h> #include <
Windows.h> using namespace std;
void Bubble_sort1 (vector<int> &t) {int length = t.size ();
for (int i = 0; i < length-1. i++) {for (int j = 0; J < length-1-I. j) {if (T[j] > t[j + 1])
{int temp = t[j];
T[J] = t[j + 1];
T[j + 1] = temp;
and/or upgraded version, added flag to determine if a cycle has been exchanged without directly ending.
void Bubble_sort2 (vector<int> &t) {int length = t.size ();
for (int i = 0; i < length-1 i++) {BOOL flag = false;
for (int j = 0; J < length-1-I; j + +) {if (T[j] > t[j + 1]) {int temp = t[j];
T[J] = t[j + 1];
T[j + 1] = temp;
Flag = true;
} if (!flag) {break;
int main () {struct TIMEB start, end;
Ftime (&start);
Srand ((unsigned) time (0));
Vector<int> v;
for (int i = 0; i < 1700 i++) {V.push_back (rand ()); V.push_back (rand ()% 10)//Generate 0 to 9 random number} bubble_sort1 (v);
Ftime (&end);
cout << "Cost Time (ms):" << (end.time-start.time) * 1000 + (END.MILLITM-START.MILLITM) << Endl;
return 0; }