早教吧 育儿知识 作业答案 考试题库 百科 知识分享

c++数组急求答案(1)对一维数组中的元素进行排序,使其按照升序输出。数组元素的值使用随机数函数得到10到99之间的数。(使用冒泡排序)(2)设有字符序列"Alan","Rose","Jack","Tom","Jod

题目详情
c++ 数组 急求答案
(1)对一维数组中的元素进行排序,使其按照升序输出。数组元素的值使用随机数函数得到10到99之间的数。(使用冒泡排序)
(2)设有字符序列"Alan", "Rose", "Jack", "Tom", "Jodan", "John", "Bill", "Bird", "Farmer", "Fisher"将这些字符序列保存在一维数组中,并对其使用简单选择法进行排序,要求字符长度小的排列在前面,若长度相同,则按照字母序列a~z排列(不区分大小写)。
▼优质解答
答案和解析

不知道是否还来得及帮到你? 写了一个纯C的, 用的都是之前发布过的几个函数。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
/* define dimensions */
const unsigned int NUM_ELMS = 10;
const unsigned int MAX_STR_NUM = 10;
const unsigned int MAX_STR_LEN = 20;
/* function declare */
void BubbleSort(int *data, const size_t num);
void SelectionSort(char [][MAX_STR_LEN], const size_t num_str);
void print_array(const int arr[], const size_t len);
void show_strings(char [][MAX_STR_LEN], const size_t num_str);
int stricmp (const char *p1, const char *p2);
int main(int argc, char** argv)
{
    /* 1. Bubble sort demo for int array */   
    int values[NUM_ELMS] = {0};
    size_t len_array;
       
    srand(time(NULL)); /* random generator initial */
       
    for (int i = 0; i < NUM_ELMS; i ++)
        values[i] = rand() % 90 + 10; /* random range 10~99 */
           
    len_array = sizeof(values)/sizeof(values[0]);
      
    printf("1) Bubble sort before & after:\n");
    print_array(values, len_array);
    BubbleSort(values, len_array);
    print_array(values, len_array);
       
    /* 2. Selection sort demo for string array */
    char szNames[][MAX_STR_LEN] = {"Alan", "Rose", "Jack", "Tom", "Jodan",
                                      "John", "Bill", "Bird", "Farmer", "Fisher"};
    size_t total_str_num = sizeof(szNames) / sizeof(szNames[0]);
                                          
    printf("\n2) Selection sort before & after:\n");                                                                                                                     
    show_strings(szNames, total_str_num);   
    SelectionSort(szNames, total_str_num);
    show_strings(szNames, total_str_num);
       
    return 0;
}
int stricmp (const char *p1, const char *p2)
{
    register unsigned char *s1 = (unsigned char *) p1;
    register unsigned char *s2 = (unsigned char *) p2;
    unsigned char c1, c2;
    do
    {
       c1 = (unsigned char) toupper((int)*s1++);
        c2 = (unsigned char) toupper((int)*s2++);
        if (c1 == '\0')
        {
           return c1 - c2;
        }
    }
       
    while (c1 == c2);
    return c1 - c2;
}
void SelectionSort(char strlist[][MAX_STR_LEN], size_t num_str)
{
    char sztemp[MAX_STR_LEN] = "0\0";
    int i, j, min;
       
    for (i = 0; i < num_str - 1; i ++)
    {
        min = i;
        for (j = i+1; j < num_str; j ++)
            if (strlen(strlist[min]) > strlen(strlist[j]) )
               min = j;
            else if (strlen(strlist[min]) == strlen(strlist[j]) ) 
            {
                if ( stricmp(strlist[min], strlist[j]) > 0 )
                   min = j;
            }
               
        if ( min != i)   
        {
           strcpy(sztemp, strlist[min]);
           strcpy(strlist[min], strlist[i]);
           strcpy(strlist[i], sztemp);   
        }
    }
       
}
void show_strings(char strlist[][MAX_STR_LEN], size_t num_str)
{
    for (int i = 0; i < num_str; i ++)
        printf("%s ", strlist[i]);
           
    printf("\n");                
}
void BubbleSort(int *data, const size_t num)
{
    int i, j, _temp;
    i = num;
       
    while (i > 0)    
    {
        for (j = 0; j < i-1; j++)
        {
            if (data[j] > data[j+1])           
            {
                _temp = data[j];
                data[j] = data[j+1];
                data[j+1] = _temp;
            }
        }       
        i--;
    }
}
void print_array(const int arr[], const size_t len)
{
    int i;
       
    for (i = 0; i < len; i++)
        printf("%d ", arr[i]);
           
    printf("\n");
}