Home > Back-end >  Algorithm of 2
Algorithm of 2

Time:09-27

A tree depth of k, and k=log2n + 1, from root to leaf filter, element comparison number up to 2 (k - 1), so, after good pile construction, screening of not more than the number in the process of sorting type:
2 ([log2 (n - 1)] + [log2 (n - 2)] +... + log22) & lt; 2 n ([log2n])
And build the heap is often no more than 4 n times, therefore heap sort the worst cases, the time complexity is O (nlogn)





Exchange Sort, Bubble Sort (mercifully Sort)

In a set of number to sort, the current has not been sorted within the scope of the whole number, from top to bottom of the adjacent two Numbers a comparison and adjustment, make larger number of sinking, smaller rises up, namely: when two adjacent number is found after their sort in contrast to the sorting requirements, will they swap,

Bubble sort of example:

The realization of the algorithm is
Void the bubbleSort (int a [], int n) {
For (int I=0; iFor (int j=0; J & lt; N - I - 1; + + j) {
If (a [j] & gt; A [m + 1])
{
Int TMP=a, [j]. A [j]=a, a + 1 bonus to [j]. A [j + 1)=TMP;
}
}
}
}

Bubble sort of common method is to add a symbolic variable exchange, used to mark a sort in the process of data exchange, if for a trip when ordering not for data exchange, the explain data has been arranged well by the requirement, can be an immediate end to the sorting, avoid unnecessary comparison process, this paper to provide the following two kinds of improved algorithm:
Symbolic variable 1, set up the pos, the sorting of way to record each to exchange the position of the last time, as a result of pos position after records have been exchanged in place, so when the next train sort by scanning the pos location,
Void Bubble_1 (int r [], int n) {
Int I=n - 1;//when the initial and final position unchanged
While (i> 0 {
Int pos=0;//in the beginning, on a visit to no record exchange
For (int j=0; JIf (r [j] & gt; R [m + 1]) {
Pos=j;//to record the location of exchange
Int TMP=r [j]; R=r [j] [j + 1); R=[m + 1] TMP;
}
I=pos.//to prepare for the next train sorting
}
}
2, the traditional bubble sort on every sort operations can only be found in a maximum or minimum, we consider the use of the sorting of per trip for forward and reverse twice a bubbling method can get two final value (the maximum and the minimum), to be sorted for almost halved,
The improved algorithm is:
Void Bubble_2 (int r [], int n) {
Int low=0;
Int high=n - 1;//set the initial value of the variable
Int TMP, j;
While (low & lt; High) {
For (j=low; JIf (r [j] & gt; R [m + 1]) {
TMP=r [j]; R=r [j] [j + 1); R=[m + 1] TMP;
}
- high;//modify high value, reach a
For (j=high; J> Low; - j)//reverse bubbling, find the smallest person
If (r [j] TMP=r [j]; R=r [j] [1]; R [1]=TMP;
}
+ + low;//modify the low value, ward a
}
}


Exchange Sort, Quick Sort, Quick Sort)
1) select a base element, usually choose the first or the last element,
2) through the record of a trip to the sorting will be ordered to be split into separate two parts, one part of the element values are smaller than benchmark element value, another part of the record element value is greater than at baseline,
3) the base element in their correct position after the sorted
4) and then record respectively for the two parts in the same way to sort, until the entire sequence and orderly,

Quick sort of example:
(1) a trip to the sorting process:

(b) the whole process of ordering

The realization of the algorithm:
The recursive implementation:
#include

Using namespace STD: : cout;
Using namespace STD: : endl;

Void print (int a [], int n) {
For (int j=0; Jcout}
cout}

Void swap (int * a, int * b)
{
Int TMP=* a;
*a=*b;
*b=tmp;
}

Int partition (int a [], int low, int high)
{
Int privotKey=a, [low].//base element
While (low & lt; High) {//from the table alternately on both ends to the middle of the scanning
While (low & lt; High & amp; & A [high] & gt;=privotKey) - high;//from the referred to in high position forward search, go up to low + 1 position, will be smaller than benchmark element exchange to the low end of the
Swap (& amp; A [low], & amp; A [high]);
While (low & lt; High & amp; & A [low] <=privotKey) + + low;
Swap (& amp; A [low], & amp; A [high]);
}
Print (a, 10);
Return low;
}


Void quickSort (int a [], int low, int high) {
If (low & lt; High) {
Int privotLoc=partition (a, low, high);//the table into two
QuickSort (a, low, privotLoc - 1);//order of the low child table recursive recursive
QuickSort (a, privotLoc + 1, high);//order of the Gao Zibiao recursive recursive
}
}

Int main () {
Int a [10]={3,1,5,7,2,4,9,6,10,8};
cout<" The initial value: ";
Print (a, 10);
QuickSort (a, 0, 9);
cout<" Results: ";
Print (a, 10);

}

Analysis:
Quick sort O is usually considered in the same order of magnitude (nlog2n) sorting method of average performance is the best, but if the initial sequence according to the key code or basic orderly, quick sort bubble sort reduced to instead, for improvement of, often take method of "three" to select the benchmark records, the sorting interval of two endpoints and midpoint record key centring is adjusted for protection, the ranking method of quick sort is an unstable,

The improvement of quick sort
In this algorithm, only the length is more than K subsequence recursive call quick sort, let original basic orderly sequence, and then orderly sequences to the basic sort, insertion sort algorithm practice has proved that the improved algorithm time complexity is reduced, and when the K value is around eight, the performance of the improved algorithm is the best, the ideas of the algorithm are as follows:
Void print (int a [], int n) {
For (int j=0; Jcout
}
cout}

Void swap (int * a, int * b)
{
Int TMP=* a;
*a=*b;
*b=tmp;
}

Int partition (int a [], int low, int high)
{
Int privotKey=a, [low].//base element
While (low & lt; High) {//from the table alternately on both ends to the middle of the scanning
While (low & lt; High & amp; & A [high] & gt;=privotKey) - high;//from the referred to in high position forward search, go up to low + 1 position, will be smaller than benchmark element exchange to the low end of the
Swap (& amp; A [low], & amp; A [high]);
While (low & lt; High & amp; & A [low] <=privotKey) + + low;
Swap (& amp; A [low], & amp; A [high]);
}
Print (a, 10);
Return low;
}


Void qsort_improve (int r [], int low, int, int k) {
If (high - low & gt; K) {//length is greater than k recursion, k for the specified number
Int the pivot=partition (r, low, high);//call the Partition algorithm remains the same
Qsort_improve (r, low, the pivot - 1, k);
Qsort_improve (r, pivot + 1, high, k);
}
}
Void quickSort (int r [], int n, int k) {
Qsort_improve (r, 0, n, k);//improved algorithm called first tree makes the basic order of

//to use insertion sort of basic orderly sequence sorting
for(int i=1; i<=n; I + +) {
Int TMP=r [I];
Int j=I - 1;
While (TMP & lt; R [j]) {
R=r [m + 1] [j]; J=j - 1;
}
R=[m + 1] TMP;
}

}



Int main () {
Int a [10]={3,1,5,7,2,4,9,6,10,8};
cout<" The initial value: ";
Print (a, 10);
QuickSort (a, 9, 4);
cout<" Results: ";
Print (a, 10);

}

Merge Sort, Merge Sort)
The basic idea
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related