Advertisement
Guest User

SelectionSortAlgorithm

a guest
Jun 9th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package testproject;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SelectionSortAlgorith {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner input = new Scanner(System.in);
  10.         int n = Integer.parseInt(input.nextLine());
  11.         int[] numbers = new int[n];
  12.         int elementIndex = 0;
  13.         int container = 0;
  14.  
  15.         for (int i = 0; i < numbers.length; i++) {
  16.             numbers[i] = input.nextInt();
  17.         }
  18.  
  19.         int minElement = Integer.MAX_VALUE;
  20.  
  21.         for (int j = 0; j < numbers.length - 1; j++) {
  22.             for (int i = j + 1; i < numbers.length; i++) {
  23.                 if (numbers[i] < numbers[j]) {
  24.                     if (numbers[i] < minElement) {
  25.                         minElement = numbers[i];
  26.                         elementIndex = i;
  27.                     }
  28.                     container = numbers[j];
  29.                     numbers[j] = numbers[elementIndex];
  30.                     numbers[elementIndex] = container;
  31.                     minElement = Integer.MAX_VALUE;
  32.  
  33.                 }
  34.             }
  35.  
  36.         }
  37.  
  38.         for (int i = 0; i < numbers.length; i++) {
  39.             System.out.println(numbers[i]);
  40.         }
  41.  
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement