Thread: selection sorting using going-down and going-up recursion

  1. #1
    Unregistered
    Guest

    selection sorting using going-down and going-up recursion

    Now I have worked on this for quite a long time.
    I have two problems.

    First and foremost, I need the sorting functions to be coded with recursive calls (I don't know how to do that from what I got here with for loops)


    What is going-up recursion and going-down recursion?
    I am reading that I need going-down recursion to sort in ascending order ; and going-up recursion for descending order.
    What does that mean.....exactly???


    Secondly, I cannot get the descending order function to work quite right. I think it is taking the first element of the original array and assigning it to array[0], then it sorts the rest in descending order. What am I doing wrong???

    PHP Code:

    #include <stdio.h>

    #define NUMELEM 10
    #define MAXELEM 200

    void select_sort_ascend(int array[], int s);
    void select_sort_descend(int array[], int s);
    void print_array (int array[], int s);

    main()
    {
      
    int i;
      
    int array[NUMELEM];

      
    /**************************************/
      /*  fill array with random numbers    */
      /**************************************/

         
    for (0NUMELEMi++)   {
         array[
    i] = rand()%MAXELEM+1;
      }

      
    /**************************************/
      /*  print random array                */
      /**************************************/

      
    printf("The random array : \n\n");

      
    print_array(array, NUMELEM);


      
    /**************************************/
      /*  sort array into ascending order   */
      /*  using selection sorting           */
      /**************************************/

      
    select_sort_ascend(array,NUMELEM);

      
    /**************************************/
      /* print array in ascending order     */
      /**************************************/

    printf("The random array in ascending order: \n\n");

    print_array(array,NUMELEM);

    /*************************/
    /*  sort array in        */
    /*    descending order   */
    /*************************/

    select_sort_descend(array, NUMELEM);

    /**************************************/
    /* print array in descending order    */
    /**************************************/

    printf("The random array in descending order: \n\n");

     
    /**************************************/
    /* print array in descending order    */
    /**************************************/

    print_array(array,NUMELEM);

    return 
    0;

    }

                  
    /***********************/
                  /*   END OF MAIN       */
                  /***********************/



    void select_sort_ascend(int array[],int s)
    {
      
    int ijsmallesttemp;

      for (
    i=0;i<NUMELEM;i++)    {
          
    smallest=i;

          for (
    j=ij<NUMELEM;j++)     {
              if (array[
    smallest] > array[j])  {
                 
    smallest j;
              }
          }

          
    temp = array[i];
          array[
    i] = array[smallest];
          array[
    smallest] = temp;
      }

    }

    void select_sort_descend(int array[],int s)
    {
      
    int ijlargesttemp;

      for (
    i=NUMELEM;i>0;i--)     {
          
    largest=i;

          for (
    j=i;j>0;j--)   {

              if (array[
    j] < array[largest])   {
                  
    largest j;
              }
          }

          
    temp = array[i];
          array[
    i] = array[largest];
          array[
    largest] = temp;
       }

    }

    void print_array(int array[],int s)
    {
       
    int i;

       for (
    i=0i<NUMELEMi++)   {
          
    printf("%3d  ",array[i]);

          if ((
    i+1)%== 0)
            
    printf("\n");
       }



  2. #2
    Sayeh
    Guest

    Recursion & Sorting

    recursion is recursion. neither going up nor going down. Recursion occurs when a function/procedure calls itself. If not done correctly, you have an infinite loops.

    "going up" and "going down" refer to the order of sorting. Either sort the data in ascending or descending order.

Popular pages Recent additions subscribe to a feed