• 排序算法(一)——冒泡排序


        刚开始自己的博客,就从最基本的排序算法开始吧,最近自己也在复习算法相关的内容。

        这里先讲讲时间复杂度为O(n2)的几种排序算法吧——冒泡排序,选择排序,插入排序;以上排序皆以升序为例,数组为list[begin..........end]。

        冒泡排序:

      名字很形象,就像烧开水冒泡一样,小的在底下,大的在上面; 算法原理就是重头到尾,相邻的两个元素进行比较,如果前面的元素大于后面的元素(即list[i]>list[i+1]),那就进行交换,一直遍历到最后,那么一次遍历完了之后保证最后一个元素是最大的,接下来在遍历begin到end-1的数组,以同样的方式,完成冒泡交换。

        上代码:

      #include <iostream>
      using namespace std;

      void swap(int &a,int &b)
      {
        int temp=a;
        a=b;
        b=temp;
        return;
      }
      void Bubble_sort(int list[],int begin,int end)
      {
        for(int i=end;i>begin;i--)
        {
          for(int j=0;j<i;j++)
            if(list[j]>list[j+1])
              swap(list[j],list[j+1]);

        }
        return;
      }
      int main()
      {
        int list[10]={1,3,34,536,23,65,3,56,78,55};
        Bubble_sort(list,0,9);
        for(int i=0;i<10;i++)
          cout <<list[i]<<" ";
        cout<<endl;
        system("pause");
      }

  • 相关阅读:
    epoll
    Neighbor Discovery Protocol Address Resolution Protocol
    text files and binary files
    cron_action
    Automation Scripts
    Toeplitz matrix
    Stolz–Cesàro theorem
    stochastic matrix
    HTTP headers with the Link header field HTTP协议支持分页(Pagination)操作,在Header中使用 Link 即可
    Markov Process
  • 原文地址:https://www.cnblogs.com/hit-joseph/p/5071602.html
Copyright © 2020-2023  润新知