//冒泡排序
public class BubbleSortTest {
public static void main(String[] args) {
// int[] array = {1, 5, 8, 7, 6, 2};
// int[] array = {1, 2, 3, 4, 5, 6, 7, 9};
int[] array = {6, 5, 4, 2, 1, 0};
int[] sortedArray = bubbleSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(sortedArray[i] + " ");
}
}
private static int[] bubbleSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) { //array.length - 1是因为当后面n-1个元素固定后第一个元素自然就固定好了
boolean flag = true;//判断当前循环是否出现过交换
for (int j = 0; j < array.length - i - 1; j++) { //array.length - i - 1是因为最后i个元素已经固定好,其次我们比较的是两个数,所以最后还得-1
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
flag=false;
}
}
if (flag == true) {
break;//如果没有出现过交换,代表整个数组已经是顺序的,直接跳出循环
}
}
return array;
}
}
//选择排序
public class SelectionSortTest {
public static void main(String[] args) {
int[] array = {1, 5, 8, 7, 6, 2};
// int[] array = {1, 2, 3, 4, 5, 6, 7, 9};
// int[] array = {6, 5, 4, 2, 1, 0};
int[] sortedArray = selectionSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(sortedArray[i] + " ");
}
}
private static int[] selectionSort(int[] array) {
int min, temp, key ;//min存储当前循环最小值,key存储最小值的下标
for (int i = 0; i < array.length-1; i++) {
min = array[i];
key=i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < min) {
min = array[j];
key = j;
}
}
if(key!=i){
temp = array[i];
array[i] = array[key];
array[key] = temp;
}
}
return array;
}
}
//插入排序
public class InsertionSortTest {
public static void main(String[] args) {
// int[] array = {1, 5, 8, 7, 6, 2};
// int[] array = {1, 2, 3, 4, 5, 6, 7, 9};
int[] array = {6, 5, 4, 2, 1, 0};
int[] sortedArray = InsertionSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(sortedArray[i] + " ");
}
}
private static int[] InsertionSort(int[] array) {
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {//如果第i个大于第i-1个,表明这个数字顺序是不需要调整的
int temp=array[i];//保存当前第i个位置的值
int j;
for (j = i; j >0&&array[j-1]>temp ; j--) {
array[j]=array[j-1];//将所有大于temp的值向后移
}
array[j]=temp;//插入temp
}
}
return array;
}
}
public class ShellsSortTest {
public static void main(String[] args) {
// int[] array = {1, 5, 5, 7, 6, 2};
// int[] array = {1, 2, 3, 4, 5, 6, 7, 9};
// int[] array={8,9,1,7,2,3,5,4,6,0};
int[] array = {6, 5, 4, 2, 1, 0};
int[] sortedArray = ShellsSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(sortedArray[i] + " ");
}
}
public static int[] ShellsSort(int[] array){
int temp;
// 增量gap, 并逐步的缩小增量
for (int gap=array.length/2; gap >0 ; gap/=2) {
// 从第gap 个元素,逐个对其所在的组进行直接插入排序
for (int i = gap; i < array.length; i++) {
int j=i;
temp=array[i];
if(temp<array[j-gap]){
while (j-gap>=0&&array[j-gap]>temp){
array[j]=array[j-gap];
j-=gap;
}
}
array[j]=temp;
}
}
return array;
}
}
挖坑法划分:https://blog.csdn.net/morewindows/article/details/6684558(参考博客)
//快速排序
public class QuickSortTest {
public static void main(String[] args) {
int[] array = {1, 5, 5,7, 6, 2};
// int[] array = {8, 2, 3, 4, 5, 6, 7, 9};
// int[] array = {6, 5, 4, 2, 1, 0};
// int[] array = {5, 6, 3, 1, 8, 7, 99};
int[] sortedArray = quickSort(array, 0, array.length - 1);
for (int i = 0; i < array.length; i++) {
System.out.print(sortedArray[i] + " ");
}
}
/*
挖坑法进行一次划分:
1、通过将基准值作为坑,先向右找到小于基准值的数,将这个数填入坑中,而将这个数原来位置作为新坑
2、然后,再向左找到小于基准值的数,将这个数填入新坑中,而将这个数原来位置作为新坑
3、不断重复1、2两步,直至left==right,将基准值填入最后的坑
*/
private static int[] quickSort(int[] array, int l, int r) {
if (l < r) {
int left = l;//左指针确保left左边的数都小于基准值
int right = r;//右指针确保right右边的数都大于基准值
int temp = array[left];//保存基准值,并挖坑
while (left < right) {
//从右向左找到第一个小于基准数的数
while (left < right && array[right] >= temp) {
right--;
}
if (left < right) {
array[left++] = array[right];//填坑,并将array[right]作为新的坑
}
//从左向右找到第一个大于基准数的数
while (left < right && array[left] <= temp) {
left++;
}
if (left < right) {
array[right--] = array[left];//填坑,并将array[left]作为新的坑
}
}
array[right] = temp;//当left和right相等时,填入基准值
quickSort(array, l, right - 1);//向左重复
quickSort(array, left + 1, r);//向右重复
}
return array;
}
}
交换法:逻辑更简单
public class QuickSortTest2 {
public static void main(String[] args) {
int[] arr = {1, 3, 5,7, 6, 2};
// int[] arr = {1, 3, 5,5,7, 6, 2,2};
// int[] arr = {-9,78,0,23,-567,70, -1,900, 4561};
quickSort(arr,0,arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
//交换法
public static int[] quickSort(int arr[],int start,int end) {
int pivot = arr[start];
int i = start;
int j = end;
while (i<j) {
while ((i<j)&&(arr[j]>pivot)) {
j--;
}
while ((i<j)&&(arr[i]<pivot)) {
i++;
}
if ((arr[i]==arr[j])&&(i<j)) {
i++;
} else {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
if (i-1>start) arr=quickSort(arr,start,i-1);
if (j+1<end) arr=quickSort(arr,j+1,end);
return (arr);
}
}
图解:
//归并排序
public class MergeSortTest {
public static void main(String[] args) {
// int[] array = {1, 5, 7, 6, 2};
// int[] array = {6, 5, 4, 2, 1, 0};
// int[] array = {5, 6, 3, 1, 8, 7, 99};
int[] array = {8, 4, 5, 7, 1, 3, 6, 2};
int[] temp = new int[array.length];
mergeSort(array, 0, array.length - 1, temp);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
private static void mergeSort(int[] array, int left, int right, int[] temp) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(array, left, mid, temp);
mergeSort(array, mid + 1, right, temp);
merge(array, left, right, temp);
}
}
private static void merge(int[] array, int left, int right, int[] temp) {
int mid = (left + right) / 2;
int i = left;
int j = mid + 1;
int index = 0;
while (i <= mid && j <= right) {
if (array[i] <= array[j]) {
temp[index++] = array[i++];
} else {
temp[index++] = array[j++];
}
}
while (i <= mid) {
temp[index++] = array[i++];
}
while (j <= right) {
temp[index++] = array[j++];
}
index = 0;
while (left <= right) {
array[left++] = temp[index++];
}
}
}
图解:
//基数排序
public class RadixSortTest {
public static void main(String[] args) {
// int[] arr = {1, 3, 5, 7, 6, 2};
// int[] arr = {1, 3, 5,5,7, 6, 2,2};
// int[] arr = {-9,78,0,23,-567,70, -1,900, 4561};
int[] arr = {53, 3, 542, 748, 14, 214,1000};
radixSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
//注意,基数排序是不需要递归的
private static void radixSort(int[] array) {
int[][] bucket = new int[10][array.length];//使用最坏情况,arr.length防止溢出
int[] count = new int[10];//记录每个桶中元素数量
//找到最大值
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
//遍历出最大值的位数
int max_len = 0;
while (max != 0) {
max/=10;
max_len++;
}
for (int i = 0, n = 1; i < max_len; i++, n *= 10) {//通过位数确定排序次数
for (int j = 0; j < array.length; j++) {//将每个数放入对应的桶
int temp = array[j] / n % (10);
bucket[temp][count[temp]++] = array[j];
}
//将每次排序好的按桶顺序放回数组
int index=0;
for (int i1 = 0; i1 < 10; i1++) {
for (int j = 0; j < count[i1]; j++) {
array[index++] = bucket[i1][j];
}
count[i1] = 0;//把count数组清零
}
}
}
}