C ordering using recursion

Asked

Viewed 2,102 times

0

I need to make a vector ordering code using recursion. I looked into the sorting methods and found two that would be interesting: Lection Sort and Quicksort. However, I tried to use Selection Sort and I was not successful. Follow my code:

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


int numeros[5]; 
int i;

void Ordenar(int numeros[], int i, int j){
  int aux;
  int menor;

  if(i==3){
  for(i=0; i<5; i++){
    printf("%d", numeros[i]);
  }
  return 0;
  }

  if(j==4){
    return Ordenar(numeros, i+1, i+2);
  }
  if(numeros[i]>numeros[j]){
    menor = j;
    aux = numeros[j];
    numeros[menor] = numeros[i];
    numeros[i] = aux;
  }

  else{
    return Ordenar(numeros, i, j+1);
  }

}


int main(){
  for(i=0; i<5; i++){
    scanf("%d", &numeros[i]);
  }

  Ordenar(numeros, 0, 1);
  return 0;
}

The idea was to leave the first number still and compare them with all the others, until finding the smallest. Then, do the same with the second, third etc.

Another question: what is best used to sort an array with recursion: Selection Sort or quicksort?

1 answer

1


SELECTION SORT

It is a simple algorithm that takes up little memory and is quite efficient if sorting small volumes of data.

It is extremely slow to order large volume of data, its complexity will always be O(n^2).

Code Example (Tested):

#include <stdio.h>
#include <string.h>

#define swap( _a, _b ) do{ int _tmp = _a; _a = _b; _b = _tmp; } while(0)

void selectionsort( int array[], int tam )
{
    int i = 0;
    int j = 0;
    int min = 0;

    for( i = 0; i < ( tam - 1 ); i++)
    {
        min = i;

        for( j = (i+1); j < tam; j++ )
            if( array[j] < array[min] )
                min = j;

        if( i != min )
            swap( array[i], array[min] );
    }
}

void exibir( int array[], int tam )
{
    int i = 0;

    for( i = 0; i < tam; i++ )
        printf( "%s%d", (i>0)?", ":"", array[i] );

    printf("\n");
}


int main( int argc, char * argv[] )
{
    int numeros[16] = { 6, -9, 7, 5, 3, -1, 8, -6, 4, 2, 1, -3, -5, 9, -8, 0 };

    printf("Array Original: ");
    exibir( numeros, 16 );

    selectionsort( numeros, 16 );

    printf("Array Ordenada: ");
    exibir( numeros, 16 );

    return 0;
}

/* fim-de-arquivo */

QUICK SORT

It is a very efficient algorithm, and on average the quicksort wave O(n log n) comparisons to sort n items.

#include <stdio.h>
#include <string.h>

#define swap( _a, _b ) do{ int _tmp = _a; _a = _b; _b = _tmp; } while(0)

void quicksort( int array[], int start, int end )
{
    if( start < end )
    {
        int l = start + 1;
        int r = end;
        int p = array[start];

        while( l < r )
        {
            if( array[l] <= p )
            {
                l++;
            }
            else if( array[r] >= p )
            {
                r--;
            }
            else
            {
                swap( array[l], array[r] );
            }
        }

        if( array[l] < p )
        {
            swap( array[l], array[start] );
            l--;
        }
        else
        {
            l--;
            swap( array[l], array[start] );
        }

        quicksort( array, start, l );
        quicksort( array, r, end );
    }
}

void exibir( int array[], int tam )
{
    int i = 0;

    for( i = 0; i < tam; i++ )
        printf( "%s%d", (i>0)?", ":"", array[i] );

    printf("\n");
}


int main( int argc, char * argv[] )
{
    int numeros[16] = { 6, -9, 7, 5, 3, -1, 8, -6, 4, 2, 1, -3, -5, 9, -8, 0 };

    printf("Array Original: ");
    exibir( numeros, 16 );

    quicksort( numeros, 0, 16 );

    printf("Array Ordenada: ");
    exibir( numeros, 16 );

    return 0;
}

/* fim-de-arquivo */

Browser other questions tagged

You are not signed in. Login or sign up in order to post.