Please explain this code !!!! :(

This is a discussion on Please explain this code !!!! :( within the C Programming forums, part of the General Programming Boards category; I got this code for selection sorting, with the functions to sort set up for recursive calls. I don't understand ...

  1. #1
    Unregistered
    Guest

    Please explain this code !!!! :(

    I got this code for selection sorting, with the functions to sort set up for recursive calls.

    I don't understand this code.

    Could someone explain to me the logic??
    basically loop by loop -- function by function

    I did find how to do it without recursion, and I actually get that.

    Also, do you think there has to be two separate functions, one to sort in ascending and descending orders; or can they be done in one function with a switch or if statement???

    Also, to find the index of the smallest and largest element of original array, does this have to be split into two separate
    functions, or again can this be accomplished with one function that finds both, yet recalls the values for sorting once in ascending order and once in descending order???

    if it would be suffice, you can email me directly at
    unique1_63301@yahoo.com

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    #define Num_elem 10
    #define Max_elem 50

    void print_array(int x[], int n);
    void place_smallest(int[], int);
    void place_largest(int[], int);


    void selection_sort(int x[],int n)
    {
        if (
    1)  {
            
    place_largest (xn);
            
    selection_sort (x1);
       }
       return;
    }

    void place_largest(int x[], int n)
    {
        
    int temp;
        
    int max_index;
        
    int j;
        
    max_index 1;

        for (
    2>= 0j--)
            if (
    x[j] > x[max_index])
            
    max_index j;

           if (
    max_index != (1))    {
               
    temp x[1];
               
    x[1] = x[max_index];
               
    x[max_index] = temp;
           }
          return;
    }

    void selection_sort2(int x[],int n)
    {
       if (
    1)    {
        
    place_smallest (xn);
        
    selection_sort2 (x1);
       }
       return;
    }

    void place_smallest(int x[], int n)
    {
        
    int temp;
        
    int min_index;
        
    int j;
        
    min_index 1;
        for (
    2>= 0j--)
              if (
    x[j] < x[min_index])
        
    min_index j;

        if (
    min_index != (1))   {
            
    temp x[1];
            
    x[1] = x[min_index];
            
    x[min_index] = temp;
        }
        return;
    }

    main()
    {
       
    int a[Num_elem];
       
    int i 00;
       
    srand((unsignedtime(NULL));
       for (
    0Num_elemi++)
           
    a[i] = rand() % Max_elem 1;
       
    print_array(aNum_elem);
       
    selection_sort(aNum_elem);

       
    printf("Sorted array in ascending order:\n");
       
    print_array(aNum_elem);
       
    printf("\n");
       
    selection_sort2(aNeum_elem);
       
    printf("Sorted array in desending order:\n");
       
    print_array(aNum_elem);
       return 
    0;
    }

    void print_array(int x[], int n)
    {
       
    int i;

       for (
    0ni++)
         
    printf("   %5d"x[i]);
         
    printf("\n");


  2. #2
    Unregistered
    Guest
    is this better, Salem???

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define Num_elem 10
    #define Max_elem 50
    
    void print_array(int x[], int n);
    void place_smallest(int[], int);
    void place_largest(int[], int);
    
    
    void selection_sort(int x[],int n)
    {
        if (n > 1)  {
            place_largest (x, n);
            selection_sort (x, n - 1);
       }
       return;
    }
    
    void place_largest(int x[], int n)
    {
        int temp;
        int max_index;
        int j;
        max_index = n - 1;
    
        for (j = n - 2; j >= 0; j--)
            if (x[j] > x[max_index])
            max_index = j;
    
           if (max_index != (n - 1))    {
               temp = x[n - 1];
               x[n - 1] = x[max_index];
               x[max_index] = temp;
           }
          return;
    }
    
    void selection_sort2(int x[],int n)
    {
       if (n > 1)    {
        place_smallest (x, n);
        selection_sort2 (x, n - 1);
       }
       return;
    }
    
    void place_smallest(int x[], int n)
    {
        int temp;
        int min_index;
        int j;
        min_index = n - 1;
        for (j = n - 2; j >= 0; j--)
              if (x[j] < x[min_index])
        min_index = j;
    
        if (min_index != (n - 1))   {
            temp = x[n - 1];
            x[n - 1] = x[min_index];
            x[min_index] = temp;
        }
        return;
    }
    
    main()
    {
       int a[Num_elem];
       int i = 0, j = 0;
       srand((unsigned) time(NULL));
       for (i = 0; i < Num_elem; i++)
           a[i] = rand() % Max_elem + 1;
       print_array(a, Num_elem);
       selection_sort(a, Num_elem);
    
       printf("Sorted array in ascending order:\n");
       print_array(a, Num_elem);
       printf("\n");
       selection_sort2(a, Neum_elem);
       printf("Sorted array in desending order:\n");
       print_array(a, Num_elem);
       return 0;
    }
    
    void print_array(int x[], int n)
    {
       int i;
    
       for (i = 0; i < n; i++)
         printf("   %5d", x[i]);
         printf("\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 01:27 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21