1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

learning alogrithms

Discussion in 'C' started by compscichick, Sep 22, 2010.

  1. compscichick

    compscichick New Member

    Joined:
    Sep 7, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    So the problem is to illustrate how the selection sort algorithm behaves on two inputs:
    A={1,2,3,4,5} and A={5,4,3,2,1}. I need to state the output generated and then write a function for Big Oh notation.

    The algorithm is:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void printout (int A [], int n)
    {
        for (int i=0; i<n; ++i)
            cout << A[i] << " ";
        cout<< '\n';
    }
    
    void selectionSort (int A[], int n)
    {
        for (int k=0; k<n-1; ++k) {
            int tmp, index = k;
            for (int i=k+1; i < n; ++i)
                if (A[i] < A[index]) index=i;
            tmp= A[k]; // swap
            A[k] = A[index];
            A [index] = tmp;
            printout (A,n);
        }
    }
    int main ()
    {
        system("pause");
        return 0; 
    }
    
    I thought i would need to insert a 5 in int A[5] for there being 5 vectors slots. I can get it to run and compile, but then nothing is outputted. What am I doing wrong?
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    In
    Code:
    int main ()
    {
        system("pause");
        return 0; 
    }
    
    You are not doing anything but pausing. Maybe you need to set up variables, call a function or two?

    Jim
     

Share This Page