In the sort algorithm, C ++ and PHP implement the bubble sort, that is, the small elements are adjusted forward or the large elements are adjusted backward. The comparison is an adjacent comparison between two elements, and the exchange also occurs between these two elements. Is the slowest sorting algorithm. In practice, it is the most efficient algorithm.
Time complexity:
The worst time complexity is O (n ^ 2). The best time complexity of bubbl
The basic idea of bubble sorting: Bubble sorting is to visit two adjacent numbers in sequence for comparison (except the last number) until the sorting is completed. Example: arr [, 97,], exchanged arr [,], exchanged arr [,], exchanged arr, 55, 65], according... basic idea of bubble sorting:
Bubble
The algorithm has to be tested and the sorting knowledge has been reviewed. Therefore, the review content is summarized as follows. For more information, see Introduction to algorithms. The sorting problem is a problem frequently mentioned in algorithms and data structures. In an interview, I asked the specific princip
Principles of Merge Sorting:
If the number of elements in the array is greater than 1, then:
Divide the array into two parts equally;
Array merging and sorting on the left; Recursion
Array merging and sorting on the right; Recursion
Merge two sorted arrays. An additional secondary array is required to save the merging result temporarily.
Otherwise, when the numbe
Document directory
Which algorithms are stable?
Nine sorting algorithms are mentioned earlier:
1. Simple selection and sorting 2. Heap sorting(1 and 2 are selected for sorting)
3. Insert sort directly 4. Hill sort(3 and 4 are
1. The basic concept of sorting and the so-called sorting of classification are operations that sort a string of records in ascending or descending order according to the size of one or some of the keywords. Sorting algorithms are the methods for sorting records according to
Heapsort is a sort algorithm designed by using the data structure of heap. A heap is a structure that is similar to a completely binary tree and meets the heap nature. That is, the key value or index of a subnode is always smaller than or greater than its parent node.
The heap sorting process is as follows:
1. Create a heap
2. Swap the beginning (maximum) and end of the heap
3. Reduce the heap size by 1 to adjust the top data of the new array to the
The basic idea of quick sorting:
By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into ordered order.
Cases:
arr = [49,38,04,97,76,13,27,49,55,65],
Merging and sorting adopts the divide and conquer idea. arrays are constantly decomposed into sub-arrays until the sub-array has only one element. Each decomposition corresponds to a Merge function, the merge function can combine the two child arrays currently decomposed. There are two methods to achieve Merge Sorting. The first method is to implement recursion. The Code is as follows:
#include
static
[Java] public class ShellSort {/*** sorts n integers, groups the number of columns according to the d1 increment, directly inserts and sorts each group, then reduces the increment and groups again, sort groups until the increment value is 1. * Hill sorting is a Sort Algorithm inserted. Also known as reducing the complexity of incremental sorting to O (nlog n) * unstable
Heap is a Complete Binary Tree. Heap sorting is a tree-based sorting method that uses the biggest features of the elements on the top of the heap to continuously retrieve the largest elements, then, adjust the remaining elements to the top heap, and sort out the largest elements in order to sort the list. For example, the sequence [,] is sorted as follows:
The Code is as follows:
Def fixUp (a): # add new el
The basic idea of quick sorting:
By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into ordered order.
Cases:
arr = [49,38,04,97,76,13,27,49,55,65],
'Merge sorting
Option explicit
Dim result, I
Dim testdata (100)
Const n= 100
Randomize
For I = 0 to n-1
Testdata (I) = round (RND () * 32768)
Next
Sub mergeto (byref array, byref DEST, low, mid, hi)
Dim I, J, K, d
I = low
J = mid
D = low
Do
If I = mid or J = Hi + 1 then
Exit do
End if
If array (I) DeST (d) = array (I)
I = I + 1
Else
DeST (d) = array (j)
J = J + 1
End if
D = d + 1
Loop
For k = I to mid-1
DeST (d) = array (k)
D = d + 1
Next
For k = J
I. Basic Concepts
Heap: this refers to a data structure, not the place where we mentioned in C # used to store referenced objects. It can be regarded as a Complete Binary Tree.In order to store the heap with arrays, each node is marked in order. In fact, we can use a simple formula to obtain the index of the parent node, left child, and right child:
Parent (I) =
Left (I) = 2i
Right (I) = 2i + 1
Max heap and Min heap:The maximum heap refers to the heap where the value of all parent nodes is g
Today, I learned about the analysis and routine of the merge algorithms of other Daniel, and then wrote a non-recursive merge algorithm based on the relevant information, so that I can easily review it later:
Template Void Merge (t src [], t Dest [], int isrcstart, int isrcend, int isrcarrlength) // because SRC contains two sequential sequences, therefore, you need to input parameters to inform the start of the two sequences.{Int I = iSrcStart-1;Int K
Algorithm idea: heap definition: source [I]
Heap sorting thought: (1) Build the waiting sequence into a heap; (2) Take the first element of the heap as the last element of the sequence,
And rebuild the remaining elements into a heap. (3) Repeat the second step until the sequence ends.
C # implementation:
///
The heap sorting logic is very simple, but the efficiency is high. The Code is as follows:
#include
#include
class heap {public: heap(int* array, size_t len):arr (array), length(len){ size_t point = (length -1)/2; for(size_t i = 0; i != point; ++i){ adjust(point-i, length); } } void sort(){ int tmp; for(size_t i = 0; i != length; ++i){ tmp = arr[0]; arr[0] = arr [
Merge Sorting is actually a sort of choice. The idea is relatively simple. The following is the sample code.
Void Merge (int sourceArr [], int targetArr [], int startIndex, int midIndex, int endIndex) {int I, j, k; for (I = midIndex + 1, j = startIndex; startIndex
[Java] public class InsertSort {/*** inserts a data entry into sorted data, in this way, a new ordered data with a number plus one is obtained. * The time complexity is O (n ^ 2) * stable sorting method * @ param nums array to be sorted * @ return output ordered array */public static void sort (int [] nums) {for (int I = 0; I
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.