排序

排序算法整理

  1. 选择排序
  2. 插入排序

选择排序

实现步骤:找到数组中最小的元素,并将它和第一个元素进行交换位置,接着找到剩余数组中最小的元素和第二个位置的元素进行位置交换,依次类推,直到整个数组都被排序
选择排序每次都是选择是最小的元素,所以被称为选择排序

代码实现

public class Selection
{
	public static void sort(Comparable[] a)
	{ // Sort a[] into increasing order.
		int N = a.length; // array length
		for (int i = 0; i < N; i++)
		{ 
                        // Exchange a[i] with smallest entry in a[i+1...N).
			int min = i; // index of a minimal entry.
			for (int j = i+1; j < N; j++) {
				if (less(a[j], a[min])) {
					min = j;
                }
            }
			exch(a, i, min);
		}
	}
// See page 245 for less(), exch(), isSorted(), and main().
}

插入排序

原文地址:https://www.cnblogs.com/xiaozu/p/8878652.html