Basic Programming Algorithms (I) Basic Programming Algorithms (II) Basic Programming Algorithms (III) Selection Sort Conditions of use: Collections of comparable sizes. Algorithm idea: Each time, select the smallest (or largest) element from the data elements to be sorted, and put it at the end of the sorted sequence until all the data elements to be sorted are sorted. Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17}
Performance analysis: time complexity is O(n^2) Heap Sort Conditions of use: Collections of comparable sizes. Algorithm idea: In fact, heap sort is an evolution of simple selection sort. It mainly reduces the number of comparisons. What is a heap? If the sequence is regarded as a complete binary tree, the values of all non-terminal nodes in the complete binary tree are not greater than (or not less than) the values of its left and right child nodes, which can be called a heap. From the properties of the heap, we can know that the top of the heap is a maximum keyword (or minimum keyword). After outputting the top of the heap, the remaining elements are built into a heap again, and then the top is output. Repeated execution in this way can get an ordered sequence, and this process is the achievement of heap sort. Heap sorting is mainly divided into two steps: (1) Build a heap from an unordered sequence. (2) Output the top element and form a new heap. Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17}
Performance analysis: Time complexity Time complexity O(nlogn) Merge Algorithm Also known as 2-way merge algorithm Conditions of use: Collections of comparable sizes. Algorithm idea: Assuming that the initial sequence contains n records, it can be regarded as n ordered subsequences, each subsequence has a length of 1, and then merged two by two to obtain [n/2] subsequences with a length of 2 or 1 (the length is 1 here, maybe the sequence length is an odd number, then the last sequence is left alone, so the length is 1); then merge two by two, and repeat this process until an ordered sequence of length n is obtained. Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17}
Performance analysis: time complexity O(nlogn) Summarize So with so many sorting algorithms, when should we use which algorithm? Because different sorting methods are suitable for different applications and requirements, choose the appropriate sorting method considering the following factors:
(1) If n is relatively small (for example, n <= 50), direct insertion sort or simple selection sort can be used. (2) If the initial state of the sequence is basically ordered, you can choose direct insertion sort or bubble sort. (3) If n is relatively large, an algorithm with a time complexity of O(nlogn) can be used: quick sort, heap sort, merge sort. Quick sort is considered to be the best method for internal sorting based on comparison. When the sorted keywords are randomly distributed, quick sort has the shortest average time. Unstable Heap sort requires less auxiliary space than quick sort, and will not encounter the worst case scenario that quick sort may encounter. But it is still relatively unstable Merge sort is relatively stable, but it is generally not recommended to use. It has poor practicality and may occupy a large amount of auxiliary space. |
<<: Easily master the basic programming algorithms (Part 2)
>>: Lollipop: A Completely Reborn Android 5.0
This is a simple mind map of paid community plann...
On March 30, the reporter learned from China Nati...
Recently, an incident in which a certain brand of...
Is a children's version of WeChat coming? Rec...
After looking forward to it for a long time, Appl...
"Tesla is bankrupt!" Tesla founder Elon...
Nowadays, the development of the financial market...
When it comes to search engines, everyone used to...
When we talk about integrated marketing , we are ...
Pinduoduo’s ability to carve out a path in the fi...
Your browser does not support the video tag In tr...
This article explains the difference between diff...
Bilibili, from its original video website centere...
introduction In 1971, Kevin Kelly dropped out of ...