标签:
1、选择排序(时间复杂度为O(n2))
选择排序的思想是在线性表中找到最小数,并将其放在表头,然后在剩下的数中找到最小数,放在第一个数之后,直到线性表中仅剩下一个数为止。
Java实现:
public static void choiceSort(Integer[] a) {
if (a == null || a.length <= 0) {
return;
}
for (int i = 0; i < a.length; i++) {
int min = i; /* 将当前下标定义为最小值下标 */
for (int j = i + 1; j < a.length; j++) {
if (a[min] > a[j]) { /* 如果有小于当前最小值的关键字 */
min = j; /* 将此关键字的下标赋值给min */
}
}
if (i != min) {/* 若min不等于i,说明找到最小值,交换 */
int tmp = a[min];
a[min] = a[i];
a[i] = tmp;
}
}
}
2、插入排序(时间复杂度为O(n2))
插入排序的思想是在已经排好的子数组中反复插入一个新的元素,直到整个数组全部排好序。
Java实现:
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {
int temp = arr[i];// 待插入的元素
int j = i;
while (j > 0 && arr[j - 1] > temp) {// 将大于temp的往后移一位
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
}
}
3、冒泡排序(时间复杂度O(n2))
冒泡排序的思想是在每次遍历中,比较连续相邻的元素,如果某一对元素是降序,则互换它们的值;否则保持不变。第一次遍历后,最后一个元素成为数组中的最大数。第二次遍历之后,倒数第二个元素成为数组中的第二大数。整个过程持续到所有元素都已排序好。
注意:如果在某次遍历中没有发生变换,那么就不必进行下一次遍历,因为所有的元素都已经排好序了。
Java实现:
public static void bubbleSort(int[] list){
boolean needNextPass = true;
for(int k = 1; k < list.length && needNextPass; k++){
needNextPass = false;
for(int i = 0; i < list.length - k; i++){
if(list[i] > list[i+1]){
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
needNextPass = true;
}
}
}
}
4、快速排序(时间复杂度O(nlogn))
快速排序的思想是在数组中选择一个主元的元素,将数组分为两个部分,使得第一部分中的所有元素都小于或者等于主元,而第二部分中的所有元素都大于主元。对第一部分递归地应用快速排序算法,然后对第二部分递归地应用快速排序算法。
Java实现:
public static void quickSort(int[] list) {
quickSort(list, 0, list.length - 1);
}
public static void quickSort(int[] list, int first, int last) {
if (last > first) {
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex - 1);
quickSort(list, pivotIndex + 1, last);
}
}
private static int partition(int[] list, int first, int last) {
int pivot = list[first];
int low = first + 1;
int high = last;
while (high > low) {
while (low <= high && list[low] <= pivot)
low++;
while (low <= high && list[high] > pivot)
high--;
if (high > low) {
int temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}
while (high > first && list[high] >= pivot)
high--;
if (pivot > list[high]) {
list[first] = list[high];
list[high] = pivot;
return high;
} else {
return first;
}
}
5、归并排序(时间复杂度O(nlogn))
归并排序的思想是将数组分为两半,对每部分递归地应用归并排序。在两个部分都拍好序后,对它们进行归并。
Java实现:
public static void mergeSort(int[] list) {
if (list.length > 1) {
int[] firstHalf = new int[list.length / 2];
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf);
int secondHalfLength = list.length - list.length / 2;
int[] secondHalf = new int[secondHalfLength];
System.arraycopy(list, list.length / 2, secondHalf, 0,
secondHalfLength);
mergeSort(secondHalf);
int[] temp = merge(firstHalf, secondHalf);
System.arraycopy(temp, 0, list, 0, temp.length);
}
}
public static int[] merge(int[] list1, int[] list2) {
int[] temp = new int[list1.length + list2.length];
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
while (pos1 < list1.length && pos2 < list2.length) {
if (list1[pos1] < list2[pos2])
temp[pos3++] = list1[pos1++];
else
temp[pos3++] = list2[pos2++];
}
while (pos1 < list1.length)
temp[pos3++] = list1[pos1++];
while (pos2 < list2.length)
temp[pos3++] = list2[pos2++];
return temp;
}
标签:
原文地址:http://www.cnblogs.com/zihaowang/p/4947577.html