冒泡排序、快速排序、堆排序

1、冒泡排序

  算法描述:

  1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
  3. 针对所有的元素重复以上的步骤,除了最后一个。
  4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较

  时间复杂度:O(n^2)

public class BubbleSort {

	static int count = 0;
	
	static int[] shulie = new int[] {87,2,548,22,453,21,9,432,75,21,33,88};
	
	public static void main(String[] args) {
		for (int i = shulie.length; 0 < i; i--) {
			for (int j = 0; j < i-1; j++) {
				if (shulie[j] > shulie[j+1]) {
					swap(j, j+1);
				}
				count++;
			}
		}
		System.out.println(Arrays.toString(shulie));
		System.out.println("比较次数:" + count);
	}
	
	static void swap(int a, int b) {
		shulie[a] = shulie[a] + shulie[b];
		shulie[b] = shulie[a] - shulie[b];
		shulie[a] = shulie[a] - shulie[b];
	}
	
}

  

2、快速排序

     算法描述:

  1. 设置两个变量i、j,排序开始的时候:i=0,j=N-1;
  2. 以第一个数组元素作为关键数据,赋值给key,即key=A[0];
  3. 从j开始向前搜索,即由后开始向前搜索(j--),找到第一个小于key的值A[j],将A[j]和A[i]互换;
  4. 从i开始向后搜索,即由前开始向后搜索(i++),找到第一个大于key的A[i],将A[i]和A[j]互换;
  5. 重复第3、4步,直到i=j; (3,4步中,没找到符合条件的值,即3中A[j]不小于key,4中A[i]不大于key的时候改变j、i的值,使得j=j-1,i=i+1,直至找到为止。找到符合条件的值,进行交换的时候i, j指针位置不变。另外,i==j这一过程一定正好是i+或j-完成的时候,此时令循环结束)

  时间复杂度:O(n^2)

public class QuickSort {

	static int count = 0;
	
	static int[] shulie = new int[] {87,2,548,22,453,21,9,432,75,21,33,88};
	
	static void swap(int a, int b) {
		shulie[a] = shulie[a] + shulie[b];
		shulie[b] = shulie[a] - shulie[b];
		shulie[a] = shulie[a] - shulie[b];
	}
	
	public static void main(String[] args) {
		kp(0, shulie.length-1);
		System.out.println(Arrays.toString(shulie));
		System.out.println("比较次数:" + count);
	}
	
	static void kp(int from, int to) {
		boolean direct = true;
		int i=from, j=to, k=shulie[from];
		while (i!=j) {
			if (direct) {
				if (shulie[j] < k) {
					swap(i, j);
					direct=false;
				} else {
					j--;
				}
			} else {
				if (shulie[i] > k) {
					swap(i, j);
					direct = true;
				} else {
					i++;
				}
			}
			count++;
		}
		if (to-from > 1) {
			if (i-1 >= from) {
				kp(from,i-1);
			}
			if (i+1 <= to) {
				kp(i+1,to);
			}
		}
	}
	
}

  

3、堆排序

     概念:

     1、堆:

          

  2、算法描述:

    1. 先将初始文件R[1..n]建成一个大根堆,此堆为初始的无序区
    2. 再将关键字最大的记录R[1](即堆顶)和无序区的最后一个记录R[n]交换,由此得到新的无序区R[1..n-1]和有序区R[n],且满足R[1..n-1].keys≤R[n].key
    3. 由于交换后新的根R[1]可能违反堆性质,故应将当前无序区R[1..n-1]调整为堆。然后再次将R[1..n-1]中关键字最大的记录R[1]和该区间的最后一个记录R[n-1]交换,由此得到新的无序区R[1..n-2]和有序区R[n-1..n],且仍满足关系R[1..n-2].keys≤R[n-1..n].keys,同样要将R[1..n-2]调整为堆。
      ……
      直到无序区只有一个元素为止。

  3、时间复杂度:O(nlgn)

public class HeapSort {
	
	private static int[] sort = new int[] {87,2,548,22,453,21,9,432,75,21,33,88};

	private static int count = 0;
	
	public static void main(String[] args) {
		buildMaxHeapify(sort);
		heapSort(sort);
		System.out.println(Arrays.toString(sort));
		System.out.println("比较次数:" + count);
	}

	private static void buildMaxHeapify(int[] data) {
		// 没有子节点的才需要创建最大堆,从最后一个的父节点开始
		int startIndex = getParentIndex(data.length - 1);
		// 从尾端开始创建最大堆,每次都是正确的堆
		for (int i = startIndex; i >= 0; i--) {
			maxHeapify(data, data.length, i);
		}
	}

	/**
	 * 创建最大堆
	 *
	 * @param data
	 * @param heapSize 需要创建最大堆的大小,一般在sort的时候用到,因为最多值放在末尾,末尾就不再归入最大堆了
	 * @param index 当前需要创建最大堆的位置
	 */
	private static void maxHeapify(int[] data, int heapSize, int index) {
		count++;
		// 当前点与左右子节点比较
		int left = getChildLeftIndex(index);
		int right = getChildRightIndex(index);

		int largest = index;
		if (left < heapSize && data[index] < data[left]) {
			largest = left;
		}
		if (right < heapSize && data[largest] < data[right]) {
			largest = right;
		}
		// 得到最大值后可能需要交换,如果交换了,其子节点可能就不是最大堆了,需要重新调整
		if (largest != index) {
			int temp = data[index];
			data[index] = data[largest];
			data[largest] = temp;
			maxHeapify(data, heapSize, largest);
		}
	}

	/**
	 * 排序,最大值放在末尾,data虽然是最大堆,在排序后就成了递增的
	 *
	 * @paramdata
	 */
	private static void heapSort(int[] data) {
		// 末尾与头交换,交换后调整最大堆
		for (int i = data.length - 1; i > 0; i--) {
			int temp = data[0];
			data[0] = data[i];
			data[i] = temp;
			maxHeapify(data, i, 0);
		}
	}

	/**
	 * 父节点位置
	 *
	 * @paramcurrent
	 * @return
	 */
	private static int getParentIndex(int current) {
		return (current - 1) >> 1;
	}

	/**
	 * 左子节点position注意括号,加法优先级更高
	 *
	 * @paramcurrent
	 * @return
	 */
	private static int getChildLeftIndex(int current) {
		return (current << 1) + 1;
	}

	/**
	 * 右子节点position
	 *
	 * @paramcurrent
	 * @return
	 */
	private static int getChildRightIndex(int current) {
		return (current << 1) + 2;
	}

	/**
	 * 以2为底的对数
	 *
	 * @paramparam
	 * @return
	 */
	private static double getLog(double param) {
		return Math.log(param) / Math.log(2);
	}
}

  

原文地址:https://www.cnblogs.com/yinkh/p/6439285.html