在一个函数中排序5个数组 [英] Sorting 5 arrays in one function

查看:80
本文介绍了在一个函数中排序5个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有5个数组和一个包含所有5个数组的指针数组,并且我需要编写一个仅使用指针数组对每个数组进行排序的函数,该怎么做? / p>

该函数需要从索引1(!)而不是0开始对数组中的每个单个数组进行排序。

  int arr1 [] = {3,9,6,7}; 
int arr2 [] = {2,5,5};
int arr3 [] = {0};
int arr4 [] = {1,6};
int arr5 [] = {4,5,6,2,1};
int * parr [] = {arr1,arr2,arr3,arr4,arr5};

我知道如何对一个数组进行排序,但是当我尝试对每个数组进行排序时我迷失了方向使用指针数组以尽可能最有效的方式处理数组。也许可以选择在一个循环中对每个数组进行排序?因为为每个数组中的每个数组执行一个完整的 for循环似乎有些奇怪



注意每个数组的第一个元素指示每一个的大小。例如:在 arr1 [0] 中为3,因此该数组中索引0之后的数字为3(9,6,7)。

解决方案

您可以循环调用标准C函数 qsort 数组 parr



例如

  #include< stdio.h> 
#include< stdlib.h>

int cmp(const void * a,const void * b)
{
return(*(const int *)b< *(const int *)a)-
(*(const int *)a< *(const int *)b);
}

int main(void)
{
int arr1 [] = {3,9,6,7};
int arr2 [] = {2,5,5};
int arr3 [] = {0};
int arr4 [] = {1,6};
int arr5 [] = {4,5,6,2,1};
int * parr [] = {arr1,arr2,arr3,arr4,arr5};

const size_t N = sizeof(parr)/ sizeof(* parr);

for(size_t i = 0; i< N; i ++)
{
qsort(parr [i] + 1,parr [i] [0],sizeof( int),cmp);
}

for(size_t i = 0; i< N; i ++)
{
for(size_t j = 0; j {
printf(%d,parr [i] [j]);
}
putchar(’\n’);
}

返回0;
}

程序输出为

  3 6 7 9 
2 5 5
0
1 6
4 1 2 5 6


If I have 5 arrays and one array of pointers that contains all of the 5 arrays, and I need to write a function that will sort every single one of the arrays using the pointers array only, how can I do that?

The function needs to sort every single one of the array starting from the index 1 (!) and not 0.

int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int * parr[] = { arr1, arr2, arr3, arr4, arr5 };

I know how to sort one array but I got a bit lost when I tried to sort every single one of the arrays using the pointers array in the most efficient way possible. Maybe there's an option to sort every array but in one loop? Because it seems a bit weird to do a whole "for" loop for every single one of the arrays

NOTE the first element of each array indicates the size of each one. For example : in arr1[0] is 3 so the amount of numbers that will come after the index 0 in that array is 3 (9,6,7).

解决方案

You can call in a loop standard C function qsort for each element of the array parr.

For example

#include <stdio.h>
#include <stdlib.h>

int cmp( const void *a, const void *b )
{
    return ( *( const int * )b < *( const int * )a ) -
           ( *( const int * )a < *( const int * )b );
}          

int main(void) 
{
    int arr1[] = { 3, 9, 6, 7 };
    int arr2[] = { 2, 5, 5 };
    int arr3[] = { 0 };
    int arr4[] = { 1, 6 };
    int arr5[] = { 4, 5, 6, 2, 1 };
    int * parr[] = { arr1, arr2, arr3, arr4, arr5 };

    const size_t N = sizeof( parr ) / sizeof( *parr );

    for ( size_t i = 0; i < N; i++ )
    {
        qsort( parr[i] + 1, parr[i][0], sizeof( int ), cmp );
    }

    for ( size_t i = 0; i < N; i++ )
    {
        for ( size_t j = 0; j < parr[i][0] + 1; j++ )
        {
            printf( "%d ", parr[i][j] );
        }
        putchar( '\n' );
    }

    return 0;
}

The program output is

3 6 7 9 
2 5 5 
0 
1 6 
4 1 2 5 6 

这篇关于在一个函数中排序5个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