C类排序

问题描述 投票:1回答:1

好吧,那么一个函数,它可以将一个数组中的元素以这样一种方式排列,即所有小于给定值的元素都被放置在大于给定值的元素的左边。

例如,如果数组的内容是{4,6,2,9,1,7,3,10},x给定为5,那么{4,3,2,1,9,7,6,10}是一个可能的解决方案,因为所有小于5的元素都在大于5的元素的左边。

另外,除了在主函数中定义数组外,禁止使用括号 [ ]。

另外,实现一个打印数组内容的函数。这两个函数都必须以递归的方式实现,你只能访问数组的每个元素一次。

好吧,所以这个 "挑战",我不知道在给定的限制下是否可行。我曾试过用while循环,然后以某种方式将其转换为递归,但你不允许改变参数。有谁知道解决方案。

我已经写了一些东西,但它是垃圾。

#include <stdio.h>
#define length 8
void selection(int array[],int size, int x){

int i=0;
int temp;

        if(( array[i]>x ) && (array[i] > array[i+1])){

             temp=array[i+1];
             array[i+1]=array[i];
             array[i]=temp;

             i++;
                selection(array+1,size-1,x)
            }
        else if(( array[i] > x) && ( array[i+1] > array[i])){
                i++;
            }

    //This is not correct 
     }
void printArray(int arr[], int start, int len)
   {

    if(start >= len)
        return;

    printf("%d ", arr[start]);


    printArray(arr, start + 1, len); 
   }
int main(){

    int array[length]={6,4,2,9,1,7,3,10};
    int x=5;

     selection(array,length,x);
     printArray(array,0,length);

return 0;
}

我还没有实现递归的解决方案,因为我试过的东西一直给出分段故障,因为我在数组之外。

有谁能在没有for或while的情况下递归地做这件事。我想你需要把数组拆开,然后一半一半的看。

c arrays recursion partition function-definition
1个回答
3
投票

给你。

#include <stdio.h>

void partition( int a[], size_t n, int pivot )
{
    if ( !( n < 2 ) )
    {
        if ( *a < pivot )
        {
            partition( a + 1, n - 1, pivot );
        }
        else
        {
            if ( *( a + n - 1 ) < pivot )
            {
                int tmp = *a;
                *a = *( a + n - 1 );
                *( a + n - 1 ) = tmp;
                partition( a + 1, n - 2, pivot );
            }
            else
            {
                partition( a, n - 1, pivot );
            }
        }
    }
}

int main(void) 
{
     int a[] = { 4, 6, 2, 9, 1, 7, 3, 10 };
     const size_t N = sizeof( a ) / sizeof( *a );

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


     int pivot = 5;

     partition( a, N, pivot );

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

     return 0;
}

程序的输出是

4 6 2 9 1 7 3 10 
4 3 2 1 9 7 6 10 

或者也可以用函数的递归定义。printArray.

#include <stdio.h>

void partition( int a[], size_t n, int pivot )
{
    if ( !( n < 2 ) )
    {
        if ( *a < pivot )
        {
            partition( a + 1, n - 1, pivot );
        }
        else
        {
            if ( *( a + n - 1 ) < pivot )
            {
                int tmp = *a;
                *a = *( a + n - 1 );
                *( a + n - 1 ) = tmp;
                partition( a + 1, n - 2, pivot );
            }
            else
            {
                partition( a, n - 1, pivot );
            }
        }
    }
}

void printArray( const int a[], size_t n )
{
    if ( n )
    {
        printf( "%d ", *a );
        printArray( a + 1, n - 1 );
    }
    else
    {
        putchar( '\n' );
    }
}

int main(void) 
{
     int a[] = { 4, 6, 2, 9, 1, 7, 3, 10 };
     const size_t N = sizeof( a ) / sizeof( *a );

     printArray( a, N );     

     int pivot = 5;

     partition( a, N, pivot );

     printArray( a, N );     

     return 0;
}

递归函数 printArray 也可以用以下方式定义

void printArray( const int a[], size_t n )
{
    n == 0 ? ( void )putchar( '\n' ) 
           : ( printf( "%d ", *a ), printArray( a + 1, n - 1 ) );
}
© www.soinside.com 2019 - 2024. All rights reserved.