Collection sorting algorithms (C ++ to achieve)

Summary

Sorting operation is very basic and common, basic algorithm is also part of the programming, I have several common sorting algorithm comparison were consolidated.

Sort violence (violence sort)

Idea: iterate, iterate each subscript have found the smallest element in the unsorted part, after the end of the traverse traverse the smallest element into position to start.

Performance: The time complexity is O (n- 2 ), regardless of the number of comparisons the algorithm to the initial state of the sequence, all in the worst performance sorting algorithms.

 

void Violence_Sort(int* A, int len){
        for(int i=0;i<len;i++)
        {
                int k = i;
                for(int j=i;j<len;j++)
                        if(A[j]<A[k])
                                k = j;
                if(k != i){
                        int t = A[i];
                        A[i] = A[k];
                        A[k] = t;
                }
        }
}

 

Insertion sort (insert sort)

Thought: The current element it has previously sorted elements sequentially comparing, the last placed in the right position, initially start the second element, the first element that has been sorted.

Performance: calculation complexity of O (n- 2 ), the sequence at smaller scale, better performance, and compare the number of elements related to the initial degree of sequence messy, and most complexity is O (n).

 

void Insert_Sort(int* A, int len){
        for(int i=1;i<len;i++)
        {
                int key = A[i];
                int j = i - 1;
                while(j >= 0 && key  < A[j]){
                        A[j+1] = A[j];
                        j--;
                }
                A[j+1] = key;
        }
}

 

 

 

Bubble sort (bubble sort) 

Thought: from left to right traversal, compare the size of two adjacent elements, the larger one on the back, each trip traversal may be found at the end of a maximum value is placed, after n-1 times to traverse.

Performance: The time complexity is O (n- 2 ), the number of elements in comparison with the initial state regardless of the performance slightly lower insertion sort.

 

void Bubble_Sort(int* A,int len){
        for(int i=1;i<len;i++)
                for(int j=0;j<len-i;j++)
                {
                        if(A[j]>A[j+1]){
                                int t = A[j+1];
                                A[j+1] = A[j];
                                A[j] = t;
                        }
                }
}

 

Merge sort (merge sort)

Idea: using divide and conquer idea, the original sequence is divided into two parts, respectively, sorting, and then merge, with emphasis on the consolidation process.

Performance: The time complexity is O (nlgn), but the merger process will use the extra storage space, take up memory.

void Merge(int* A, int low, int mid, int high){
        int left[mid-low+1],right[high-mid];
        int sentry = A[mid]>A[high]?(A[mid]+1):(A[high]+1);
        int l = 0;
        int r = 0;
        int i;
        for(i=0;i<mid-low+1;i++)
                left[i] = A[low+i];
        left[i] = sentry;//添加哨兵
        for(i=0;i<high-mid;i++)
                right[i] = A[mid+1+i];
        right[i] =  sentry;//添加哨兵
        for(int i=low;i<=high;i++)
                if(left[l] <= right[r])
                        A[i] = left[l++];
                else
                        A[i] = right[r++];
}

void Merge_Sort(int* A, int low, int high){
        if(high > low){
                int mid = (low+high)/2;
                Merge_Sort(A, low, mid);
                Merge_Sort(A, mid+1, high);
                Merge(A, low, mid, high);
        }
}

 

Quick sort (quick sort)

Idea: to merge sort is similar, using divide and conquer idea, select an element value (usually selected last element), than it is a small part on the left than on the right side it is large, then the above respectively of two parts operating know the end of the recursion, the key step is to classify the elements, and only takes O (1) extra storage space.

Performance: The time complexity is O (nlgn), and merge sort different from the constant-level algorithm occupies extra storage, better performance in large-scale applications ordered sequence.

int Partition(int* A, int low, int high){
        int key = A[high];
        int i = low;
        for(int j=low;j<high;j++)
                if(A[j]<=key){
                        int t = A[j];
                        A[j] = A[i];
                        A[i] = t;
                        i++;
                }
        A[high] = A[i];
        A[i] = Key;
         return I; 
} // classified shifting array element, a key to the left <= key, both the right> key. Return to the index key. 


void quick_sort ( int * A, int Low, int High) { IF (Low> = High) return ; int K = the Partition (A, Low, High); quick_sort (A, Low, K - . 1 ); quick_sort (A, K + . 1 , High); }

Heapsort (heap sort)

Thought: heap data structure sort, heap memory is a binary tree with the array, divided into a maximum and minimum heap stack according to the size relationship between the parent and child nodes, where the maximum heap sort.

Performance: The time complexity is O (nlgn), in actual use, performance is generally inferior to sort quick sort and heap sort.

void Max_Heapify(int* A, int i,int size){
        int l = 2*i;
        int r = 2*i + 1;
        int large = i;
        if(l <= size && A[l] > A[i])
                large = l;
        else
                large = i;
        if(r <= size && A[r] > A[large])
                large = r;
        if(large != i){
        int t = A[large];
        A[large] = A[i];
        A[i] = t;
        Max_Heapify(A, large, size);
        }
}

void Build_Max_Heap(int* A, int size){
        for(int i=size/2;i>0;i--)
                Max_Heapify(A,i,size);
}
  
void Heap_Sort(int* A, int len){
        Build_Max_Heap(A, len);
        while(len-1){
                int t = A[1];
                A[1] = A[i];
                A[i] = t;
                len--;
                Max_Heapify(A,1,len);
        }
}

  

 

 

 

 

Guess you like

Origin www.cnblogs.com/zz-zhang/p/11447557.html
Recommended