zoukankan      html  css  js  c++  java
  • C 字符串数组排序

    #include<stdio.h>
    #include<string.h>
    #include <malloc.h>
    void q_sortB(char str[20][20], int n);
    void qs(char str[20][20],int n);
    
    void main() {
        int i, n;
        char str[20][20] = { { "Adam" }, { "Bob" }, { "Dimen" }, { "Colin" }, {
                "Correal" }, { "Sick" }, { "Rachel" } };
    
        char * str1[20]= { { "Adam" }, { "Bob" }, { "Dimen" }, { "Colin" },
                { "Correal" }, { "Sick" }, { "Rachel" } };
        qs(str,7);
        q_sortB(str, 7);
        for (i = 0; i < 7; i++){
            printf("%s\n", str[i]);
        }
    
    
    }
    
    void qs(char str[20][20],int n){
        char temp[20];
        int i=0;
        int j=0;
        int min=i;
        for(i=0;i<n-1;i++){
            min=i;
            for(j=i;j<n;j++){//本次找最小值的范围是从i开始  到最末尾
                if(  strcmp(str[j],str[min])==-1  ){
                    min=j;
                }
            }
            //此时min指向最小的
            //那么应该把min放在已排序部分的后一个//也就是本次排序的第一个
            strcpy(temp,str[i]);
            strcpy(str[i],str[min]);
            strcpy(str[min],temp);
    
        }
    }
    
    //泡泡
    void q_sortB(char str[20][20], int n) {
        char a[20];
        int i, j;
        for (i = 0; i < n-1; i++) {
            for (j = i ; j < n-1; j++)
                if (strcmp(str[j], str[j + 1]) > 0) {
                    strcpy(a, str[j]);
                    strcpy(str[j], str[j + 1]);
                    strcpy(str[j+1], a);
                }
        }
    
    }
  • 相关阅读:
    PHP 学习轨迹
    beego 遇到的一些问题
    Fiddler 502问题
    SourceTree
    Trait
    PHP PSR 标准
    解决MySQL联表时出现字符集不一样
    Git 代码管理命令
    PHP 运行相关概念
    CentOS 7
  • 原文地址:https://www.cnblogs.com/cart55free99/p/2979703.html
Copyright © 2011-2022 走看看