几种排序算法的具体实现
1.快排
随机找基准,将小于基准的数放左边,大雨基准的数放右边,不停迭代,首先从两个数开始
//时间复杂度O(n*logn),空间复杂度O(n*logn)
public class QuickSortDemo {
public static void quickSort(int[] arr, int startIndex, int endIndex) {
if (startIndex < endIndex) {
//找出基准
int partition = partition(arr, startIndex, endIndex);
//分成两边递归进行
quickSort(arr, startIndex, partition - 1);
quickSort(arr, partition + 1, endIndex);
}
}
//找基准
private static int partition(int[] arr, int startIndex, int endIndex) {
int pivot = arr[startIndex];
int left = startIndex;
int right = endIndex;
while (left != right) {
while (left < right && arr[right] > pivot) {
right--;
}
while (left < right && arr[left] <= pivot) {
left++;
}
//找到left比基准大,right比基准小,进行交换
if (left < right) {
swap(arr, left, right);
}
}
//第一轮完成,让left和right重合的位置和基准交换,返回基准的位置
swap(arr, startIndex, left);
return left;
}
//两数交换
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] a = {2, 4, 6, 1, 3, 7, 9, 8, 5};
quickSort(a, 0, a.length - 1);
System.out.println(Arrays.toString(a));
}
}
2.插入排序,时间复杂度为O(n*2)
每趟将一个数放入已经排好序的序列
public class InsertionDemo {
public static void main(String[] args) {
int[] arrs = InsertDemo(new int[]{34,8,64,51,32,21});
for (int arr : arrs) {
System.out.print(arr+" ");
}
}
public static int[] InsertDemo(int[] nums){
int p = nums.length-1;
for(int i = 1;i<=p;i++){
int tmp = nums[i];
for(int j = i;j>0&&compareTo(tmp,nums[j-1])<0;j--){
//比较每趟的数与前一数的大小,如小于,则此数前移,前数右移
nums[j]=nums[j-1];
nums[j-1]=tmp;
}
}
return nums;
}
//交换两数的大小
public static int compareTo(int x,int y){
if(x>=y){
return 1;
}else {
return -1;
}
}
}
3.归并排序
时间复杂度O(n*logn),合并两个已经排好序的表
public class MergeSortDemo {
public static void main(String[] args) {
int[] array=new int[]{34,8,64,51,32,21};
if (array == null || array.length == 0)
return;
int[] temp = new int[array.length];
MergeSort(array, 0, array.length - 1, temp);
}
//归并
public static void MergeSort(int[] array,int first,int last,int[] temp){
if(first<last){
int mid=(first+last)/2;
MergeSort(array,first,mid,temp);// 递归归并左边元素
MergeSort(array, mid + 1, last, temp); // 递归归并右边元素
mergeArray(array, first, mid, last, temp); // 再将二个有序数列合并
}
}
//合并两个有序数组
public static void mergeArray(int[] array,int first,int mid,int last,int temp[]){
//把array分成两个表
int i=first,j=mid+1;
int k=0;
while(i<mid+1&&j<=last){
if(array[i]<=array[j]){
temp[k++]=array[i++];
}else{
temp[k++]=array[j++];
}
}
while (i<mid+1){
temp[k++]=array[i++];
}
while (j<last){
temp[k++]=array[j++];
}
for(i=0;i<k;i++){
array[i]=temp[i];
}
//return array;
}
}
4. 希尔排序
时间复杂度O(n*2)
public class ShellSortDemo {
public static void main(String[] args) {
int[] arrs = ShellSort(new int[]{34,8,64,51,32,21});
for (int arr : arrs) {
System.out.print(arr+" ");
}
}
public static int[] ShellSort(int[] nums){
for(int step=nums.length/2;step>0;step/=2){
for(int i = step;i<nums.length;i++){
int tmp=nums[i];
int j=i;
int y=0;
while (j-step>=0&&tmp<nums[j-step]){
nums[j]=nums[j-step];
j=j-step;
}
nums[j]=tmp;
}
}
return nums;
}
}
5.冒泡
public class BubbleSortDemo {
public static void main(String[] args) {
int[] arrs = BubbleSort(new int[]{34,8,64,51,32,21});
System.out.println(Arrays.toString(arrs));
}
public static int[] BubbleSort(int[] nums){
int m=nums.length;
int tmp=0;
for(int i=0;i<m-1;i++){
for(int j=0;j<m-1-i;j++){
if(nums[j]>nums[j+1]){
tmp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=tmp;
}
}
}
return nums;
}
}