專題 查找與排序的Java代碼實現(一)


專題 查找與排序的Java代碼實現(一)

查找(Searching)

線性查找(linear search)

屬於無序查找算法,適合於存儲結構為順序存儲或鏈接存儲的線性表。
基本思想:從數據結構線形表的一端開始,順序掃描,依次將掃描到的結點關鍵字與給定值k相比較,若相等則表示查找成功;若掃描結束仍沒有找到關鍵字等於k的結點,表示查找失敗。
時間復雜度:O(n)
具體代碼:

    //-------------------------------------------------------------------------
    //  Searches the specified array of objects using a linear search 線性查找
    //  algorithm. Returns null if the target is not found.
    //-------------------------------------------------------------------------
    public static Comparable linearSearch (Comparable[] data,
                                           Comparable target) {
        Comparable result = null;
        int index = 0;

        while (result == null && index < data.length){
            if (data[index].compareTo(target) == 0)
                result = data[index];
            index++;
        }

        return result;
    }

二分查找(binary search)

屬於有序查找算法,元素必須是有序的,如果是無序的則要先進行排序操作。
基本思想:用給定值k先與中間結點的關鍵字比較,中間結點把線形表分成兩個子表,若相等則查找成功;若不相等,再根據k與該中間結點關鍵字的比較結果確定下一步查找哪個子表,這樣遞歸進行,直到查找到或查找結束發現表中沒有這樣的結點。
時間復雜度:O(log2n)
具體代碼:

    //--------------------------------------------------------------------------
    //  Searches the specified array of objects using a binary search 二分查找
    //  algorithm. Returns null if the target is not found.
    //--------------------------------------------------------------------------
    public static Comparable BinarySearch(Comparable[] data,
                                          Comparable target){
        Comparable result = null;
        int first = 0, last = data.length-1, mid;

        while (result == null && first <= last){
            mid = (first + last) / 2; // determine midpoint
            if (data[mid].compareTo(target)==0)
                result = data[mid];
            else
                if (data[mid].compareTo(target) == 0)
                    result = mid - 1;
                else
                    first = mid + 1;
        }

        return result;
    }

排序(Sorting)

選擇排序(selection sort)

基本思想:對一個數列進行排序時,每次從剩余的序列中挑選出最小的記錄,放到序列開始位置,以此類推,直到數列的所有數字都已經放到最終位置為止。
時間復雜度:O(n^2)
具體代碼:

//-----------------------------------------------------------------
    //  Sorts the specified array of integers using the selection
    //  sort algorithm.
    //-----------------------------------------------------------------
    public static void selectionSort (Comparable[] data) {
        int min;

        for (int index = 0; index < data.length-1; index++) {
            min = index;
            for (int scan = index+1; scan < data.length; scan++)
                if (data[scan].compareTo(data[min]) < 0)
                    min = scan;

            swap (data, min, index);
        }
    }

    //-----------------------------------------------------------------
    //  Swaps two elements in the specified array.
    //-----------------------------------------------------------------
    private static void swap (Comparable[] data, int index1, int index2)
    {
        Comparable temp = data[index1];
        data[index1] = data[index2];
        data[index2] = temp;
    }

插入排序(insertion sort)

基本思想:每次從數列中取一個還沒有取出過的數,並按照大小關系插入到已經取出的數中使得已經取出的數仍然有序。
時間復雜度:O(n^2)
具體代碼:

//-----------------------------------------------------------------
    //  Sorts the specified array of objects using an insertion
    //  sort algorithm.
    //-----------------------------------------------------------------
    public static void insertionSort (Comparable[] data)
    {
        for (int index = 1; index < data.length; index++)
        {
            Comparable key = data[index];
            int position = index;

            // Shift larger values to the right
            while (position > 0 && data[position-1].compareTo(key) > 0)
            {
                data[position] = data[position-1];
                position--;
            }

            data[position] = key;
        }
    }

