lecture.mp3


 

     1:  // SelectionSort.java
     2:  // by:  Raymond Lamp
     3:  //  date: 09/29/2010
     4:  //  the selection sort
     5:  
     6:  
     7:  import java.util.Scanner; // Scanner is in the java.util package 
     8:  
     9:  public class SelectionSort
    10:  {  //  class SelectionSort
    11:  
    12:    // Create a Scanner object
    13:    public static Scanner input = new Scanner(System.in); 
    14:  
    15:    public static int CompareCount = 0;  // the compare counter
    16:    public static int SwapCount = 0;  // the swap counter
    17:  
    18:    public static void main(String[] args) 
    19:    {   // void Main( args )
    20:  
    21:  
    22:      // a java program that uses the selection sort to sort an array of integers
    23:  
    24:      int[] MyList = new int[ 100 ];  //  an integer array that will hold 100 integers
    25:      int ListSize = 100;  // number of elements in the list
    26:  
    27:  
    28:  
    29:      // place code here
    30:  
    31:  
    32:  
    33:      // create a list of random integers and display them
    34:      Echoln("     create a random list of integers" );
    35:      CreateRandomList( MyList, ListSize );
    36:  
    37:      // display the random list of integers
    38:      Echoln("     the unsorted list of integers" );
    39:      DisplayList( MyList, ListSize );
    40:  
    41:      //  use the selection sort on the array of integers
    42:      Echoln("     doing the selection sort on the list of integers" );
    43:      DoSelectionSort( MyList, ListSize );
    44:  
    45:      // display the sorted list of integers
    46:      Echoln("     the sorted list of integers" );
    47:      DisplayList( MyList, ListSize );
    48:  
    49:  
    50:      // print the counts
    51:  
    52:      Echoln("     compare count = " + CompareCount + "     swap count = " + SwapCount );
    53:  
    54:  
    55:  
    56:    }   // Main( args )
    57:  
    58:  
    59:  
    60:  
    61:    public static void CreateRandomList( int[] List, int Size )
    62:    {  //  void CreateRandomList( List, Size )
    63:  
    64:  
    65:      // place random integers 0 .. 999 in the array List
    66:  
    67:      int Index;  // used in for loop
    68:  
    69:      for( Index = 0;  Index < Size ;  Index ++ )
    70:      List[ Index ] = ( int)( Math. random() * 1000 );
    71:  
    72:  
    73:    }  //  void  CreateRandomList( List, Size )
    74:  
    75:  
    76:    public static void DisplayList( int[] List, int Size )
    77:    {  //  void DisplayList( List, Size )
    78:  
    79:      // display an array of integers
    80:  
    81:      int Index;  //  used in for loop
    82:  
    83:      for( Index = 0;  Index < Size;  Index ++ )
    84:      {  //  for Index loop
    85:  
    86:        if(( Index % 10 ) == 0 )
    87:        Echoln( "" );  // new line
    88:        System. out. printf("%6d", List[ Index ] );  // display the value
    89:      }  //  for Index loop
    90:  
    91:      Echoln( "" );  // new line
    92:  
    93:  
    94:    }  //  void DisplayList( List, Size )
    95:  
    96:  
    97:  
    98:    public static void DoSelectionSort( int[] List, int Size )
    99:    {  //  void DoSelectionSort( List, Size )
   100:  
   101:      int Current;  // goes from 0 .. Size - 1
   102:      int Smallest;  // result of FindSmallest
   103:  
   104:      for( Current = 0;  Current < Size;  ++ Current )
   105:      {  //  for Current loop
   106:  
   107:        Smallest = FindSmallest( List, Current, Size - 1 );
   108:        DoSwap( List, Current, Smallest );
   109:  
   110:      }  //  for Current loop
   111:  
   112:  
   113:    }  //  void Do SelectionSort( List, Size )
   114:  
   115:    public static void DoSwap( int[] List, int X, int Y )
   116:    {  //  void DoSwap( List, X, Y )
   117:  
   118:  
   119:      int Z;  // the temp Z for a X Y Z swap
   120:  
   121:  
   122:      if( X != Y )
   123:      {  // if X and Y are not the same
   124:  
   125:        ++ SwapCount;  // add 1 to the swap counter
   126:  
   127:        Z = List[ X ];  // Z = X
   128:        List[ X ] = List[ Y ];  // X = Y
   129:        List[ Y ] = Z;  // Y = Z
   130:  
   131:      }  // if X and Y are not the same
   132:  
   133:    }  //  void DoSwap( List, X, Y )
   134:  
   135:  
   136:  
   137:  
   138:    public static int FindSmallest( int[] List, int Low, int High )
   139:    {  //  int FindSmallest( List, Low, High )
   140:  
   141:      int Offset;  // offset of smallest
   142:      int Smallest;  //  the smallest value in the list
   143:      int Index;  // used in for loop
   144:  
   145:      // set List[ Low ] to be the smallest
   146:  
   147:      Offset = Low;
   148:      Smallest = List[ Low ];
   149:  
   150:  
   151:  
   152:      for( Index = Low + 1;  Index <= High;  Index ++ )
   153:      {  //  for Index loop
   154:  
   155:  
   156:        ++ CompareCount;  // add 1 to the compare counter
   157:  
   158:        // did we find a new smallest value
   159:        if( List[ Index ] < Smallest )
   160:        {  // a new smallest value
   161:  
   162:          Smallest = List[ Index ];
   163:          Offset = Index;  // remember the offset of smallest
   164:        }  //  a new smallest value
   165:  
   166:      }  //  for Index loop
   167:  
   168:      return( Offset );  // the list offset of the smallest value in the unsorted part of the list
   169:  
   170:    }  //  int FindSmallest( List, Low, High )
   171:  
   172:  
   173:  
   174:    public static void Echo( String EchoStr )
   175:    {  //  void Echo( EchoStr )
   176:  
   177:      //  display EchoStr to the screen
   178:      System. out. print( EchoStr );
   179:    }  //  void Echo( EchoStr )
   180:  
   181:  
   182:    public static void Echoln( String EchoStr )
   183:    {  //  void Echoln( EchoStr )
   184:  
   185:      // display EchoStr to the screen followed by a newline
   186:      Echo( EchoStr );
   187:      System. out. println();
   188:  
   189:    }  //  void Echoln( EchoStr )
   190:  
   191:    public static int PromptForInt( String PromptStr )
   192:    {  //  int PromptForInt( PromptStr )
   193:  
   194:      //  display the prompt Str
   195:  
   196:      Echo( PromptStr );
   197:  
   198:  
   199:      // return the input int
   200:  
   201:      return( input. nextInt() );
   202:  
   203:    }  //  int PromptForInt( PromptStr )
   204:  
   205:  
   206:    public static double PromptForDouble( String PromptStr )
   207:    {  //  double PromptForDouble( PromptStr )
   208:  
   209:      //  display the prompt Str
   210:  
   211:      Echo( PromptStr );
   212:  
   213:  
   214:      // return the input double
   215:  
   216:      return( input. nextDouble() );
   217:  
   218:    }  //  double PromptForDouble( PromptStr )
   219:  
   220:  
   221:    public static String PromptForString( String PromptStr )
   222:    {  //  String PromptForString( PromptStr )
   223:  
   224:      //  display the prompt Str
   225:  
   226:      Echo( PromptStr );
   227:  
   228:  
   229:      // return the input String
   230:  
   231:      return( input. next() );
   232:  
   233:    }  //  String PromptForString( PromptStr )
   234:  
   235:  
   236:  
   237:    public static String PromptForLine( String PromptStr )
   238:    {  //  String PromptForLine( PromptStr )
   239:  
   240:      //  display the prompt Str
   241:  
   242:      Echoln( PromptStr );
   243:  
   244:  
   245:      // return the input String
   246:  
   247:      return( input. nextLine() );
   248:  
   249:    }  //  String PromptForLine( PromptStr )
   250:  
   251:  
   252:  }   // class SelectionSort
   253:  
   254:  

source code
SelectionSort.java