zoukankan      html  css  js  c++  java
  • 简单排序

     1 #include <bits/stdc++.h>
     2 //#include <time.h>
     3 //#include <stdlib.h>
     4 using namespace std;
     5 
     6 template<typename var>
     7 void bubbleSort(var array[],int len)
     8 {    //简单冒泡排序 
     9     for(int i=1;i<len;i++)
    10         for(int j=0;j<len-i;j++)
    11             if(array[j]>array[j+1])
    12                 swap(array[j],array[j+1]);
    13 }
    14 template<typename var>
    15 void selectSort(var array[],int len)
    16 {    //简单选择排序
    17     for(int i=0;i<len-1;i++)
    18         for(int j=i+1;j<len;j++)
    19              if(array[j]<array[i])
    20                  swap(array[i],array[j]);
    21 }
    22 template<typename var>
    23 void insertSort(var array[],int len)
    24 {    //简单插入排序
    25     for(int i=1;i<len;i++)
    26     {
    27         for(int j=i;j>0&&array[j]<array[j-1];j--)
    28             swap(array[j],array[j-1]);
    29     }    
    30 }
    31 int main()
    32 {
    33     double array[1005];
    34     srand(time(0));
    35     cout<<"生成100个0~1000随机数
    ";
    36     for(int i=0;i<1000;i++)
    37     {
    38         array[i]=10000*((double)rand()/RAND_MAX);
    39         cout<<setw(10)<<array[i];
    40     }
    41     clock_t ts=clock();
    42     insertSort(array,1000);
    43     clock_t te=clock();
    44     cout<<"
    排序后的1000个数
    ";
    45     cout<<"花费时间:"<<(1000.0*(te-ts)/CLOCKS_PER_SEC)<<"ms"<<endl; 
    46     for(int i=0;i<1000;i++)
    47         cout<<setw(10)<<array[i];
    48     return 0;
    49 }
  • 相关阅读:
    A.02.01—功能定义—一般定义
    A.02.00—功能定义与唤醒—起始
    A.01.12—模块的输出—通讯(CAN&LIN)
    A.01.11—模块的输出—输出复用和可配
    A.01.10—模块的输出—PWM高端输出
    A.01.09—模块的输出—PWM低端输出
    redis命令
    memcached命令
    kafka命令
    nginx命令
  • 原文地址:https://www.cnblogs.com/Fresh--air/p/7698892.html
Copyright © 2011-2022 走看看