美文网首页
Java-四种面试常考排序

Java-四种面试常考排序

作者: 在南方的北方人_Elijah | 来源:发表于2017-03-02 00:56 被阅读1605次

快速排序

快速排序的思想依据是分治法,选取第一个元素为对比值,然后将表中比对比值小的放在左边,将对比值大的放在右边,然后再将左边的列表用同样的方法进行排列,将右边的方法进行排列,依次递归下去,最终有序。下面列出java实现

package arithmetic;

/**
 * Created by elijahliu on 2017/3/2.
 */
public class QuickSort {
    int a[] = {49,37,65,97,76,13,27,49,78};

    public QuickSort(){

        quickSort(a,0,a.length-1);

        for (int i:a
             ) {
            System.out.print(i+" ");

        }


    }
    public int getMiddle(int[] list,int low,int high){
        int temp = list[low];
        while (low < high) {
            while (low < high && list[high] >= temp) {
                high--;
            }
            list[low] = list[high];
            while (low < high && list[low] <= temp) {
                low++;
            }
            list[high] = list[low];
        }
        list[low] = temp;
        return low;
    }

    public void _quickSort(int[] list, int low, int high) {

        if (low < high) {
            int mid = getMiddle(list, low, high);
            _quickSort(list, low, mid - 1);
            _quickSort(list, mid + 1, high);
        }
    }

    public void quickSort(int[] list, int low, int high) {
        if (list.length > 0) {
            _quickSort(list, low, high);
        }
    }

    public static void main(String[] args) {
        new QuickSort();
    }

}

直接插入排序

直接插入排序的算法思想:从第二个元素开始,如果比前面的元素都小,那么就将前面的元素往后移一位,然后把自己的值放到前面的位置上,最终由小到大排列。
java实现

package arithmetic;

/**
 * Created by elijahliu on 2017/3/2.
 */
public class InsertSort {
    public InsertSort(){
        insertSort();

    }

    int a[] = {49, 38, 65, 97, 76, 13, 27, 49};

    public void insertSort(){
        int temp = 0;
        for(int i = 1;i<a.length;i++) {
            temp = a[i];
            int j = i - 1;
            for(;j>=0&&temp<a[j];j--) {
                a[j + 1] = a[j];
            }
            a[j + 1] = temp;
        }
        for (int i:a
             ) {
            System.out.println(i+" ");
        }

    }

    public static void main(String[] args) {
        new InsertSort();
    }
}

冒泡排序

最简单的冒泡排序,大一就学过了

package arithmetic;

/**
 * Created by elijahliu on 2017/3/2.
 */
public class BubbleSort {

    public BubbleSort() {
        bubbleSort();
    }

    int a[] = {49, 38, 65, 97, 76, 13};

    public void bubbleSort() {
        int temp = 0;
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }

        for (int i : a
                ) {
            System.out.println(i + "");

        }
    }

    public static void main(String[] args) {
        new BubbleSort();
    }

}

简单选择排序

算法思想:选取第一个作为标准值,然后对比后面的数,选取一个最大的,与第一个进行交换,position用于标记最大的数的位置。然后选择第二个数为标准值,然后依次进行下去。

package arithmetic;

/**
 * Created by elijahliu on 2017/3/2.
 */
public class SelectSort {
    int a[] = {1, 54, 6, 3, 78, 34, 12, 45};

    public SelectSort() {
        seletctSort();
    }

    public void seletctSort() {
        int position = 0;
        for (int i = 0; i < a.length; i++) {
            position = i;
            int temp = a[i];
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] > temp) {
                    temp = a[j];
                    position = j;
                }
            }
            a[position] = a[i];
            a[i] = temp;
        }

        for (int i : a
                ) {
            System.out.print(i + " ");
        }

    }

    public static void main(String[] args) {
        new SelectSort();

    }
}

相关文章

  • Java-四种面试常考排序

    快速排序 快速排序的思想依据是分治法,选取第一个元素为对比值,然后将表中比对比值小的放在左边,将对比值大的放在右边...

  • 图文详解面试常考算法「拓扑排序」

    前言 Topological sort 又称 Topological order,这个名字有点迷惑性,因为拓扑排序...

  • java-四种排序算法

    复习java基础的时候,顺手写的。代码比较丑……1、直接插入排序 2、希尔排序 3、简单选择排序 4、堆排序

  • 常用排序算法(一)

    排序是笔试面试的常考知识点,检验应聘者对排序这类基本算法的接受程度和数据结构的理解。在2017春季暑期实习生招聘过...

  • Java-线程池专题 (美团面试题)

    Java-线程池专题 (美团面试题)

  • 五种常见排序算法实现(Java)

    Java-五种排序算法实现 前言及准备 这篇我们会介绍比较简单的五种排序算法:插入排序、冒泡排序、快速排序、选择排...

  • Swift 3.0实现面试常考的排序算法

    1.插入排序 ⒈ 从第一个元素开始,该元素可以认为已经被排序⒉ 取出下一个元素,在已经排序的元素序列中从后向前扫描...

  • Android面试常考

    1. 请描述一下Activity 生命周期。 答: 如下图所示。共有七个周期函数,按顺序分别是: onCreate...

  • 面试常考(python)

    Python语言特性 1.Python的函数参数传递 2.元类 metaclass metaclass 允许创建类...

  • 面试常考总结

    css BFC Block formatting context 块级格式上下文形成独立的渲染区域内部元素的渲染不...

网友评论

      本文标题:Java-四种面试常考排序

      本文链接:https://www.haomeiwen.com/subject/hcfhgttx.html