java - counting swap count and compares in selection sort -


i'm kind of new java , i'm having trouble understanding concept. in code need add swap count , compare count , got far.

public static void selectionsort ( int [ ] num, int howmany )  {      int i, j, first, temp;     int compcount;   int swapcount;    ( = num.length - 1; > 0; i-- )    {      compcount = 0;      swapcount = 0;       first = 0;   //initialize subscript of first element       for(j = 1; j <= i; j ++)   //locate smallest element between positions 1 , i.       {            compcount++;            if( num[ j ] < num[ first ] )                       first = j;       }       temp = num[ first ];   //swap smallest found element in position i.       num[ first ] = num[ ];       num[ ] = temp;        swapcount++;     }            system.out.println("selection sort " + compcount + swapcount );      

    public static void selectionsort ( int [ ] num, int howmany )      {          int i, j, first, temp;         int compcount = 0;       int swapcount = 0;        ( = num.length - 1; > 0; i-- )        { /* should not reinitializing swap count , compcount inside loop. make 0 after each iteration not want*/           first = 0;   //initialize subscript of first element           for(j = 1; j <= i; j ++)   //locate smallest element between positions 1 , i.           {                compcount++;                if( num[ j ] < num[ first ] )                           first = j;           }           temp = num[ first ];   //swap smallest found element in position i.           num[ first ] = num[ ];           num[ ] = temp;            swapcount++;         }                system.out.println("selection sort " + compcount + swapcount );      

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -