算法 - 交换排序(Java)

/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.algorithm.sort;

/**
 *
 * @author Chimomo
 *
 * <p>
 * In programming design, swap sort is one of basic sorting algorithms.
 * So-called swapping, is swapping the position of two records by comparing
 * their key. The trait of swap sort is: move the record whose key is bigger
 * towards the rear of sequence and move the record whose key is smaller to the
 * front of sequence.
 * </p>
 */
public class SwapSort {

    /**
     * The swap sort.
     *
     * @param a The array to be sorted
     */
    public static void sort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    int t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                }
            }

            // Print each round of sorting.
            System.out.print("Round " + (i + 1) + ": ");
            for (int k = 0; k < a.length; k++) {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }

}

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/80565272
今日推荐