zoukankan      html  css  js  c++  java
  • 冒泡排序、插入排序、快速排序

    一、冒泡排序

    #include<iostream>

    using namespace std;

    void bubble_sort(int a[],int len)

    {

           int i,j,temp;

           int exchange=0;

           for(i=0;i<len;i++)

           {

                  exchange=0;

                  for(j=1;j<=len-i-1;j++)

                  if(a[j-1]<a[j])

                  {

                         temp=a[j-1];

                         a[j-1]=a[j];

                         a[j]=temp;

                         exchange=1;

                  }

                  if(exchange!=1)

                         return;

           }

    }

    int main()

    {

           int a[]={7,3,5,8,9,1,2,4,6};

           bubble_sort(a,9);

           for(int i=0;i<9;i++)

                  cout<<a[i]<<" " ;

           return 0;

    }

     二、插入排序

    #include<iostream>

    using namespace std;

    void insert_sort(int a[],int n)

    {

           int i=0,j=0,temp=0;

           for(i=1;i<n;i++)

           {

                  temp=a[i];

                  for(j=i-1;temp<a[j]&&j>=0;j--)

                         a[j+1]=a[j];

                  a[j+1]=temp;

           }

    }

    int main()

    {

           int a[]={7,3,5,8,9};

           insert_sort(a,5);

           for(int i=0;i<5;i++)

                  cout<<a[i]<<" " ;

           return 0;

    }

    三、快速排序

    #include<iostream>

    using namespace std;

    void quick_sort(int a[],int low,int high)

    {

           int i,j,pivot;

           if(low<high)

           {

                  pivot=a[low];

                  i=low;

                  j=high;

                  while(i<j)

                  {

                         while(i<j && a[j]>=pivot)

                                j--;

                         if(i<j)

                                a[i++]=a[j];

                         while(i<j && a[i]<=pivot)

                                i++;

                         if(i<j)

                                a[j--]=a[i];

                  }

                  a[i]=pivot;

                  quick_sort(a,low,i-1);

                  quick_sort(a,i+1,high);

           }

    }

    int main()

    {

           int data[9]={54,38,96,23,15,72,60,45,83};

           quick_sort(data,0,8);

           for(int i=0;i<9;i++)

                  cout<<data[i]<<" " ;

           return 0;

    }

  • 相关阅读:
    Java Message Service学习(一)
    二叉树的层序遍历算法实现
    二叉树的操作之统计二叉树中节点的个数
    java.uti.Random类nextInt方法中随机数种子为47的奇怪问题
    最大子序列和问题
    参数对象Struts2中Action的属性接收参数
    方法字段[C# 基础知识系列]专题二:委托的本质论
    struts2属性Struts2中属性接收参数中文问题和简单数据验证
    权限检查联系人ProfileProvider
    最小较小codeforces 2B The least round way
  • 原文地址:https://www.cnblogs.com/fuyanan/p/3354246.html
Copyright © 2011-2022 走看看