对常见的几种排序算法进行了实现,上传,方便后续复习。
public class SortDemo {
static int[] arrayInit;
public static void main(String[] args) {
//创建数组
createArrayNum();
//存储未排序数组,方便后续多种排序使用
int[] arrayDisorder = arrayInit;
//打印初始数组
printArray(arrayInit, "初始数组:");
//直接选择排序
directSort(arrayDisorder);
//冒泡排序
bubbleSort(arrayDisorder);
//直接插入排序结果
insertSor(arrayDisorder);
//快排
printArray(quickSort(arrayDisorder, 0, arrayDisorder.length - 1), "快速排序结果为:");
}
/**
* 随机生成数组大小(10~20)和内容(0~100)
*/
public static void createArrayNum() {
//生成大小
int arrayLenth = (int) (Math.random() * 10 + 10);
arrayInit = new int[arrayLenth];
//初始化内容
for (int i = 0; i < arrayInit.length; i++) {
//随机生成0~100的整数
arrayInit[i] = (int) (Math.random() * 100);
}
}
/**
* 打印数组
*
* @param array
*/
public static void printArray(int[] array, String str) {
//存储数组内容
String arrayStr = "{";
//遍历拼接为字符串
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1) {
arrayStr += array[i] + "}";
System.out.println(str + arrayStr);
} else {
arrayStr += array[i] + ",";
}
}
}
/**
* 直接选择排序,时间复杂度O(N^2)
*
* @param array
*/
public static void directSort(int[] array) {
//temp方便数组交换位置;tempIndex记录每次获取的最小数的下标
int temp, tempIndex;
for (int i = 0; i < array.length; i++) {
tempIndex = i;
//遍历找出i~array.length中最小的数,记录其下标
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[tempIndex]) {
tempIndex = j;
}
}
//若所得最小数不是下标为i的数,则交换两数位置,确保每次循环后i上的数为剩余数的最小值
if (tempIndex != i) {
temp = array[tempIndex];
array[tempIndex] = array[i];
array[i] = temp;
}
}
printArray(array, "直接选择排序后:");
}
/**
* 冒泡排序,时间复杂度:最好O(N),最坏O(N^2),平均O(N^2)
*
* @param array
*/
public static void bubbleSort(int[] array) {
int temp;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
//相邻两数比较,把大的数往后移
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printArray(array, "冒泡排序后:");
}
/**
* 直接插入排序,时间复杂度:最好O(N),最坏O(N^2),平均O(N^2)
*
* @param array
*/
public static void insertSor(int[] array) {
int temp;
int j;
//初始array[0]自成有序数组,故从1开始遍历
for (int i = 1; i < array.length; i++) {
//存储下标为i的值,以防后续操作中丢失
temp = array[i];
//将下标为i的数同下标小于i的数比较,将其放到合适的位置(左边的都比其小,右边的都比其大)
j = i - 1;
//array[i]和有序数组中的相比,将比其大的往后挪
while (j >= 0 && (array[j] > temp)) {
//往后挪一位,下次再比较应与j的前一位相比,故j--
array[j + 1] = array[j];
j--;
}
//若不满足while条件,则左侧的数都比array[i]小,应将array[i]放在下标为j+1出(比较后j<0或j处数小于array[i])
array[j + 1] = temp;
}
printArray(array, "直接插入排序结果:");
}
/**
* 快排,时间复杂度O(NlogN)
*
* @param array
* @param start
* @param end
* @return
*/
public static int[] quickSort(int[] array, int start, int end) {
//temp方便数组元素互换位置;start_temp复制起点;
// end_temp复制终点;arrayReference作为参考数
int temp, start_temp, end_temp, arrayReference;
start_temp = start;
end_temp = end;
arrayReference = array[start];
//将比参考数小的或等于它的放到它的左边,比参考数大的放在它的右边,
// 然后对左右两边分别执行上述操作
while (start < end) {
//从后往前找,大于参考数的不动
while ((start < end) && (array[end] > arrayReference)) {
end--;
}
//从前往后找,小于参考数的不动
while ((start < end) && (array[start] < arrayReference)) {
start++;
}
//若进行上述操作后start<end且array[start]==array[end](前后相等特殊情况,无互换的必要)则start+1后继续,
// 否则互换两者位置(确保将小于或等于参考值的换到前面,大于参考值的换到后面)
if ((start < end) && (array[start] == array[end])) {
start++;
} else {
temp = array[start];
array[start] = array[end];
array[end] = temp;
}
}
//当start==end时,该位置上的数必是上面的参考数,其左边的数必<=参考数
//对参考数左边的数重复上述操作,直至顺序正确
if (start - 1 > start_temp) {
array = quickSort(array, start_temp, start - 1);
}
//对参考数右边的数重复上述操作,直至顺序正确
if (end + 1 < end_temp) {
array = quickSort(array, end + 1, end_temp);
}
return array;
}
}