• 常用的排序和查找


    冒泡排序

    void BubbleSort(int *a, int n)
    {
      int i,j,t;
      for(i=0; i<n-1; i++)
      {
        for(j=0; j<n-1-i; j++)
        {
          if(a[j]>a[j+1])
          {
            t = a[j];
            a[j] = a[j+1];
            a[j+1] = t;
          }
        }
      }
    }

    选择排序

    void SelectSort(int *a, int n)
    {
    int i,j,m,t;
    for(i=0; i<n-1; i++)
    {
    m = i;
    for(j=i+1; j<n; j++)
    {
    if(a[m]>a[j])
    {
    m=j;
    }
    }
    t=a[m];
    a[m] = a[i];
    a[i] = t;
    }
    }

    插入排序

    void InsertSort(int *a, int n)
    {
    int in,out,t;
    for(out=1; out<n; out++)
    {
    t = a[out];
    in = out;
    while(in>0 && a[in-1]>=t)
    {
    a[in] = a[in-1];
    in--;
    }
    a[in] = t;
    }
    }

    快速排序

    void QuickSort(int *a, int l, int r)
    {
    int i,j,t;
    if(l <r)
    {
    i=l;
    j=r;
    t = a[i];
    while(i<j)
    {
    while(i<j && a[j]>t) j--;
    if(i<j)
    {
    a[i] = a[j];
    i++;
    }
    while(i<j && a[i]<t) i++;
    if(i<j)
    {
    a[j] = a[i];
    j--;
    }
    }
    a[i] = t;
    QuickSort(a, l, i-1);
    QuickSort(a, i+1, r);
    }
    }

    归并排序

    void Merge(int *initList, int *mergeList, int left, int mid, int right)
    {
    int i,j,k;
    i=left;
    j=mid+1;
    k=left;
    while(i<=mid && j<=right)
    {
    if(initList[i]<initList[j])
    {
    mergeList[k++]=initList[i++];
    }
    else
    {
    mergeList[k++]=initList[j++];
    }

    if(i>mid)
    {
    while(j<=right)
    {
    mergeList[k++]=initList[j++];
    }

    }
    else if(j>right)
    {
    while(i<=mid)
    {
    mergeList[k++]=initList[i++];
    }
    }

    }
    }
    void MergePass(int *initList, int *mergeList, int n, int s)
    {
    int i,j;
    for(i=0; i<=n-2*s+1; i+=2*s)
    {
    Merge(initList, mergeList, i, i+s-1, i+2*s-1);
    }
    if(i+s<n)
    Merge(initList, mergeList, i, i+s-1, n-1);
    else
    for(j=i; j<=n-1; j++)
    mergeList[j] = initList[j];
    }
    void MergeSort(int *a, int b, int n)
    {
    int s=1;
    while(s<n)
    {
    MergePass(a, b, n, s);
    s*=2;
    MergePass(b, a, n, s);
    }
    }

    基数排序

    int get_index(int num, int dec,int order)
    {
    int i,j,n;
    int index;
    int div;
    for(i=dec; i>order; i--)
    {
    n=1;
    for(j=0; j<dec-1; j++)
    n*=10;
    div=num/n;
    num-=div*n;
    dec--;
    }
    n=1;
    for(i=0;i<order-1;i++)
    n*=10;
    index=num/n;
    return index;
    }
    void radis_sort(int array[], int dec, int order)
    {
    int i,j;
    int index;
    int tmp[10]={0};
    int num[10]={0};
    for(i=0; i<10; i++)
    {
    index=get_index(array[i], dec, order);
    num[index]++;
    }
    for(i=1; i<10; i++)
    num[i]+=num[i-1];
    for(i=10-1;i>=0;i--)
    {
    index=get_index(array[i], dec, order);
    {
    j=--num[index];
    tmp[j]=array[i];
    }
    }
    for(i=0;i<10;i++)
    {
    array[i]=tmp[i];
    }
    }

    二分查找

    int BinSelect(int x, int a[], int n)
    {
    int low=0, hight=n-1, mid;

    while(low <= hight)
    {
    mid = (low + hight)/2;
    if(x > a[mid])
    {
    low = mid +1;
    }
    else if(x < a[mid])
    {
    hight = mid-1;
    }
    else if(x == a[mid])
    {
    return mid-1;
    }

    }
    return -1;
    }

  • 相关阅读:
    Golang gRPC学习(01): gRPC介绍
    MySQL InnoDB存储引擎大观
    SpringBoot学习-图文并茂写Hello World
    Golang的goroutine协程和channel通道
    业务 产品 技术的一点看法
    需求一直做不完,怎么办?
    技术管理:项目开发中的几种风险管理
    go内存管理
    etcd实现分布式锁分析
    强缓存与协商缓存
  • 原文地址:https://www.cnblogs.com/youxianyen/p/7000483.html
Copyright © 2020-2023  润新知