排序算法:

     基本:冒泡,快速,选择,堆,插入,shell
     多路并归:

1.冒泡排序:

     思想:交换排序,通过相邻的交换来达到排序的目的。
     流程:
          1.对数组中的各数据,依次比较相邻两个元素的大小。
          2.如果前面的大,交换。经过一轮,可把最小的排好。
          3.然后用同样的方法,把剩下的数据排好。最后从小到大排好相应的数据。
#include <iostream>
#include <time.h>
#define SIZE 10
using namespace std;
void BubbleSort(int *a,int len)
{
     int temp;
     for(int i=0;i<len-1;i++)
     {
          for(int j=len-1;j>i;j--)
          {
               if(a[j-1]>a[j])
               {
                    temp=a[j];
                    a[j]=a[j-1];
                    a[j-1]=temp;    
               }         
          }
          cout<<i<<":";
          for(int k=0;k<len;k++)
          {
               cout<<a[k]<<" ";
          }
          cout<<endl;    
     }
}
int main()
{
     int shuzu[SIZE];
     srand(time(NULL));//随机种子
     for(int i=0;i<SIZE;i++)
     {
          shuzu[i]=rand()/1000+100;    
     }
     cout<<"old:";
     for(int i=0;i<SIZE;i++)
     {
          cout<<shuzu[i]<<"  ";
     }
     cout<<endl;
     BubbleSort(shuzu,SIZE);
     cout<<"now:";
     for(int i=0;i<SIZE;i++)
     {
          cout<<shuzu[i]<<"  ";
     }
     cout<<endl;
    
}
 
选择排序:
     
     思路:在每一步选取最小的来重新排列。
     流程:
          1.首先从原始数据中选中最小的一个,和位于第一个位置的数据交换、
          2.接着从剩下的n-1数据中选择次小的,与第二个位子交换。
          3.这样重复,数组从小到大排列。
#include <iostream>
#include <time.h>
using namespace std;
#define SIZE 10
void SelectionSort(int *a,int len)
{
     int temp,k;
     for(int i=0;i<len-1;i++)
     {
          k=i;
          for(int j=i+1;j<len-1;j++)
          {
               if(a[j]<a[k])
                    k=j;
          }
          if(k!=i)//交换
          {
               temp=a[k];
               a[k]=a[i];
               a[i]=temp;
          }    
     }    
}
int main()
{
      int shuzu[SIZE];
      srand(time(NULL));
      for(int i=0;i<SIZE;i++)
      {
           shuzu[i]=rand()/1000+100;
      }
      cout<<"old:";
       for(int i=0;i<SIZE;i++)
      {
               cout<<shuzu[i]<<" ";
      }
      cout<<endl;
      SelectionSort(shuzu,SIZE);
      cout<<"now:";
      for(int i=0;i<SIZE;i++)
      {
           cout<<shuzu[i]<<" ";
      }
      cout<<endl;
}
 
插入排序:
     思路:通过未排序的数据逐个插入合适的位置来完成工作。
     流程:
          1.首先对数组的前两个数据进行从小到大排序
          2.接着第3个数据插入合适的位子
          3.第4个
          4.重复
 
#include <iostream>
#include <time.h>
#define SIZE 10
using namespace std;
void InsertionSort(int *a,int len)
{
     int temp,j,i,k;
     for(i=1;i<len;i++)
     {
          //temp=a[i],a[j+1]=a[j];a[j+1]=temp;就是个交换。 判断 j-- 为逻辑
          temp=a[i],
          j=i-1;
          while(j>=0 && temp<a[j])
          {
               a[j+1]=a[j];
               j--;
          }
          a[j+1]=temp;
     }
}
int main()
{
       int arr[SIZE];
        srand(time(NULL));
        for(int i=0;i<SIZE;i++)
        {
             arr[i]=rand()/1000+100;
          }
          cout<<"old:";
          for(int j=0;j<SIZE;j++)
        {
             cout<<arr[j]<<"  ";
          }
          cout<<endl;
          InsertionSort(arr,SIZE);
          cout<<"now:";
          for(int k=0;k<SIZE;k++)
        {
             cout<<arr[k]<<"  ";
          }
          cout<<endl;
}
 
    Shell排序:(希尔排序,缩小增量排序)
     流程
          1.将有n个元素的数组,分成n/2个数字序列。第一个数据和第n/2+1成一对、。。。
          2.一次循环是每个序列对排好顺序
          3。然后,在变为n/4
          4.不断重复。
