The so-called sort, is to organize the records in the file, so that the key words in ascending (or descending) order. The exact definition is as follows:
Input: N records r1,r2,...,rn, the corresponding key words are k1,k2,...,kn respectively.
Output: Ril,ri2,...,rin, make ki1≤ki2≤ ... ≤kin. (or ki1≥ki2≥ ...) ≥kin).
The time cost of sorting can be measured by the number of data comparisons performed in the algorithm and the number of times the data was moved. The basic sorting algorithms are as follows: Swap sort (bubble sort, quick sort), select sort (Direct select sort, heap sort), insert sort (direct insert sort, hill Sort), merge sort, allocation sort (base sort, box sort, count sort). The following is a list of the code for each algorithm, followed by a brief analysis. The code that allocates the sorting algorithm is not listed.
(1) Quick sort: The fast sort is a sort of division exchange which was proposed by C.r.a.hoare in 1962. It adopts a strategy of divide and conquer, which is often referred to as the Partition method (Divide-and-conquermethod). The best, average degree of complexity is O (NLOGN), the worst is O (n^2).
void Quick_sort1 (int a[],int l,int r)
{
if (L >= R) return
;
int I, J, p;
i = l-1, j = l,p = A[r];
while (J < R)
{
if (A[j] < P)
swap (A[++i], a[j]);
j + +;
}
Swap (A[++i], a[r]);
Quick_sort1 (A, L, i-1);
Quick_sort1 (A, i+1, R);
}
In the book "Introduction to Algorithms", the pseudocode for this program is given. When an array element is equal, in reverse order, and sequentially arranged, calling this program causes a stack overflow. Because each division is the worst bad part. can be improved. Each of the above procedures is a fixed set of benchmark elements, if it is randomly generated, it can greatly reduce the probability of the worst division.
void Quick_sort2 (int a[],int l,int r)
{
if (L >= R) return
;
int i,j,p;
i = L-1,j = l;
P=l + rand ()% (r-l); Number
swap (a[p], a[r] between randomly generated [l,r]);
p = a[r];
while (J < R)
{
if (A[j] < P)
swap (A[++i], a[j]);
j + +;
}
Swap (A[++i], a[r]);
Quick_sort2 (A, L, i-1);
Quick_sort2 (A, i+1, R);
}
However, when the array elements are equal, there is a stack overflow. Adjustments can be made as follows.
void quick_sort3 (int a[],int l,int r)
{
if (L >= R) return
;
int i,j,p;
i = l-1, j = r, p = a[r];
while (1)
{doing
{i++} while (A[i] < P && I < R);
do {j--} while (A[j] > P && J > L);
if (i >= j) break
;
Swap (A[i], a[j]);
Swap (A[i],a[r]);
Quick_sort3 (A, L, i-1);
Quick_sort3 (A, i+1, R);
}
However, when the sequence of array elements is in reverse order, there is also a stack overflow. If you combine the two, you can avoid stack overflow as much as possible.
void quick_sort4 (int a[],int l,int r)
{
if (L >= R) return
;
int i,j,p;
i = l-1, j = r;
p = l + rand ()% (r-l);
Swap (A[p],a[r]);
p = a[r];
while (1)
{doing
{i++} while (A[i] < P && I < R);
do {j--} while (A[j] > P && J > L);
if (i >= j) break
;
Swap (A[i], a[j]);
Swap (A[i], a[r]);
Quick_sort4 (A, L, i-1);
Quick_sort4 (A, i+1, R);
}
(2) Bubble sort: 22 Compare the keywords for the records to be sorted, and then swap the two records in reverse order until there are no reversed records.
void Bubble_sort1 (int a[],int n)
{
int i,j;
for (i = 0; i < n-1; i++)
{
for (j = i+1; J < N; j +)
{
if (A[i] > a[j])
swap (a[i), a[j]; c32/>}}}
Can be slightly improved, when the array number of elements in order, the time complexity of O (n). Add a variable, and if there is no swap in the scan, end the sort, because the array is sorted.
void Bubble_sort2 (int a[],int n)
{
int i,j;
for (i = 0; i < n-1; i++)
{
bool exchange = FALSE;
for (j = i+1 J < N; j + +)
{
if (A[i] > A[j])
{
Exchange = true;
Swap (A[i], a[j]);
}
if (Exchange = false) break
;
}
}
After the Netizen pointed out, above this bubble sort has the problem, cannot obtain the correct result. The following quote from the correct wording of the user:
void Bubble_sort2 (int a[],int n)
{
int i,j;
for (i = 0;i < n-1; i++)
{
bool exchange = FALSE;
for (j = n-1;j > i; j--)
{
if (A[j-1] > A[j])
{
Exchange = true;
Swap (A[j-1], a[j]);
}
if (Exchange = false) break
;
}
}
(3) Direct selection order: each trip from the record to be sorted to select the smallest key record, in the order placed in the finished sub file, until all the records sorted.
void Select_sort1 (int a[],int n)
{
int i,j;
for (i = 0; i < n-1 i++)
{
int min = i;
for (j = i+1 J < N; j + +)
{
if (A[j] < a[min])
min = j;
}
if (min!= i)
swap (A[i], a[min]);
}
(4) heap sorting: According to the input data, using the heap adjustment algorithm to form the initial heap, and then exchange the root element and the tail element, the total number of elements minus 1, and then from the root downward adjustment. The best, worst, and average time complexity for heap sorting is O (NLOGN)
void Heap_siftdown (int a[],int n,int p)/adjustment algorithm
{
int i = p,j = i*2+1;
int tmp = A[i];
while (J < N)
{
if (j+1 < n && A[j] < a[j+1])
j + +;
if (A[j] <= tmp) break
;
else
{
A[i] = a[j];
i = J;j = J*2+1
}
}
A[i] = tmp;
}
void Heap_sort1 (int a[],int n)
{
int i;
for (i = (n-1)/2; I >= 0;i--)
Heap_siftdown (A, n, i);
for (i = n-1;i >= 0; i--)
{
swap (a[i], a[0]);
Heap_siftdown (A, I, 0);
}
(5) Direct insertion sort: each time a record to be sorted is inserted at its key size into the appropriate position in the previously sorted child file until all records are inserted. When the array is sorted, the time complexity of the direct insertion sort is O (n)
void Insert_sort1 (int a[],int n)
{
int i,j;
for (i = 1; i < n; i++)
{
for (j = i; j > 0 && a[j]<a[j-1]; j--)
swap (a[j-1], a[j]);
If you expand the Swap function, you can speed up the sorting.
void Insert_sort2 (int a[],int n)
{
int i,j;
for (i = 1; i < n; i++)
{for
(j = i; j > 0 && a[j] < a[j-1]; j--)
{
int t = A[j-1];
A[J-1] = a[j];
A[J] = t;}}}
Can be further improved, the INSERT_SORT2 algorithm continues to assign value to T, you can move assignment statements outside the loop.
void Insert_sort3 (int a[],int n)
{
int i,j;
for (i = 1;i < n; i++)
{
int t = a[i];
for (j = i; j > 0 && a[j-1] > t; j--)
a[j] = a[j-1];
A[J] = t;
}
}
(6) Hill sort: First take an integer D1 less than n as the first increment, the entire record of the file into D1 groups. All records that are multiples of the DL are placed in the same group. First in each group of direct interpolation sort; Then, take the second increment d2<d1 repeat the grouping and sorting above until the incremental dt=1 (DT<DT-L<...<D2<D1) is taken, that is, all records are placed in the same group for direct insertion sort.
The last increment must be 1, in fact, is to invoke the direct insertion sort algorithm.
void Shell_sort1 (int a[],int n)
{
int i = n;
do{
i = I/3 + 1;
Shell_pass1 (A, n, i);
} while (i > 1);
}
void Shell_pass1 (int a[],int N,int Inc)//inc to 1 o'clock, in fact, is directly inserted sort
{
int i,j;
for (i = inc i < N; i++)
{
int t=a[i];
for (j = I;j >= inc && A[j-inc] > t; j-= Inc)
a[j] = A[j-inc];
A[J] = t;
}
}
(7) Merge sort: use "merge" technology to sort. Merge refers to merging several sorted children into an ordered file. can be used for external sorting.
void Merge_sort1 (int a[],int b[],int l,int r)
{
if (L >= R) return
;
int m = (l+r)/2;
Merge_sort1 (A, B, L, m);
Merge_sort1 (A, B, m+1, r);
Merge1 (A, B, L, M, R);
void Merge1 (int a[],int b[],int l,int m,int r)
{
int i,j,k;
for (i = l; I <= R; i++)
b[i] = a[i];
i = l; j = m+1; k = l;
while (I <= m && J <= R)
{
if (B[i] <= b[j]) a[k++] = b[i++];
else a[k++] = b[j++];
}
while (i <= m) a[k++] = b[i++];
while (J <= r) a[k++] = b[j++];
}
The test driver and two auxiliary programs for the program of the above algorithm are given. To test some sort algorithm, simply remove the annotation.
#include <iostream> #include <ctime> using namespace std;
const int N = 100;
int a[n];
int b[n];
int main () {int i;
Srand (Time (0));
for (i=0;i<n;i++)//a[i]= n-i;
for (i = 0;i < N; i++) A[i]=rand ()%N;
Long Start,end;
start = Clock ();
Quick_sort1 (a,0,n-1);
Quick_sort2 (a,0,n-1);
Quick_sort3 (a,0,n-1);
QUICK_SORT4 (a,0,n-1);
Bubble_sort1 (A,n);
Bubble_sort2 (A,n);
Merge_sort1 (a,b,0,n-1);
Heap_sort1 (A,n);
Shell_sort1 (A,n);
Select_sort1 (A,n);
Insert_sort1 (A,n);
Insert_sort2 (A,n);
Insert_sort3 (A,n);
end = Clock ();
Print_array (A, N);
cout<< "Total time is:" << (End-start)/1000.0<< ' s ' <<endl;
return 0;
} void Swap (int a[],int i,int j)/exchange element {int t = A[i];
A[i] = A[j];
A[J] = t;
} void Print_array (int a[],int n)//print element value {for (int i = 0; i < n; i++) {cout<<a[i]<< ';
if (i%10==0 && i!=0) cout<<endl; } cout<<endl;
}