C ordering using recursion

0

I need to make a vector sort code using recursion. I looked into sorting methods and found two interesting ones: selection sort and quicksort. However, I tried to use selection sort and I did not succeed. Here is 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 make the first number stop and compare it with all the others until you find the smallest number. Then do the same with the second, third etc.

Another question: what is better to use to sort a vector with recursion: selection sort or quicksort?

    
asked by anonymous 20.06.2017 / 19:23

1 answer

1

SELECTION SORT

This is a simple algorithm that takes up little memory and is very efficient when sorting small volumes of data.

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

Sample Code (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

This is a very efficient algorithm, and on average, quicksort takes% with% comparisons to sort O(n log 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 */
    
20.06.2017 / 20:33