冒泡排序(bubble sort)

屬於交換排序
基本思想:經過一趟冒泡排序之后,序列中最大的記錄到了序列最后,而較小的記錄位置均向前移動了
時間復雜度:O(n^2)
具體代碼:

 //-----------------------------------------------------------------
    //  Sorts the specified array of objects using a bubble sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void bubbleSort (Comparable[] data)
    {
        int position, scan;

        for (position = data.length - 1; position >= 0; position--)
        {
            for (scan = 0; scan <= position - 1; scan++)
                if (data[scan].compareTo(data[scan+1]) > 0)
                    swap (data, scan, scan+1);
        }
    }

快速排序(quick sort)

屬於交換排序,快速排序是對冒泡排序的一種改進。
基本思想:將待排序序列分成兩部分,其中一部分的記錄都比另一部分的記錄小,隨后分別對這兩部分再分成兩部分,使一部分的記錄都小於另一部分,如此反復最終使整個序列最終有序。
時間復雜度:O(n*log2n)
具體代碼:

//-----------------------------------------------------------------
    //  Sorts the specified array of objects using the quick sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void quickSort (Comparable[] data, int min, int max)
    {
        int pivot;

        if (min < max)
        {
            pivot = partition (data, min, max);  // make partitions
            quickSort(data, min, pivot-1);  // sort left partition
            quickSort(data, pivot+1, max);  // sort right partition
        }

    }

    //-----------------------------------------------------------------
    //  Creates the partitions needed for quick sort.
    //-----------------------------------------------------------------
    private static int partition (Comparable[] data, int min, int max)
    {
        // Use first element as the partition value
        Comparable partitionValue = data[min];

        int left = min;
        int right = max;

        while (left < right)
        {
            // Search for an element that is > the partition element
            while (data[left].compareTo(partitionValue) <= 0 && left < right)
                left++;

            // Search for an element that is < the partitionelement
            while (data[right].compareTo(partitionValue) > 0)
                right--;

            if (left < right)
                swap(data, left, right);
        }

        // Move the partition element to its final position
        swap (data, min, right);

        return right;
    }

歸並排序(merge sort)

基本思想:重復調用歸並算法,首先將單個記錄視為一個有序序列,然后不斷將相鄰的兩個有序序列合並得到新的有序序列,如此反復,最后得到一個整體有序的序列
時間復雜度:O(n*log2n)
具體代碼:

    //-----------------------------------------------------------------
    //  Sorts the specified array of objects using the merge sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void mergeSort (Comparable[] data, int min, int max)
    {
        if (min < max)
        {
            int mid = (min + max) / 2;
            mergeSort (data, min, mid);
            mergeSort (data, mid+1, max);
            merge (data, min, mid, max);
        }
    }

    //-----------------------------------------------------------------
    //  Sorts the specified array of objects using the merge sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void merge (Comparable[] data, int first, int mid,
                              int last)
    {
        Comparable[] temp = new Comparable[data.length];

        int first1 = first, last1 = mid;  // endpoints of first subarray
        int first2 = mid+1, last2 = last;  // endpoints of second subarray
        int index = first1;  // next index open in temp array

        //  Copy smaller item from each subarray into temp until one
        //  of the subarrays is exhausted
        while (first1 <= last1 && first2 <= last2)
        {
            if (data[first1].compareTo(data[first2]) < 0)
            {
                temp[index] = data[first1];
                first1++;
            }
            else
            {
                temp[index] = data[first2];
                first2++;
            }
            index++;
        }

        //  Copy remaining elements from first subarray, if any
        while (first1 <= last1)
        {
            temp[index] = data[first1];
            first1++;
            index++;
        }

        //  Copy remaining elements from second subarray, if any
        while (first2 <= last2)
        {
            temp[index] = data[first2];
            first2++;
            index++;
        }

        //  Copy merged data into original array
        for (index = first; index <= last; index++)
            data[index] = temp[index];
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM