首页 > 编程语言 >常见排序算法整理

常见排序算法整理

时间:2022-10-27 04:44:05浏览次数:53  
标签:sort www nums int 常见 gap 算法 https 排序

 

import java.util.Random;

public class Sorting {

    /**
     *  For each element, compare with all the elements before it and swap position accordingly
     *  https://www.toptal.com/developers/sorting-algorithms/insertion-sort
     *  https://www.geeksforgeeks.org/insertion-sort/
     */
    public int[] insertionSort(int[] nums){
        int n = nums.length;
        //starting from i=1
        for(int i = 1; i < n; i++) {
            //save the current number to key
            int key = nums[i];
            //keep shifting the element to find the place for "insertion"
            int j = i - 1;
            while (j >= 0 && nums[j] > key) {
                nums[j + 1] = nums[j];
                j--;
            }
            //place the current element
            nums[j + 1] = key;
        }
        return nums;
    }

    /**
     * For each element, search all the elements after it to find the minimal one and swap with it.
     * https://www.toptal.com/developers/sorting-algorithms/selection-sort
     * https://www.geeksforgeeks.org/selection-sort/
     */
    public int[] selectionSort(int[] nums){
        int n = nums.length;
        //starting from i=0
        for(int i=0;i<n-1;i++) {
            //define cur as minIdx and update minIdx for all the elements after cur element
            int minIdx = i;
            for(int j = i+1;j<n;j++){
                if(nums[j] < nums[minIdx]){
                    minIdx = j;
                }
            }
            //swap cur with the minimal after it
            swap(nums,i,minIdx);
        }
        return nums;
    }

    /**
     * For each round, compare adjacent element for all the unsorted elements to pop up one maximum element.
     * https://www.toptal.com/developers/sorting-algorithms/bubble-sort
     * https://www.geeksforgeeks.org/bubble-sort/
     */
    public int[] bubbleSort(int[] nums){
        int n = nums.length;
        //if no swap for this iteration, sort can exit earlier
        boolean swapped;
        for(int i=0;i<n-1;i++){
            swapped = false;
            //for all the elements before cur, swap with adjacent element
            for(int j=0;j<n-1-i;j++){
                if(nums[j]>nums[j+1]){
                    swap(nums,j,j+1);
                    swapped = true;
                }
            }
            if(!swapped){
                break;
            }
        }
        return nums;
    }

    /**
     * Shell sort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position ahead.
     * When an element has to be moved far ahead, many movements are involved. The idea of ShellSort is to allow the
     * exchange of far items. In Shell sort, we make the array h-sorted for a large value of h.
     * We keep reducing the value of h until it becomes 1
     *
     * https://www.toptal.com/developers/sorting-algorithms
     * https://www.geeksforgeeks.org/shellsort/
     */
    public int[] shellSort(int[] nums){
        int n = nums.length;
        //define the gap and then reduce the gap
        for(int gap = n/2; gap>0; gap /=2){
            //Do a gapped insertion sort for this gap size;
            for(int i = gap; i< n ; i++){
                //for each element, find the correct place to put it
                int temp = nums[i];
                int j;
                for(j = i; j>=gap && nums[j-gap] > temp; j -=gap){
                    nums[j] = nums[j-gap];
                }
                nums[j] = temp;
            }
        }
        return nums;
    }

    /**
    * A recursive algorithm continuously splits the array in half until it cannot be further divided.
    * Finally, when both halves are sorted, the merge operation is applied.
    * https://www.toptal.com/developers/sorting-algorithms/merge-sort
    * https://www.geeksforgeeks.org/merge-sort/
    **/
    public int[] mergeSort(int[] nums){
        int n = nums.length;
        mergeSort(nums,0,n-1);
        return nums;
    }

    private void mergeSort(int[] nums, int l, int r){
        if(l<r){
            int m = l +(r-l)/2;
            mergeSort(nums,l,m);
            mergeSort(nums,m+1,r);
            merge(nums,l,m,r);
        }
    }

    //merge subarray nums[l,m] and nums[m+1,r]
    private void merge(int[] nums, int l, int m, int r){
        int n1 = m-l+1;
        int n2 = r-m;

        int[] lTmp = new int[n1];
        int[] rTmp = new int[n2];
        //copy into tmp arrays
        for(int i=0;i<n1;i++){
            lTmp[i] = nums[l+i];
        }
        for(int j=0;j<n2;j++){
            rTmp[j] = nums[m+1+j];
        }

        //merge, be careful k = l
        int i =0, j =0;
        int k = l;
        while(i<n1 && j<n2){
            if(lTmp[i] <= rTmp[j]){
                nums[k++] = lTmp[i++];
            }else{
                nums[k++] = rTmp[j++];
            }
        }
        while(i<n1){
            nums[k++] = lTmp[i++];
        }
        while(j<n2){
            nums[k++] = rTmp[j++];
        }
    }

