面試常見演算法-排序尋找演算法

來源:互聯網
上載者:User

標籤:插入排序   希爾排序   快速排序   二分尋找   堆排序   

演算法是程式員必被的一個技能,在面試中常常出現,下面總結了面試中出現的常見演算法,這些演算法程式員應該牢記在心中,要非常熟練。

插入排序演算法

原理:將數組分為無序區和有序區兩個區,然後不斷將無序區的第一個元素按大小順序插入到有序區中去,最終將所有無序區元素都移動到有序區完成排序。

要點:設立哨兵,作為臨時儲存和判斷數組邊界之用。

public class InsertSort {   private static void insertSort(int[] a) {       int j;       int tmp;       for (int i = 1; i < a.length; i++) {           tmp = a[i];           for (j = i; j > 0 && tmp < a[j - 1]; j--) {                a[j] = a[j - 1];           }           a[j] = tmp;       }    }}

希爾排序演算法

原理:又稱增量縮小排序。先將序列按增量劃分為元素個數相同的若干組,使用直接插入排序法進行排序,然後不斷縮小增量直至為1,最後使用直接插入排序完成排序。

要點:增量的選擇以及排序最終以1為增量進行排序結束。

public class ShellSort {    private static void shellSort(int[] a) {        int j;        int tmp;        for (int gap = a.length / 2; gap >0; gap /= 2) {            for (int i = gap; i < a.length;i++) {                tmp = a[i];                for (j = i; j >= gap && tmp< a[j - gap]; j -= gap) {                    a[j] = a[j - gap];                }                a[j] = tmp;            }        }    }}

冒泡排序演算法

原理:將序列劃分為無序和有序區,不斷通過交換較大元素至無序區尾完成排序。

要點:設計交換判斷條件,提前結束以排好序的序列迴圈。

public class BubbleSort {   private static void bubbleSort(int[] a) {        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]) {                    swap(a, j, j + 1);                }           }       }    }   private static void swap(int[] a, int x, int y) {       int tmp = a[x];       a[x] = a[y];       a[y] = tmp;    }}

快速排序演算法

原理:不斷尋找一個序列的中點,然後對中點左右的序列遞迴的進行排序,直至全部序列排序完成,使用了分治的思想。

要點:遞迴、分治

public class QuickSort {   private static void quickSort(int[] a) {       quickSort(a, 0, a.length - 1);    }   private static void quickSort(int[] a, int left, int right) {       if (left < right) {           int pivot = a[left];           int lo = left;           int hi = right;           while (lo < hi) {                while (lo < hi &&a[hi] >= pivot) {                    hi--;                }                a[lo] = a[hi];                while (lo < hi &&a[lo] <= pivot) {                    lo++;                }                a[hi] = a[lo];           }           a[lo] = pivot;           quickSort(a, left, lo - 1);           quickSort(a, lo + 1, right);       }    }}

簡單選擇排序演算法

原理:將序列劃分為無序和有序區,尋找無序區中的最小值和無序區的首元素交換,有序區擴大一個,迴圈最終完成全部排序

public class SelectSort {   private static void selectSort(int[] a) {       int idx;       for (int i = 0; i < a.length; i++) {           idx = i;           for (int j = i + 1; j < a.length; j++) {                if (a[idx] > a[j]) {                    idx = j;                }           }           swap(a, idx, i);       }    }   private static void swap(int[] a, int x, int y) {       int tmp = a[x];       a[x] = a[y];       a[y] = tmp;    }}

堆排序演算法

原理:利用大根堆或小根堆思想,首先建立堆,然後將堆首與堆尾交換,堆尾之後為有序區。

要點:建堆、交換、調整堆

public class HeapSort {   private static void heapSort(int[] a) {       // 先建立大堆,從第一個非葉子結點開始調整,然後調整第二個非葉子結點...       for (int i = a.length / 2; i >= 0 ; i--) {           shiftDown(a, i, a.length);       }       // 調整大堆,將最大的元素調整到未排好序的部分的末尾       for (int i = a.length - 1; i > 0 ; i--) {           swap(a, 0, i);           shiftDown(a, 0, i);       }    }   private static void shiftDown(int[] a, int i, int n) {       int child;       int tmp;       for (tmp = a[i]; i * 2 + 1 < n; i = child) {           child = i * 2 + 1;           if (child != n - 1 && a[child] < a[child + 1]) {                child++;           }           if (tmp < a[child]) {                a[i] = a[child];            } else {                break;           }       }       a[i] = tmp;    }   private static void swap(int[] a, int x, int y) {       int tmp = a[x];       a[x] = a[y];       a[y] = tmp;    }}

歸併排序演算法

原理:將原序列劃分為有序的兩個序列,然後利用歸併演算法進行合并,合并之後即為有序序列。

要點:歸併、分治

public class MergeSort {   private static void mergeSort(int[] a) {       int[] b = new int[a.length];       mergeSort(a, b, 0, a.length - 1);    }   private static void mergeSort(int[] a, int[] b, int left, int right) {       if (left < right) {           int center = left + (right - left) / 2;           mergeSort(a, b, left, center);           mergeSort(a, b, center + 1, right);           merge(a, b, left, center + 1, right);       }    }   private static void merge(int[] a, int[] b, int leftPos, int rightPos, intrightEnd) {       int leftEnd = rightPos - 1;       int tempPos = leftPos;       int numElements = rightEnd - leftPos + 1;        while (leftPos <= leftEnd && rightPos <= rightEnd) {           if (a[leftPos] <= a[rightPos]) {                b[tempPos] = a[leftPos];                tempPos++;                leftPos++;           } else {                b[tempPos] = a[rightPos];                tempPos++;                rightPos++;           }       }       while (leftPos <= leftEnd) {           b[tempPos] = a[leftPos];           tempPos++;           leftPos++;       }       while (rightPos <= rightEnd) {           b[tempPos] = a[rightPos];           tempPos++;           rightPos++;       }       for (int i = 0; i < numElements; i++, rightEnd--) {           a[rightEnd] = b[rightEnd];       }    }}

二分尋找演算法

public class BinarySearch {   public static int binarySearch(int[] a, int v) {       int mid;       int lo = 0;       int hi = a.length - 1;       while (lo <= hi) {           mid = lo + ((hi - lo) >>> 1); // 移位元運算的優先順序比較低,要用括弧           if (a[mid] == v) { // 已經找到                return mid;           } else if (a[mid] < v) { // 可能在右邊                lo = mid + 1;           } else { // 可能在左邊                hi = mid - 1;           }       }       return -1; // 未找到    }}

面試常見演算法-排序尋找演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.