正文之前
最近在实验室除了看论文貌似没啥事情, 但是老板论文都不给我推荐, 所以我干脆自己再来找点东西看好了.. 刚好前几天在珞樱网上找到了一份慕课的收费视频的录制版.. 下下来好好看一下好了~
正文
- //########### Compare.cpp ###########
- #include <iostream>
- #include "Student.h"
- #include "SortTestHelper.h"
- #include <algorithm>
- #include <time.h>
- using namespace std;
- template<typename T>
- void insertSort(T arr[], int n){
- for (int i = 1; i <n; ++i)
- {
- T e = arr[i];
- int j=i;
- for (; j> 0; --j)
- {
- if (e <arr[j-1])
- arr[j] = arr[j-1];
- else
- break;
- }
- arr[j] = e;
- }
- }
- template<typename T>
- void selectSort(T arr[], int n){
- for(int i = 0 ; i <n ; i ++){
- int minIndex = i;
- for( int j = i + 1 ; j < n ; j ++ )
- if( arr[j] < arr[minIndex] )
- minIndex = j;
- swap( arr[i] , arr[minIndex] );
- }
- }
- template<typename T>
- void bubbleSort(T arr[], int n){
- for (int i = n-1; i> 0; --i)
- {
- for (int j = 0; j <i; ++j)
- {
- if (arr[j]> arr[j+1])
- swap(arr[j], arr[j+1]);
- }
- }
- }
- int main() {
- clock_t start,end;
- int n = 10000;
- // 测试模板函数, 传入整型数组
- int *a = SortTestHelper::generateRandomArray(n,0,n);
- start = clock();
- insertSort( a , n );
- end = clock();
- cout<<"insertSort:"<<double(end- start)<<endl;
- for (int i = 0; i <50; ++i)
- {
- cout<<a[i]<<" ";
- }
- cout<<endl;
- cout<<endl;
- delete[] a;
- int *b = SortTestHelper::generateRandomArray(n,0,n);
- start = clock();
- selectSort( b , n );
- end = clock();
- cout<<"selectSort:"<<double(end- start)<<endl;
- for (int i = 0; i < 50; ++i)
- {
- cout<<b[i]<<" ";
- }
- cout<<endl;
- cout<<endl;
- delete[] b;
- int *c = SortTestHelper::generateRandomArray(n,0,n);
- start = clock();
- bubbleSort( c , n );
- end = clock();
- cout<<"bubbleSort:"<<double(end- start)<<endl;
- for (int i = 0; i < 50; ++i)
- {
- cout<<c[i]<<" ";
- }
- cout<<endl;
- cout<<endl;
- delete[] c;
- // 测试模板函数, 传入自定义结构体 Student 数组
- // Student d[4] = { {"D",90} , {"C",100} , {"B",95} , {"A",95} };
- // bubbleSort(d,4);
- // for( int i = 0 ; i < 4 ; i ++ )
- // cout<<d[i];
- // cout<<endl;
- return 0;
- }
emmm 以后要多用模板, 不然的话很难受~ 感觉自己会很低级... 所以还是好好的学习吧..
- //######## SortTestHelper.h #########
- //
- // Created by liuyubobobo on 7/13/16.
- //
- #ifndef INC_04_INSERTION_SORT_SORTTESTHELPER_H
- #define INC_04_INSERTION_SORT_SORTTESTHELPER_H
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <ctime>
- #include <cassert>
- using namespace std;
- namespace SortTestHelper {
- template<typename T>
- T *generateRandomArray(int n, T range_l, T range_r) {
- T *arr = new T[n];
- srand(time(NULL));
- for (int i = 0; i <n; i++)
- arr[i] = rand() % (range_r - range_l + 1) + range_l;
- return arr;
- }
- int *generateNearlyOrderedArray(int n, int swapTimes){
- int *arr = new int[n];
- for(int i = 0 ; i < n ; i ++ )
- arr[i] = i;
- srand(time(NULL));
- for( int i = 0 ; i < swapTimes ; i ++ ){
- int posx = rand()%n;
- int posy = rand()%n;
- swap( arr[posx] , arr[posy] );
- }
- return arr;
- }
- int *copyIntArray(int a[], int n){
- int *arr = new int[n];
- copy(a, a+n, arr);
- return arr;
- }
- template<typename T>
- void printArray(T arr[], int n) {
- for (int i = 0; i <n; i++)
- cout << arr[i] << " ";
- cout << endl;
- return;
- }
- template<typename T>
- bool isSorted(T arr[], int n) {
- for (int i = 0; i <n - 1; i++)
- if (arr[i]> arr[i + 1])
- return false;
- return true;
- }
- template<typename T>
- void testSort(const string &sortName, void (*sort)(T[], int), T arr[], int n) {
- clock_t startTime = clock();
- sort(arr, n);
- clock_t endTime = clock();
- cout << sortName << ":" << double(endTime - startTime) / CLOCKS_PER_SEC << "s"<<endl;
- assert(isSorted(arr, n));
- return;
- }
- };
- #endif //INC_04_INSERTION_SORT_SORTTESTHELPER_H
为什么上面的文件没有高亮?? 卧槽???? 算了, 无所谓了~~
把这两个文件丢在一个文件夹下, 然后运行第一个 Cpp 文件就 OK.
大概来展示下效果..
MMP 为什么冒泡程序这么菜?
正文之后
因为属于复习阶段, 所以就不是很清白的讲了, 我也是过一遍的形式.. 查漏补缺吗... 虽然冒泡这儿出了点小 bug 让我很难受.. 不过理解为上..
这是 Java 的, 我还有差不多多的 python 的..C++ 的倒是比较少... 还有不少机器学习的
来源: http://www.jianshu.com/p/f19a199dcecf