zoukankan      html  css  js  c++  java
  • 手写快排

    快排思想(从小到大排):

       根据冒泡排序,时间复杂度实在过高,我们不妨这么想,比如随机说一组数:

       5 8 9 6 3 4;

       我们从一(序号)开始,如若a[i]>a[j],则交换二者的值,没有的话则跳过,具体过程如下:

      ①3 8 9 6 5 4
      ②3 6 9 8 5 4
      ③3 5 9 8 6 4
      ④3 4 9 8 6 5
      ⑤3 4 8 9 6 5
      ⑥3 4 6 9 8 5
      ⑦3 4 5 9 8 6
      ⑧3 4 5 8 9 6
      ⑨3 4 5 6 9 8
      ⑩3 4 5 6 8 9

      代码如下:

    #include<bits/stdc++.h>
    using namespace std;
    int n;
    int ans;
    int a[10200];
    int main()
    {
      cin>>n;
      for(int i=1;i<=n;i++)
      cin>>a[i];
      for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
          if(a[i]>a[j]) swap(a[i],a[j]),ans++;else continue;
          for(int i=1;i<=n;i++) cout<<a[i]<<' ';//每次交换后输出
          cout<<endl; //
        }
       for(int i=1;i<=n;i++)//
       cout<<a[i]<<' ';//
      cout<<ans;//输出次数,和a[i];
      return 0;
    }

    ❀完结撒花❀

  • 相关阅读:
    powershell和cmd区别
    装饰器笔记
    url参数和字典的相互转化
    Python装饰器详解
    python字符串格式化f-string
    Python函数(function)与方法(method)区别
    jenkins钉钉插件报错keywords not in content
    jenkins配置邮件
    vim常用操作
    Vue之axios请求
  • 原文地址:https://www.cnblogs.com/fuhuayongyuandeshen/p/14278729.html
Copyright © 2011-2022 走看看