Common sorting algorithm finishing 2 (C++ implementation)

1. Merge Sort

  • Time complexity: O(nlogn)
  • Space Complexity: O(n)

  The basic idea of ​​the algorithm: Merge every two adjacent elements to get a new merged array, and merge and sort every two groups again until all elements are sorted.

  C++ implementation:

void mergehelp(int* A,int left,int mid,int right)
    {
        int* B=new int[right-left+1];
        int index=0;
        int i=left;
        int j=mid+1;
        while(i<=mid&&j<=right)
        {
            B[index++]=A[i]<=A[j]?A[i++]:A[j++];
        }
        while(i<=mid)
        {
            B[index++]=A[i++];
        }
        while(j<=right)
        {
            B[index++]=A[j++];
        }
        for(int i=0;i<index;i++)
        {
            A[left++]=B[i];
        }
    }
    void merge(int* A,int left,int right)
    {
        if(left>=right)
            return;
        int mid=(left+right)/2;
        merge(A,left,mid);
        merge(A,mid+1,right);
        mergehelp(A,left,mid,right);
    }
    int* mergeSort(int* A, int n) {
        int left=0;
        int right=n-1;
        merge(A,0,n-1);
        return A;

    }

2. Quick Sort

  • Time complexity: O(nlogn)
  • Space complexity: O(logn)

  The basic idea of ​​the algorithm:

  Find the location point through the partition function, and use recursive thinking to continuously divide the two-part array until the number of arrays is 1. The idea of ​​the partition function is to find the division point pivot by filling the pit method, placing the elements greater than or equal to the pivot value on the right side of the array, placing the elements less than or equal to the pivot value on the left side of the array, and returning the position of the division point pivot.

  C++ implementation:

int partition(int* A,int left,int right)
    {
        int pivot=A[left];
        while(left<right)
        {
            while(left<right&&A[right]>=pivot)
            {
                right--;
            }
            swap(A[left],A[right]);
            while(left<right&&A[left]<=pivot)
            {
                left++;
            }
            swap(A[left],A[right]);
        }
        return left;
    }
    void quickSortHelp(int* A,int left,int right)
    {
        if(left<right)
        {
            int pivot=partition(A,left,right);
            quickSortHelp(A,left,pivot-1);
            quickSortHelp(A,pivot+1,right);
        }
    }
    int* quickSort(int* A, int n) {
        int left=0;
        int right=n-1;
        quickSortHelp(A,left,right);
        return A;

    }

3. Heap Sort

  • Time complexity: O(nlogn)
  • Space Complexity: O(1)
  • The basic idea of ​​the algorithm is to construct a maximum heap, and the element at the top of the heap is the maximum value. If the element at the top of the heap is exchanged with the last element of the array, the last element is sorted. Continue to swap the top element of the heap with the last element until the number of arrays is one. After each swap, the heap needs to be adjusted once using the heapify function. The process of constructing the maximum heap also uses the heapify function to continuously adjust the array values.
  • C++ implementation:
void heapify(int* A,int left,int right)
    {
        int cur=left;
        int child=2*cur+1;
        while(child<right)
        {
            if(child+1<right&&A[child+1]>A[child])
            {
                child++;
            }
            if(A[child]>A[cur])
            {
                swap(A[cur],A[child]);
                cur=child;
                child=2*cur+1;
            }
            else
            {
                break;
            }
        }
    }
    int* heapSort(int* A, int n) {
        for(int i=n/2-1;i>=0;i--)
        {
            heapify(A,i,n-1);
        }
        for(int i=n-1;i>0;i--)
        {
            swap(A[i],A[0]);
            heapify(A,0,i);
        }
        return A;
    }

4. Hill sort

  • Time complexity: O(nlogn)
  • Space Complexity: O(1)
  • The basic idea of ​​the algorithm: Hill sort is a derivative of insertion sort. Set an increment d, divide the array elements into d increments, and use insertion sort in each increment. Then reduce the value of increment d, divide the elements into d increments again, and perform insertion sort. Until the increment d is 1, the sorting is completed.
  C++实现:int* shellSort(int* A, int n) {
        int d=n/2;
        while(d>=1)
        {
            for(int i=d;i<n;i++)
            {
                int get=A[i];
                int j=i-d;
                while(j>=0&&A[j]>=get)
                {
                    A[j+d]=A[j];
                    j=j-d;
                }
                A[j+d]=get;
            }
            d=d/2;
        }
        return A;
    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325668233&siteId=291194637
Recommended