Summary of Common Sorting Algorithms (Not Completed)

Posted by Archbob on Tue, 24 Sep 2019 05:44:36 +0200

Article directory

Summary

Time complexity

  • Average case: fast row, Hill sort, merge sort, stack is nlogn, the rest is n^2; special case is base sort, complexity is d(n+rd) (where n is the number of key words, D is the key number of key words, such as 930, d=3; RD is the number of key word bases, base refers to the symbols that constitute keywords, such as Guan. When the key number is numeric, the symbol constituting the keyword is 0-9, so rd=10)
  • Worst case: n^2 in the fast queue, the same as the average.
  • Auxiliary Note: Quickly return to the queue at nlogn speed (the average complexity of the four sorts is nlogn)

Spatial complexity

  • Fast Row: nlogn
  • Merge Sort: n
  • Cardinal sorting: rd
  • Others: 1

Initial condition

Direct interpolation is easy to interpolate into n, bubbling is easy to interpolate into n, and easy to interpolate means that the initial sequence is ordered.

stability

Emotional instability, quickly choose a bunch of friends to chat

Fast scheduling, Hill sorting, simple selection sorting, heap sorting is not stable, the rest are stable.

Sequencing principle

  • One-time sorting ensures that a keyword reaches its final position. This sort is the two kinds of exchange classes (bubble, fast row) and the two kinds of selection classes (simple selection, heap).
  • The number of keyword comparisons in the sorting algorithm is independent of the original sequence--simple selection sorting and half insertion sorting
  • The ranking number of sorting algorithms is related to the original sequence - sorting of exchange classes
  • In the worst case, the time complexity is at least nlogn** with the help of ** "comparison" sorting algorithm.

Direct Insert Sort and Half Insert Sort

The biggest difference between them is the way to find the insertion position. Direct insertion searches in sequence, while half insertion searches in half

Bubble Sort

//java language implementation
public class BubbleSort {
    public void sort(int[] a)
    {
        for(int i = 0 ; i< a.length ; ++i)
        {
            for(int j = i; j< a.length; ++j)
            {
                if(a[i] > a[j])
                {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }
}

#python language implementation
class Solution:
    def bubble_sort(self, my_list):
        for passnum in range(len(my_list)-1, 0, -1):
            for i in range(passnum):
                if my_list[i] > my_list[i + 1]:
                    temp = my_list[i]
                    my_list[i] = my_list[i + 1]
                    my_list[i + 1] = temp

Selection Sort

Insertion Sort

Shell Sort

Merge Sort

Quick Sort

//java language implementation
public class QickSort {
    public void sort(int[] a, int left, int right)
    {
        if(left >= right) return;
        int pivotValue = partition(a, left ,right);
        sort(a,left,pivotValue - 1);
        sort(a,pivotValue + 1 ,right);
    }
    public int partition(int[] a, int left, int right)
    {
            int pivotValue = a[left];
            int i = left;
            int j = right + 1;
            while (true)
            {
                while (a[++i] < pivotValue) if(i == right) break;
                while (pivotValue < a[--j]) if (j == left) break;
                if(i >= j) break;
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
            int temp = a[left];
            a[left] = a[j];
            a[j] = temp;
            return j;
    }
}
#python language implementation

class Solution:

    def quick_core(self, my_list, first, end):
        if first < end:
            split_point = self.partition(my_list, first, end)

            self.quick_core(my_list, first, split_point - 1)
            self.quick_core(my_list, split_point + 1, end)

    def partition(self, my_list, first, end):

        pivot_value = my_list[first]

        first_mark = first + 1
        end_mark = end

        flag = True

        while flag:
            while first_mark <= end_mark and my_list[first_mark] <= pivot_value:
                first_mark = first_mark + 1

            while my_list[end_mark] >= pivot_value and end_mark >= first_mark:
                end_mark = end_mark - 1

            if end_mark < first_mark:
                flag = False
            else:
                temp = my_list[first_mark]
                my_list[first_mark] = my_list[end_mark]
                my_list[end_mark] = temp

        temp = my_list[first]
        my_list[first] = my_list[end_mark]
        my_list[end_mark] = temp

        return end_mark
    

Topics: shell REST Java Python