#include <iostream>
#include <time.h>
using namespace std;
#define SIZE 10

void ShellSrot(int *a,int len)
{
     int i,j;
     int r,temp;
     for(r=len/2;r>=1;r/=2)
     {
          for(i=r;i<len;i++)
          {
               temp=a[i];
               j=i-r;
               while(j>=0&&temp<a[j])
               {
                    a[j+r]=a[j];
                    j-=r;
               }
               a[j+r]=temp;    
          }    
     }
}
int main()
{
        int arr[SIZE];
        srand(time(NULL));
        for(int i=0;i<SIZE;i++)
        {
             arr[i]=rand()/1000+100;
          }
          cout<<"old:";
          for(int j=0;j<SIZE;j++)
        {
             cout<<arr[j]<<"  ";
          }
          cout<<endl;
          ShellSrot(arr,SIZE);
          cout<<"now:";
          for(int k=0;k<SIZE;k++)
        {
             cout<<arr[k]<<"  ";
          }
          cout<<endl;
}

ACM学习<3>的更多相关文章

  1. ACM学习-POJ-1143-Number Game

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1143-Number Game Number Game Time Limit: 1000MS   Memory ...

  2. ACM学习-POJ-1125-Stockbroker Grapevine

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1125-Stockbroker Grapevine Stockbroker Grapevine Time Li ...

  3. ACM学习-POJ-1003-Hangover

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1003-Hangover Hangover Time Limit: 1000MS   Memory Limit ...

  4. ACM学习-POJ-1004-Financial Management

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1003-Financial Management Financial Management Time Limi ...

  5. acm学习指引

    acm学习心得及书籍推荐   一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划练练: 第 ...

  6. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  7. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  8. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  9. ACM学习历程—HDU5521 Meeting(图论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...

  10. ACM学习网站、

    转载:http://www.cnblogs.com/zhourongqing/archive/2012/05/24/2516180.html http://61.187.179.132/JudgeOn ...

随机推荐

  1. thinkphp 视图(一)

    视图 View <?php namespace app\index\controller; class Index{ public function index(){ return view() ...

  2. 可视化-echarts流向图制作

    案例: http://www.internetke.com/jsEffects/2018040406/ 前段时间用echarts做了流程图,在此记录下制作步骤. 一.Echarts是什么 Echart ...

  3. 解决安装xcode后git使用报错的问题

    一.现象: htmlxdeMacBook-Pro:demo htmlx$ git status Agreeing to the Xcode/iOS license requires admin pri ...

  4. CentOS_mini下make安装

    执行make时显示: make: *** No targets specified and no makefile found. Stop. 用网上的教程: wget http://ftp.gnu.o ...

  5. js常用判断和语法

    1.js获取选中的redio元素 var version = $('.version input[name="input1"]:checked').val();//单选框默认选中& ...

  6. MyAdvice 填充方法(在原有方法上添加方法)

    //applicationContext.xml配置文件  /UserServiceImp继承于UserService接口 <!-- 1 配置目标对象-->    <bean nam ...

  7. [uboot] (第一章)uboot流程——概述

    http://blog.csdn.net/ooonebook/article/details/52939100 [uboot] uboot流程系列: [project X] tiny210(s5pv2 ...

  8. BeanUtil拷贝

    拷贝vo对象 一些查询到的数据很多是不需要的,可以创建vo对象,对需要的对象属性进行拷贝 maven依赖 <dependency> <groupId>org.projectlo ...

  9. PowerShell 命令行调试指引(转)

    How to manage a debugging session Before you start debugging, you must set one or more breakpoints. ...

  10. Linux 根据PID找到相应应用程序的运行目录

    1.找到运行程序的PID # ps aux | grep redis root pts/ S+ : : grep redis root ? Ssl Aug30 : redis-server *: # ...