您的位置:首页 > 编程语言 > Java开发

java实现四种常用排序算法

2017-07-13 19:16 274 查看

四种常用排序算法

冒泡排序

特点:效率低,实现简单

思想(从小到大排):每一趟将待排序序列中最大元素移到最后,剩下的为新的待排序序列,重复上述步骤直到排完所有元素。这只是冒泡排序的一种,当然也可以从后往前排。

public void bubbleSort(int array[]) {
int t = 0;
for (int i = 0; i < array.length - 1; i++)
for (int j = 0; j < array.length - 1 - i; j++)
if (array[j] > array[j + 1]) {
t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
}
}


选择排序

特点:效率低,容易实现。

思想:每一趟从待排序序列选择一个最小的元素放到已排好序序列的末尾,剩下的位待排序序列,重复上述步骤直到完成排序。

public void selectSort(int array[]) {
int t = 0;
for (int i = 0; i < array.length - 1; i++)
for (int j = i + 1; j < array.length; j++)
if (array[i] > array[j]) {
t = array[i];
array[i] = array[j];
array[j] = t;
}
}


插入排序

特点:效率低,容易实现。

思想:将数组分为两部分,将后部分元素逐一与前部分元素比较,如果当前元素array[i]小,就替换。找到合理位置插入array[i]

public void insertionSort(int array[]) {
int i, j, t = 0;
for (i = 1; i < array.length; i++) {
t = array[i];
for (j = i - 1; j >= 0 && t < array[j]; j--)
array[j + 1] = array[j];
array[j + 1] = t;
}
}


快速排序

特点:高效,时间复杂度为nlogn。

采用分治法的思想:首先设置一个轴值pivot,然后以这个轴值为划分基准将待排序序列分成比pivot大和比pivot小的两部分,接下来对划分完的子序列进行快排直到子序列为一个元素为止。

public void quickSort(int array[], int low, int high) {// 传入low=0,high=array.length-1;
int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。
if (low < high) {
p_pos = low;
pivot = array[p_pos];
for (i = low + 1; i <= high; i++)
if (array[i] > pivot) {
p_pos++;
t = array[p_pos];
array[p_pos] = array[i];
array[i] = t;
}
t = array[low];
array[low] = array[p_pos];
array[p_pos] = t;
// 分而治之
quickSort(array, low, p_pos - 1);// 排序左半部分
quickSort(array, p_pos + 1, high);// 排序右半部分
}


测试demo:

import java.util.Arrays;
public class sortTest {
// 冒泡排序
public void bubbleSort(int array[]) { int t = 0; for (int i = 0; i < array.length - 1; i++) for (int j = 0; j < array.length - 1 - i; j++) if (array[j] > array[j + 1]) { t = array[j]; array[j] = array[j + 1]; array[j + 1] = t; } }

// 选择排序
public void selectSort(int array[]) { int t = 0; for (int i = 0; i < array.length - 1; i++) for (int j = i + 1; j < array.length; j++) if (array[i] > array[j]) { t = array[i]; array[i] = array[j]; array[j] = t; } }

// 插入排序
public void insertionSort(int array[]) { int i, j, t = 0; for (i = 1; i < array.length; i++) { t = array[i]; for (j = i - 1; j >= 0 && t < array[j]; j--) array[j + 1] = array[j]; array[j + 1] = t; } }

// 分治法快速排序
public void quickSort(int array[], int low, int high) {// 传入low=0,high=array.length-1; int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。 if (low < high) { p_pos = low; pivot = array[p_pos]; for (i = low + 1; i <= high; i++) if (array[i] > pivot) { p_pos++; t = array[p_pos]; array[p_pos] = array[i]; array[i] = t; } t = array[low]; array[low] = array[p_pos]; array[p_pos] = t; // 分而治之 quickSort(array, low, p_pos - 1);// 排序左半部分 quickSort(array, p_pos + 1, high);// 排序右半部分 }
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = { 37, 47, 23, 100, 19, 56, 56, 99, 9 };
sortTest st = new sortTest();
// st.bubbleSort(array);
// st.selectSort(array);
// st.insertionSort(array);
st.quickSort(array, 0, array.length - 1);
System.out.println("排序后:" + Arrays.toString(array));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 排序算法