Java Array Sort selectionSort(int[] arr)

Here you can find the source of selectionSort(int[] arr)

Description

selection Sort

License

Open Source License

Declaration

public static void selectionSort(int[] arr) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static void selectionSort(int[] arr) {
        System.out.println(Arrays.toString(arr));
        int counter = 0;
        boolean swapRequired = false;
        for (int i = 0; i < arr.length - 1; i++) {
            int minArrIndex = i;
            swapRequired = false;//from  w w w .ja va2 s . c  o  m
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minArrIndex]) {
                    minArrIndex = j;
                    swapRequired = true;
                }
                counter++;
            }
            if (swapRequired) {
                int temp = arr[i];
                arr[i] = arr[minArrIndex];
                arr[minArrIndex] = temp;
                System.out.println(Arrays.toString(arr));
            }
        }
        System.out.println(Arrays.toString(arr));
        System.out.println("#" + counter);
    }
}

Related

  1. getSortedDistinct(int[] values)
  2. intArrayToShortArraySorted(int[] source)
  3. multiQuickSort(int[]... arrays)
  4. quickSort(int[] arr, int startIndex, int endIndex)
  5. radixSort(int[] vs)
  6. sort( final Item[] values, final Item[] auxiliary, final int first, final int last)
  7. sort( float[] array)
  8. sort(byte[] b, int pos)
  9. sort(double s[], int idx[])