Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package selectionsort;
- /**
- *This program was coded early by Jason Stewart
- * The purpose of this program is to generate an array of 25
- * random integers and sort it.
- */
- public class SelectionSort {
- public static void main(String[] args) {
- int[] list = new int[25];//an array of 25 integers
- selectionRandom(list);//calls the method that sorts
- }//end main
- public static void selectionRandom(int[]list)
- {
- System.out.println("Unsorted list: ");
- for (int i =0; i < list.length; i++) //for loop to generate values for
- { //array.length
- list[i] = (int)(1 + Math.random()*20);
- System.out.println(list[i]);
- }
- selectionSort(list);
- }//end method list
- public static void selectionSort(int[] list)//method that exists to sort
- { //and print integers
- System.out.println("\nSorted list: ");
- for (int i =0; i < list.length - 1; i++)//loop with swap algorythm
- {
- int currentMin = list[i]; //establishes the current min
- int currentMinIndex = i; //establishes the element name of current min
- for ( int j = i + i; j < list.length; j++) //verifies current min
- {
- if (currentMin > list[j])
- {
- currentMin = list[j]; //swaps current min value with list[j]
- currentMinIndex = j; //swaps the value
- }
- }//end for
- if (currentMinIndex != i) //swaps the element of array length
- {
- list[currentMinIndex] = list[i]; //swaps current min element with length[i]
- list[i] = currentMin; //swaps element value
- }//end if
- }//end for
- for ( int i = list.length - 1; i>=0; i--)//prints out the sorted array
- System.out.println(list[i]);
- }//end method
- }//end class
Add Comment
Please, Sign In to add comment