八大排序算法总结(2)

 

 

归并排序; 

 剑指Offer--35数组中的逆序对

148. Sort List(归并排序)

如下图所示,为数组 [7, 3, 2, 6, 0, 1, 5, 4][7,3,2,6,0,1,5,4] 的归并排序过程。

 

 

 1 //merge sort
 2     public static void merge(int[] a,int aux[],int lo ,int mid,int hi ){
 3         int i = lo;
 4         int j = mid+1;
 5         //copy a
 6         for (int k = lo; k <= hi; k++) 
 7             aux[k] = a[k];
 8         //merge
 9         for(int k = lo;k<=hi;k++){        
10             //先判断是否越界
11             if(i>mid)                    a[k]=aux[j++];
12             else if(j>hi)                a[k]=aux[i++];
13             // merge
14                 else if(aux[j]<=aux[i])     a[k]=aux[j++];
15                 else                     a[k]=aux[i++];
16         }
17     }
18     public static void mergesort(int[] a,int aux[],int lo,int hi) {
19         //边界条件
20                 if(lo>=hi) return ;
21         int mid = (hi-lo)/2+lo;
22         mergesort(a,aux, lo, mid);
23         mergesort(a,aux, mid+1, hi);
24         merge(a,aux, lo, mid, hi);
25         
26     }
27     public static void mergeSort(int[] a){
28         int aux[] = new int[a.length];
29         mergesort(a,aux, 0, a.length-1);
30     }

 

class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        def merge(nums,aux,l,m,r):
            i = l
            j = m
            k = l
            while i < m and j < r :
                if aux[i] < aux[j]:
                    nums[k] = aux[i]
                    i+=1
                    k+=1
                else:
                    nums[k] = aux[j]
                    j+=1
                    k+=1
            while i < m :
                nums[k] = aux[i]
                i+=1
                k+=1
            while j < r :
                nums[k] = aux[j]
                j+=1
                k+=1
            aux[l:r] = nums[l:r]
        def merge_sort(nums,aux,l,r):
            if l>=r-1 :
                return
            m = l + int((r-l)/2)
            merge_sort(nums,aux,l,m)
            merge_sort(nums,aux,m,r)
            merge(nums,aux,l,m,r)

        aux = nums.copy()
        merge_sort(nums,aux,0,len(nums))
        return aux

 

void merge(vector<int>& nums,int low, int mid, int high){
    int l_start = low;
    int l_end = mid;
    int r_start = mid + 1;
    int r_end = high;
    int k = low;
    vector<int> aux = vector<int>(nums);
    while(l_start <= l_end && r_start <= r_end) aux[k++] = nums[l_start]<nums[r_start]?nums[l_start++]:nums[r_start++];
    while(l_start <= l_end) aux[k++] = nums[l_start++];
    while(r_start <= r_end) aux[k++] = nums[r_start++];
    //把数据复制回原数组
    for(int i = low; i <= high; ++i) {
        nums[i] = aux[i];
    }
};
void merge_and_sort(vector<int>& nums,int low,int high) {
    if (low>=high) return;
    int mid = low + (high - low) / 2;
    merge_and_sort(nums,low,mid);
    merge_and_sort(nums,mid+1,high);
    merge(nums,low,mid,high);
};
void mergeSort(vector<int>& nums) {
    merge_and_sort(nums,0,nums.size()-1);
};

  

 

快速排序

class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        def partition(nums,l,r):
            pivot = nums[l]
            i = l+1 
            j = r
            while i <= j:
                if nums[i] < pivot < nums[j]:
                    nums[i],nums[j] = nums[j],nums[i]
                    i+=1
                    j-=1
                if nums[i] >= pivot:
                    i+=1
                if nums[j] <= pivot:
                    j-=1
            nums[j],nums[l] = nums[l],nums[j]
            return j

        def quick_sort(nums,l,r):
            if l>r :
                return
            j = partition(nums,l,r)
            quick_sort(nums,l,j-1)
            quick_sort(nums,j+1,r)
            
        random.shuffle(nums)
        quick_sort(nums,0,len(nums)-1)
        return nums[::-1]

 


215. 数组中的第K个最大元素(快排,堆排序)
 
   int partition(vector<int>& nums, int left, int right) {
        int pivot = nums[left], i = left + 1, j = right;
        while (i <= j) {
            if (nums[i] < pivot &&  pivot < nums[j] ) {
                swap(nums[i++], nums[j--]);
            }
            if (nums[i] >= pivot) {
                i++;
            }
            if (pivot >= nums[j]) {
                j--;
            }
        }
        swap(nums[left], nums[j]);
        return j;
    }
void partation_and_sort(vector<int>& nums, int low ,int high) {
    if (low>=high) return;
    int j = partation(nums,low,high);
    cout << j << endl;
    partation_and_sort(nums,low,j);
    partation_and_sort(nums,j+1,high);
}
void quickSort(vector<int>& nums) {
    partation_and_sort(nums,0,nums.size()-1);
}

 

 

堆排序


215. 数组中的第K个最大元素(快排,堆排序)

class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        def max_heapfiy(nums,l,r):
            dad = l 
            son = dad*2+1

            while son <= r:
                if son+1 <= r and nums[son] > nums[son+1]:
                    son+=1
                if nums[dad] < nums[son]:
                    return 
                else:
                    nums[son],nums[dad] = nums[dad],nums[son]
                    dad = son
                    son = dad*2+1


        n = len(nums)
        for i in range(0,n//2)[::-1]:
            max_heapfiy(nums,i,n-1)
        
        for i in range(1,n)[::-1]:
            nums[i],nums[0] = nums[0],nums[i]
            max_heapfiy(nums,0,i-1)
        return nums[::-1]

 

 
#include <iostream>
#include <algorithm>
using namespace std;
 
void max_heapify(int arr[], int start, int end) 
{
    //建立父节点指标和子节点指标
    int dad = start;
    int son = dad * 2 + 1;
    while (son <= end)  //若子节点指标在范围内才做比较
    {    
        if (son + 1 <= end && arr[son] < arr[son + 1]) //先比较两个子节点大小,选择最大的
            son++;
        if (arr[dad] > arr[son]) //如果父节点大於子节点代表调整完毕,直接跳出函数
            return;
        else  //否则交换父子内容再继续子节点和孙节点比较
        {
            swap(arr[dad], arr[son]);
            dad = son;
            son = dad * 2 + 1;
        }
    }
}
 
void heap_sort(int arr[], int len) 
{
    //初始化,i从最後一个父节点开始调整
    for (int i = len / 2 - 1; i >= 0; i--)
        max_heapify(arr, i, len - 1);
    //先将第一个元素和已经排好的元素前一位做交换,再从新调整(刚调整的元素之前的元素),直到排序完毕
    for (int i = len - 1; i > 0; i--) 
    {
        swap(arr[0], arr[i]);
        max_heapify(arr, 0, i - 1);
    }
}
 
void main() 
{
    int arr[] = { 3, 5, 3, 0, 8, 6, 1, 5, 8, 6, 2, 4, 9, 4, 7, 0, 1, 8, 9, 7, 3, 1, 2, 5, 9, 7, 4, 0, 2, 6 };
    int len = (int) sizeof(arr) / sizeof(*arr);
    heap_sort(arr, len);
    for (int i = 0; i < len; i++)
        cout << arr[i] << ' ';
    cout << endl;
    system("pause");
}

 

posted @ 2017-12-08 21:07  乐乐章  阅读(277)  评论(0编辑  收藏  举报