【问题标题】:C kind of sortingC类排序
【发布时间】:2020-08-11 04:49:12
【问题描述】:

好的,这是一个函数,它以这样一种方式排列数组中的元素,即所有元素 小于给定值的位置放置在较大元素左侧的位置 比给定值。

例如,如果数组内容是 {4,6,2,9,1,7,3,10} 并且 x 为 5,则 {4,3,2,1,9,7,6,10} 是一种可能的解决方案,因为所有小于 5 的元素都在左侧 大于 5 的元素。

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

另外,实现一个打印数组内容的函数。这两个功能必须是 递归实现。 您只能访问数组的每个元素一次。

好的,所以这个“挑战”我不知道在给定的限制下是否可行。我试图用一个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 的情况下递归地执行此操作。我猜你需要拆分数组,然后对半看

【问题讨论】:

  • "you are not allowed to change the parameters"是什么意思?你不能改变函数声明?你不能改变参数的值?
  • @Useless 我的意思是你不能改变 ""void Organizer(int array[],int size, int x){"" 你不能添加或删除参数
  • 您想在递归时更改数组和长度参数。如果第一个元素 x 则将长度减一并且您不再考虑这一点。如果第一个是> x,最后一个是
  • 我想要求是在中间留下 == x 的任何元素?这是在每一端都需要考虑的一组额外案例。

标签: c arrays recursion partition function-definition


【解决方案1】:

你来了。

#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 ) );
}

【讨论】:

  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 2015-09-20
  • 2016-10-15
  • 1970-01-01
相关资源
最近更新 更多