    /**
     * Build a max heap and then keeps popping out the max
     * https://www.toptal.com/developers/sorting-algorithms/heap-sort
     * https://www.geeksforgeeks.org/heap-sort/
     * https://www.cnblogs.com/jdflyfly/p/3913571.html
     *
    */
    public int[] heapSort(int[] nums){
        //be careful with the index here, n,n-1
        int n = nums.length;
        buildHeap(nums);
        //swap cur with element 0 and then heapify 0
        for(int i = n-1;i>0;i--){
            swap(nums,i,0);
            heapify(nums,--n,0);
        }
        return nums;
    }

    private void buildHeap(int[] nums){
        //use heapify to build heap
        for(int i=nums.length/2; i>=0; i--){
            heapify(nums,nums.length,i);
        }
    }

    private void heapify(int[] nums, int n, int i){
        int l = 2*i+1;
        int r = 2*i+2;
        //define the largest and update it
        int largest = i;
        if(l<n && nums[l] > nums[i]){
            largest = l;
        }
        if(r<n && nums[r] > nums[largest]){
            largest = r;
        }
        if(largest != i){
            //if swapped with one child, continue to heapify
            swap(nums,largest,i);
            heapify(nums,n,largest);
        }

    }

    /**
     * 快排算法核心的部分便是partition过程,这里的partition采取最后一个元素作为pivot,也可以采用其他方法决定pivot然后跟last element交换
     * https://www.cnblogs.com/jdflyfly/p/3897331.html
     * https://www.toptal.com/developers/sorting-algorithms/quick-sort
     * https://www.geeksforgeeks.org/quick-sort/
     */
    Random random = new Random();
    public int[] quickSort(int[] nums){
        qSort(nums,0,nums.length-1);
        return nums;
    }

    private void qSort(int[] a, int p, int r){
        if(p<r){
            int q = partition(a,p,r);
            qSort(a,p,q-1);
            qSort(a,q+1,r);
        }
    }

    private int partition(int[] a, int p, int r){
        //x is the privot element, i points to the last small element, j scans all the elements
        int randomIdx = random.nextInt(r-p+1) + p;
        swap(a,randomIdx, r);
        int x = a[r];
        int i = p-1;
        int j = p;
        for(j = p; j<r; j++){
            if(a[j]<=x){
                i++;
                swap(a,i,j);
            }
        }
        swap(a,i+1,r);
        return i+1;
    }

    private void swap(int[] nums, int i, int j){
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

}

  

标签:sort,www,nums,int,常见,gap,算法,https,排序
From: https://www.cnblogs.com/jdflyfly/p/16830729.html

相关文章

  • 代码随想录算法训练营带三期第一天 | 704. 二分查找,27. 移除元素
    第一天打卡,从C++,python往java和go的方向转变。一刷leetcode以前学过的算法都不记得了,错了好几次看了官方文档才改过来的。 第二题非常难懂具体是什么意思我还在空间......
  • C++算法之旅、02 从木棒切割问题领悟二分法精髓
    172、木棒切割问题https://sunnywhy.com/problem/172题目描述给出n根木棒的长度,现在希望通过切割它们来得到至少k段长度相等的木棒(长度必须是整数),问这些长度相等的木......
  • K-近邻算法
    1.简介K-近邻算法(K-NearestNeighbor,KNN),属于监督学习,是一中基本分类与回归方法。k近邻法的输入为实例的特征向量,对应于特征空间的点;输出为实例的类别,可以取多类,k......
  • Mybatis常见知识点
    Mybatis常见知识点${}和#{}有什么区别${}是简单的字符串替换,属于静态文本替换,并且并不是在预处理的时候进行替换,实在编译的时候进行替换,可能会存在sql注入的风险。替换......
  • 代码随想录算法训练营第一天|704、二分查找|27、移除元素
    704.二分查找·这是三个数的故事left,middle,right题目链接:https://leetcode.cn/problems/binary-search/前提:数组有序  小->大   数组无重复数   使用语......
  • 算法 第四版 电子书 pdf
    作者:[美]RobertSedgewick/[美]KevinWayne出版社:人民邮电出版社原作名:Algorithms译者:谢路云 链接:算法第四版  本书作为算法领域经典的参考书,全面介......
  • 基于GA优化的竞价博弈频谱分配算法的matlab仿真
    目录一、理论基础二、核心程序三、仿真测试结果作者ID:fpga和matlabCSDN主页:https://blog.csdn.net/ccsss22?type=blog擅长技术:1.无线基带,无线图传,编解码2.机器视觉......
  • Acwing 快速排序
    基于分治的思想,每次划分后,保证基准(x)前的数都比基准小,其后的树都比基准大即可。#include<iostream>usingnamespacestd;constintN=100010;intq[N];voidquic......
  • 基于形态学处理的交通标志检测分割算法matlab仿真
    目录一、理论基础二、核心程序三、仿真测试结果作者ID:fpga和matlabCSDN主页:https://blog.csdn.net/ccsss22?type=blog擅长技术:1.无线基带,无线图传,编解码2.机器视觉......
  • 田忌赛马(带索引的数组排序)
    870. AdvantageShuffleMedium27821FavoriteShareGiventwoarrays ​​A​​​ and ​​B​​ ofequalsize,the advantageof ​​A​​ withrespectto ​​......