Java Insertion Sort insertionSortInPlaceSwap(int[] xs)

Here you can find the source of insertionSortInPlaceSwap(int[] xs)

Description

insertion Sort In Place Swap

License

Creative Commons License

Declaration

public static void insertionSortInPlaceSwap(int[] xs) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    public static void insertionSortInPlaceSwap(int[] xs) {
        for (int i = 1; i < xs.length; i++) {
            int j = i;
            while (j > 0 && xs[j - 1] > xs[j]) {
                swap(xs, j, j - 1);//from  w w  w .  ja  v a2 s  .  co  m
                j = j - 1;
            }
        }
    }

    private static void swap(int[] xs, int a, int b) {
        int temp = xs[a];
        xs[a] = xs[b];
        xs[b] = temp;
    }
}

Related

  1. insertionSort(int[] a)
  2. insertionSort(int[] a)
  3. insertionsort(int[] array)
  4. insertionSort(int[] input)
  5. insertionSort(int[] target, int[] coSort)