Guest User

Untitled

a guest
Jul 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package selectionsort;
  6.  
  7. /**
  8.  *This program was coded early by Jason Stewart
  9.  * The purpose of this program is to generate an array of 25
  10.  * random integers and sort it.
  11.  */
  12. public class SelectionSort {
  13.  
  14.    
  15.     public static void main(String[] args) {
  16.       int[] list = new int[25];//an array of 25 integers
  17.       selectionRandom(list);//calls the method that sorts
  18.     }//end main
  19.  
  20.     public static void selectionRandom(int[]list)
  21.     {
  22.         System.out.println("Unsorted list: ");
  23.         for (int i =0; i < list.length; i++) //for loop to generate values for
  24.         {                                    //array.length
  25.             list[i] = (int)(1 + Math.random()*20);
  26.             System.out.println(list[i]);
  27.         }
  28.       selectionSort(list);
  29.     }//end method list  
  30.     public static void selectionSort(int[] list)//method that exists to sort                                              
  31.     {                                           //and print integers
  32.         System.out.println("\nSorted list: ");
  33.         for (int i =0; i < list.length - 1; i++)//loop with swap algorythm
  34.         {
  35.            
  36.             int currentMin = list[i]; //establishes the current min
  37.             int currentMinIndex = i;  //establishes the element name of current min
  38.            
  39.             for ( int j = i + i; j < list.length; j++) //verifies current min
  40.             {
  41.                 if (currentMin > list[j])
  42.                 {
  43.                     currentMin = list[j]; //swaps current min value with list[j]
  44.                     currentMinIndex = j; //swaps the value
  45.                 }
  46.             }//end for
  47.            
  48.             if (currentMinIndex != i) //swaps the element of array length
  49.             {
  50.                 list[currentMinIndex] = list[i]; //swaps current min element with length[i]
  51.                 list[i] = currentMin; //swaps element value
  52.                
  53.             }//end if
  54.            
  55.            
  56.         }//end for
  57.            
  58.         for ( int i = list.length - 1; i>=0; i--)//prints out the sorted array
  59.               System.out.println(list[i]);  
  60.     }//end method
  61. }//end class
Add Comment
Please, Sign In to add comment