zoukankan      html  css  js  c++  java
  • 所有的排序算法

    /**************插入排序****************/

    #include <stdio.h>

    int str[]={3,1,34,5,3,7,9};
    void insertSort(int str[],int length);
    int length(int str[]);
    void printArray(int str[],int length);
    int main(){
    insertSort(str,7);
    printArray(str,7);
    return 0;
    }

    void printArray(int str[],int length){
    for(int i=0;i<length;i++){
    printf("%d\t",str[i]);
    }
    printf("\n");
    }

    void insertSort(int a[],int length){
    int i,j,temp;
    for(i=1;i<length;i++)
    {
    if(a[i]<a[i-1]){
    temp=a[i];
    a[i]=a[i-1];

    for(j=i-2;j>=0 && a[j]>temp;j--){
    a[j+1]=a[j];
    }
    a[j+1]=temp;
    }
    }
    }

    /******************shell排序************************/

    #include <stdio.h>

    int str[]={3,1,34,5,3,7,9};
    void shellSort(int str[],int length,int gap);
    //int length(int str[]);
    void printArray(int str[],int length);
    int main(){
    shellSort(str,7,5);
    printArray(str,7);
    return 0;
    }

    void printArray(int str[],int length){
    for(int i=0;i<length;i++){
    printf("%d\t",str[i]);
    }
    printf("\n");
    }

    void shellSort(int a[],int length,int gap){
    int i,j,temp;
    for(gap;gap>0;gap=gap/2)
    for(i=gap;i<=length-gap;i++)
    {
    if(a[i]<a[i-gap]){
    temp=a[i];
    a[i]=a[i-gap];

    for(j=i-gap;j>=0 && a[j]>temp;j=j-gap){
    a[j+gap]=a[j];
    }
    a[j+gap]=temp;
    }
    }
    }

    /***************************快速排序算法***************************/

    #include <stdio.h>
    #include <string.h>

    void Qsort(int a[],int left,int right);
    int partition(int a[],int left,int right);
    void swap(int a[],int left,int right);
    void printA(int a[]);
    int main(){
    int a[]={
    10,9,8,7,6,5,4,3,2,1,};
    Qsort(a,0,9);
    printA(a);
    }

    void Qsort(int a[],int left,int right){
    if(left<=right) //一定是left<=right,这个要清楚啊。还是快速排序不熟的缘故啊
    {
    int sign=partition(a,left,right);
    Qsort(a,left,sign-1);
    Qsort(a,sign+1,right);
    }
    }

    int partition(int a[],int left,int right)
    {
    int temp=a[left];
    while(left<right){
    while(left<right && temp<=a[right]) {
    right--;
    }
    a[left]=a[right];
    while(left<right && temp>=a[left]){
    left++;
    }
    a[right]=a[left];
    }
    a[left]=temp;
    return left;
    }
    void swap(int a[],int left,int right){
    int temp;
    temp=a[right];
    a[right]=a[left];
    a[left]=temp;
    }
    void printA(int a[]){
    for(int i=0;i<10;i++)
    {
    printf("%d\t",a[i]);
    }
    }

  • 相关阅读:
    spring-boot-maven-plugin not found的解决方案
    springboot项目idea代码报红,maven clean, maven reimport都不起作用
    ubuntu升级python版本(3.5 -> 3.6)
    安卓时间戳转成时间存在误差
    litepal创建数据库表失败
    安卓实现标题和按钮在一行,按钮靠最右边布局
    idea2019设置智能提示忽略大小写
    PowerDesigner连接MySQL逆向生成PDM
    javaweb开发页面数字过长显示科学计数法的问题
    react native cannot read property 'navigate' of undefined
  • 原文地址:https://www.cnblogs.com/mingChou/p/sort.html
Copyright © 2011-2022 走看看