1.插入排序
void insert_sort(int a[], int len)
{
int tmp;
for (int i = 1; i <= len - 1; ++i)
{
for (int j = 0; j < i; ++j)
{
if (a[i] < a[j])
{
tmp = a[j];
a[j] = a[i];
for (int k =i; k >j; --k)
{
if (k != j + 1)
a[k] = a[k - 1];
else
a[k] = tmp;
}
break;
}
}
}
return;
}
2.希尔排序
void ShellSort(int a[], int len)
{
int d = len;
int tmp;
while (d > 1)
{
d = (d+1)/2;
for (int i = d; i < len; ++i)
{
//插入排序
for (int j = i%d; j < i;j+=d)
{
if (a[j]>a[i])
{
tmp = a[j];
a[j] = a[i];
for (int k = i; k > j; k=k-d)
{
if (k != j + d)
a[k] = a[k - d];
else
a[k] = tmp;
}
break;
}
}
}
}
return;
}
3.归并排序
void Merge(int a[],int b[],int lpos,int rpos,int rightend );
void Msort(int a[], int b[], int left, int right)
{
int mid;
if (left < right)
{
mid = (left+right) / 2;
Msort(a,b,left,mid);
Msort(a,b,mid+1,right);
Merge(a,b,left,mid+1,right);
}
return;
}
void Merge(int a[], int b[], int lpos, int rpos, int rightend)
{
int i, leftend, len, tmppos;
leftend = rpos - 1;
tmppos = lpos;
len = rightend - lpos + 1;
while (lpos <= leftend && rpos <= rightend)
{
if (a[lpos] < a[rpos])
{
b[tmppos++] = a[lpos++];
}
else
b[tmppos++] = a[rpos++];
}
while (lpos <= leftend)
b[tmppos++] = a[lpos++];
while (rpos <= rightend)
b[tmppos++] = a[rpos++];
for (i = 0; i < len; ++i,rightend--)
a[rightend] = b[rightend];
return;
}
4.快速排序
void QuickSort(int a[], int left, int right)
{
if (left < right)
{
int k = left;
int std = a[k];
int sub;
int rightend = right;
while (left < right)
{
left = left + 1;
while (a[left] < std ) { left++; }
while (a[right] > std ) { right--;}
if (left < right)
{
sub = a[right];
a[right] = a[left];
a[left] = sub;
}
}
sub = a[right];
a[right] = a[k];
a[k] = sub;
QuickSort(a, k, right - 1);
QuickSort(a, right+1, rightend);
}
}