(i) Insert sort (insertion sort): is one of the simplest sorting algorithms, and the insertion sort consists of a sort of N-1. For p = 1 trip to P = N-1, insert sort guarantees that the element from position 0 to position P is sorted. General method: on page p, move the element on position p to the left to its correct position in the front p + 1 element . (the element on position P is staged in TMP, and all the larger elements are moved to the right by one position (before position P).) The TMP is then placed in the correct position. )
void Insertsort (Elemtype a[], int N)//n array length
{
Int J, P;
Elemtype temp;
for (P = 1; P < N; p++)
{
temp = a[p];
for (j = P; J > 0 && a[j-1] > temp; j--)
A[J] = a[j-1];
A[J] = temp;
}
}
Insert Sort analysis: Because each of the nested loops takes N iterations, the insertion sort is O (n^2), and the bounds are accurate because the inverse input can reach the bounds.
(ii) Hill Sort (shellsort): This algorithm is one of the first algorithms to break the two time barrier. It works by gathering elements of a certain interval; the distances used for each comparison are reduced as the algorithm progresses until only the last order of the adjacent elements is sorted.
Hill sort uses a sequence h1, H2, ..., HK, called an increment sequence. As long as H1 = 1, any increment sequence is feasible, but some increment sequences are better than others. For each I have a[i] <= A[i + HK] After using a single trip of incremental HK, all elements separated by HK are sorted. At this point the file is hk-sorted. one of the important properties of hill sort is that a hk-sort of file keeps its hk-sort of . A trip hk-The role of the sort is to perform a single insertion order on the HK separate sub-array.
2. Hill sort "Fetch Hill increment: N/2 (Next rounding)"
void Shellsort (Elemtype a[], int N)
{
int I, j, Increment;
Elemtype temp;
for (Increment = N/2; Increment > 0; Increment/= 2)
{
for (i = Increment; i < N; i++)
{
temp = A[i];
for (j = i; J >= Increment; J-= Increment)
if (A[j-increment] > Temp)
A[J] = a[j-increment];
Else
Break
A[J] = temp;
}
}
}
The worst case scenario for Hill sorting when using hill increments is O (n^2). The problem is that these incremental pairs are not necessarily reciprocal, so smaller increments can have a small impact.
Hibbard presents a slightly different increment sequence: 1, 3, 7, ..., 2^k-1. The worst case scenario for Hill sorting when using Hibbard increments is
O (n^ (3/2)).
(iii) Bubble sort:
//3. Bubble sort (General thinking: does not exactly match the meaning of the bubbling sort) 22 compare adjacent elements
void bubblesort_1 (Elemtype a[], int N)
{
int i, J, temp ;
for (i = 0; i < N-1; i++)
{
for (j = i + 1; j < N; J + +)
{
if (A[j] < a[i])
{
temp =a[i];
a[i] = A[j];
a[j] = temp;
}
}
}
}
/*
22: Is the meaning of the adjacent two elements
if there are n elements that need to compare n-1 times, each round is reduced by a comparison
so-called bubble sort is from bottom to top 22 comparison
*/
Void Bubblesort_2 (Elemtype a[], int N)
{
int i, J, temp, flag;
flag = 1;
for (i = 0; i < N-1 && flag; i++) //flag = 0 indicates that no element is exchanged in the bubbling,
Which is already ordered sequence, can reduce the number of comparisons
{
for (j = N-1; j > i; j--)
{
Flag = 0;
if (A[j-1] > A[j])//22 comparison
{
Temp =a[j-1];
A[J-1] = A[j];
A[J] = temp;
flag = 1;
}
}
}
}
The bubble sort is O (n^2).
All code:
Various sorting algorithms
#include <iostream>
using namespace Std;
typedef int ELEMTYPE;
1. Insert Sort
void Insertsort (Elemtype a[], int N)
{
Int J, P;
Elemtype temp;
for (P = 1; P < N; p++)
{
temp = a[p];
for (j = P; J > 0 && a[j-1] > temp; j--)
A[J] = a[j-1];
A[J] = temp;
}
}
2. Hill sort "Fetch Hill increment: N/2 (Next rounding)"
void Shellsort (Elemtype a[], int N)
{
int I, j, Increment;
Elemtype temp;
for (Increment = N/2; Increment > 0; Increment/= 2)
{
for (i = Increment; i < N; i++)
{
temp = A[i];
for (j = i; J >= Increment; J-= Increment)
if (A[j-increment] > Temp)
A[J] = a[j-increment];
Else
Break
A[J] = temp;
}
}
}
//3. Bubble sort (General thinking: does not exactly match the meaning of the bubbling sort) 22 compare adjacent elements
void bubblesort_1 (Elemtype a[], int N)
{
int i, J, temp;
for (i = 0; i < N-1; i++)
{
for (j = i + 1; j < N; J + +)
{
&nbs P; if (A[j] < a[i])
{
temp =a[i];
a[i] = A[j];
a[j] = temp;
}
}
}
}
/*
22: Is the meaning of the adjacent two elements
if there are n elements that need to compare n-1 times, each round is reduced by a comparison
so-called bubble sort is from bottom to top 22 comparison
*/
Void Bubblesort_2 (Elemtype a[], int N)
{
int i, J, temp, flag;
flag = 1;
for (i = 0; i < N-1 && flag; i++) //flag = 0 indicates that no element is exchanged in the bubbling,
Which is already ordered sequence, can reduce the number of comparisons
{
for (j = N-1; j > i; j--)
{
Flag = 0;
if (A[j-1] > A[j])//22 comparison
{
Temp =a[j-1];
A[J-1] = A[j];
A[J] = temp;
flag = 1;
}
}
}
}
int main ()
{
int arr[10] = {5, 2, 8, 6, 3, 1, 7, 9, 4, 10};
Insertsort (arr, 10);
Shellsort (arr, 10);
Bubblesort_2 (arr, 10);
for (int i = 0; i <; i++)
cout << Arr[i] << "";
cout << Endl;
System ("pause");
return 0;
}
Sort (i)