冒泡排序
import java.util.Arrays;
public class Main {
static int temp;
public static void main(String[] args) {
int[] nums = {12,15,13,46,87,164,454,22,45,65};
Bubbing(nums);//调用BBubbing方法
}
public static void Bubbing(int[] nums){
//排序前的数组
System.out.println("排序前数组序列:");
System.out.println(Arrays.toString(nums));//[12, 15, 13, 46, 87, 164, 454, 22, 45, 65]
//冒泡排序
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length-i-1; j++) {
if(nums[j]>nums[j+1]){
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
//遍历输出排序后的数组
System.out.println("排序后数组序列:");
System.out.println(Arrays.toString(nums));//[12, 13, 15, 22, 45, 46, 65, 87, 164, 454]
}
}
选择排序
import java.util.Arrays;
public class Main {
static int temp;
public static void main(String[] args) {
int[] nums = {12,15,13,46,87,164,454,22,45,65};
Select(nums, nums.length);
}
public static void Select(int [] nums,int len){
for (int i = 0; i < len-1; i++) {
int index =i;
int j;
for (j = i+1; j < len; j++) {
if(nums[j] < nums[index]){
index = j;
}
}
int temp = nums[index];
nums[index] = nums[i];
nums[i] = temp;
System.out.println(Arrays.toString(nums));
//[12, 15, 13, 46, 87, 164, 454, 22, 45, 65]
//[12, 13, 15, 46, 87, 164, 454, 22, 45, 65]
//[12, 13, 15, 46, 87, 164, 454, 22, 45, 65]
//[12, 13, 15, 22, 87, 164, 454, 46, 45, 65]
//[12, 13, 15, 22, 45, 164, 454, 46, 87, 65]
//[12, 13, 15, 22, 45, 46, 454, 164, 87, 65]
//[12, 13, 15, 22, 45, 46, 65, 164, 87, 454]
//[12, 13, 15, 22, 45, 46, 65, 87, 164, 454]
//[12, 13, 15, 22, 45, 46, 65, 87, 164, 454]
}
}
}
插入排序
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] num = {45,12,3,56,78,13,456,123,2};
insertionSort(num);
System.out.println(Arrays.toString(num));
}
public static int[] insertionSort(int []arr) {
int len = arr.length;
int preIndex, current;
for (int i = 1; i < len; i++) {
//获取下标前一个值
preIndex = i - 1;
//获取当前下标的值
current = arr[i];
//判断,下标大于零,按升序排序
while (preIndex >= 0 && arr[preIndex] > current) {
//因为已经取出当前下标的值,这里只需就前面的值往后移动
arr[preIndex + 1] = arr[preIndex];
//每次赋值完成,下标进行--;进行下一次的判断,赋值
preIndex--;
}
arr[preIndex + 1] = current;
}
return arr;
}
}
[12, 45, 3, 56, 78, 13, 456, 123, 2]
[3, 12, 45, 56, 78, 13, 456, 123, 2]
[3, 12, 45, 56, 78, 13, 456, 123, 2]
[3, 12, 45, 56, 78, 13, 456, 123, 2]
[3, 12, 13, 45, 56, 78, 456, 123, 2]
[3, 12, 13, 45, 56, 78, 456, 123, 2]
[3, 12, 13, 45, 56, 78, 123, 456, 2]
[2, 3, 12, 13, 45, 56, 78, 123, 456]
快速排序
public static void main(String[] args) {
int[] arr = new int[]{45,12,3,56,78,13,456,123,2};
//快速排序,传入数组,最开始的位置
quickSort(arr,0,arr.length-1);
//打印输出
printArr(arr);
}
public static void quickSort(int[] arr,int left,int right){
//判断左边的下标是否和右边的相等,等于的时候就结束程序
if (left < right){
//将数组分为两个部分,arr[left..pivot]和arr[pivot...right],pivot为基准数
//接收基准数所在的下标
int mid = get_mid(arr, left, right);
//递归的对左边序列进行排序,使有序
quickSort(arr,left,mid-1);
//递归的对右边序列进行排序,使有序
quickSort(arr,mid+1,right);
}
}
public static int get_mid(int[] arr,int left,int right){
//以left下标的值为基准数,此时left的位置相当于是空的,可以往里面赋值
int pivot = arr[left];
//当left与right相遇的时候,循环结束
while (left < right){
//遍历右边的数
//如果右边的数大于基准数,说明符合,将下标right--
while (arr[right] >= pivot && left < right)right--;
//当右边的数没有大于基准数时,将aa[right]的值赋给上面的挖空的位置
arr[left] = arr[right];
//遍历左边的数
//如果左边的数小于基准数,说明符合,将left++
while (arr[left] <= pivot && left < right)left++;
//如果左边的数大于基准数,说明这个应该出现在基准数的右边,将arr[left]的值赋给arr[right]的位置
arr[right] = arr[left];
}
//循环结束后,中间的一个位置的值是空的,这时候就需要将基准数的值赋到这个位置,可以满足左边的数都比基准数小
arr[left] = pivot;
//返回left小标,作为下一次循环的基准数的下标
return left;
}
private static void printArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}