Find and sort are the algorithms that are often used in programs

First, find

look for: sequential lookups, binary lookups, hash table lookups, and two-tree sorting lookups.

The focus of a hash table and a two-tree lookup is on its data structure. The main advantage of a hash table is the ability to find an element at an O (1) time, which is the most efficient way to find it. The disadvantage is that additional space is required to implement the hash table.

Second, sort

Sorting is divided into insert sort, swap sort, select sort Merge sort etc. The pros and cons of these sorts of methods (extra space consumption, average time complexity, and worst-time complexity), are characterized by emphasis.

1. Insert Sort

A. Direct insertion

When a given sequence of data elements is ordered, the minimum number of comparisons between keywords is the best case of the time Complexity O (N) and the worst Case O (n^2)

void Insertsort (int*a, int length) {for (int i = 1; i < length; i++) {int tmp = A[i];int j = 0;for (j = i-1; J >=0&am p;&tmp<a[j];j--)///When the unordered element is less than the ordered element, move the ordered element to the whole of the element to be sorted after moving back {a[j + 1] = a[j];} A[J+1] = tmp;}}

Insert sort is the most stable sort method

B. Binary insertion

Similar to the direct insert ordering process, the binary method is used to find the insertion position.

void Biinsertsort (int *a, int length) {for (int i = 1; i < length; i++) {Int. TMP = A[i];int left = 0;int right = I-1;in T j = 0;while (left <= right) {int mid = (left + right), if (TMP < A[MID])//binary {right = Mid-1;} Else{left = mid + 1;}} for (j = i-1; J >= left; j--)//Shift {A[J + 1] = a[j];} A[j + 1] = tmp;}}

2. Exchange sorting

22 comparison, if found in reverse order, then exchange, until the element sequence is not reverse order

A. Bubble sort

Time complexity O (n^2) is a stable sorting method

void Bubblesort (int *a, int length) {for (int i = 0; i < length; i++) {for (int j = 0; J < Length-i-1; J + +) {if (A[j] >a[j + 1]) swap (A[j], a[j + 1]);}}}

B. Quick Sort

1) Set two variables I, J, at the beginning of the order: i=0,j=n-1;

2) The first array element as the key data, assigned to the key, that is key =a[0];

3) Forward search from J, that is, after starting the forward search (j--), find the first value less than key A[j], will a[j] and A[i] interchange;

4) Backward search from I, that is, start backward search (i++), find the first a[i] greater than key ], interchange a[i] and A[J];

5) Repeat 3rd, 4, until i=j, (3,4 step, did not find the matching criteria, that is, 3 a[j] is not less than key, 4 A[i] is not larger than the time of the key change J, I value, so j=j-1,i=i+1, until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).

int partition (int *a, int i, int j) {int base = a[i];while (I&NBSP;<&NBSP;J) {//scan while from right to left (base < a[j] &&&NBSP;I<J) j--;if (I<J)//After the previous step while loop, A[i]>a[j]{swap (A[i], a[j]); i++;} Scan from left to right while (I<j && base>a[i]) i++;if (i<j) {swap (a[i], a[j]); j--;}} A[i] = base;return i;} Void quicksort (int *a, int start, int end) {int index=0;if (start < end) {index = partition (a, start, end); QuickSort (a, start, index - 1); QuickSort (A, index + 1, end);}}

3. Select sort

A. Direct selection of sorting

B. Heap sequencing

Quick Sort

The key to fast sorting is to first select a number in the array, then the number in the array is divided into two parts, smaller than the selection of the array on the left side, large to the right.

This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1772737

Find and sort