C类排序
C kind of sorting
好的,所以一个函数以这样的方式排列数组中的元素,即所有元素
小于给定值的元素被放置在较大元素左侧的位置
比给定的值。
例如数组内容为{4,6,2,9,1,7,3,10},x为5,则
{4,3,2,1,9,7,6,10} 是一个可能的解决方案,因为所有小于 5 的元素都在左边
大于 5 的元素。
此外,除了在main函数中定义数组外,禁止使用括号[]。
另外,实现一个打印数组内容的函数。两个函数都必须是
递归实现。
您只能访问数组的每个元素一次。
好的所以这个 "challenge" 我不知道给定的限制是否可行。我试图用一个 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 的情况下递归地执行此操作。我猜你需要拆分数组然后一半一半地看
给你。
#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 ) );
}
好的,所以一个函数以这样的方式排列数组中的元素,即所有元素 小于给定值的元素被放置在较大元素左侧的位置 比给定的值。
例如数组内容为{4,6,2,9,1,7,3,10},x为5,则 {4,3,2,1,9,7,6,10} 是一个可能的解决方案,因为所有小于 5 的元素都在左边 大于 5 的元素。
此外,除了在main函数中定义数组外,禁止使用括号[]。
另外,实现一个打印数组内容的函数。两个函数都必须是 递归实现。 您只能访问数组的每个元素一次。
好的所以这个 "challenge" 我不知道给定的限制是否可行。我试图用一个 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 的情况下递归地执行此操作。我猜你需要拆分数组然后一半一半地看
给你。
#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 ) );
}