字典序排序

一、題目描述

給1000個人名字符串,如:raina tina anne....將這些人名按字典順序排序

二、解題思路

字典順序就是按a b c d順序,對字符串排列,可以直接用strcmp實現

三、解題算法

/*****************************************
author:tmw
date:2018-8-26
******************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void swap( char* str1, char* str2 )
{
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    int len = len1>len2?len1:len2;
    char* temp = (char*)malloc(len*sizeof(char));

    temp = strcpy(temp, str1);
    str1 = strcpy(str1, str2);
    str2 = strcpy(str2, temp);

    free(temp);
}

void dictionary_sort( char** str, int str_len )
{
    if( str == NULL ) return;
    int i,j;
    for(i=0; i<str_len; i++)
    {
        for(j=i+1; j<str_len; j++)
        {
            /**前面人名的字典序大於後面人名的字典序--換位置**/
            if( strcmp(str[i], str[j]) > 0 )
                swap(str[i], str[j]);
        }
    }
    /**按字典序輸出**/
    for(i=0; i<str_len; i++)
        printf("%s ",str[i]);
}

 

                                                                                 夢想還是要有的,萬一實現了呢~~~加油~~ヾ(◍°∇°◍)ノ゙~~~~~~~~~~~~~~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章