{
mid=(low+high)/2;
MergeSort(R,low,mid);
MergeSort(R,mid+1,high);
Merge(R,low,mid,high);
}
}
int main(void)
{
int i;
int a[10]=;
int low=0,high=9;
}
}
void main()
{
int arr[]={12,34,64,23,87,54,89,45,56,1,3,7,5,4,2};
shell_sort(arr,15);
}
堆排序
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
while(i<=m)
R1[p++]=R[i++];
while(j<=high)
R1[p++]=R[j++];for(p=0,i=low;i<=high;p++,i++)
R[i]=R1[p];
}
void MergeSort(int R[],int low,int high)
{
int mid;
{
int i, last;
last = -1;
for (i=0; i<10; i++) {
if (fr[i] != -1) {
if (last == -1) {
head = fr[i];
last = re[i];
}
else {
a[last].next = fr[i];
last = re[i];
{ temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;}
}
for(i=1;i<11;i++)
printf("%5d,",a[i] );
printf("\n");
}
选择排序
void sa(int array[],int n)
{
int i,j,k,temp;
for(i=1;i<=10;i++) printf("%d ",a[i]);
printf("\n");
}
归并排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Merge(int *R,int low,int m,int high)
fr[x] = re[x] = i;
}
else {
a[re[x]].next = i;
re[x] = i;
}
}
for (i=0; i<10; i++) {
if (fr[i] != -1) {
a[re[i]].next = -1;
}
}
}
void Collect(node *a)
{
int i;
for (i=0; i<10; i++) {
fr[i] = -1;
}
for (i=head; i!=-1; i=a[i].next) {
int x = a[i].data / w % 10;
10=9(十位)
if (fr[x] == -1) {
}
}
}
a[last].next = -1;
}
void Out(node *a, int w)
{
int i, p, k;
printf("weight == %d\n", w);
for (i=0; i<10; i++) {
printf("fr[%d] ", i);
p = fr[i];
for(i=1;i<=n;i++)
scanf("%d",&r[i]);
Insertsort(r,n);
}
希尔排序
#include <stdio.h>
void shell_sort(int arr[],int size)
{
int i,j,k,temp;
for(i=size/2;i>0;i/=2)
{
while (head != -1) {
printf("%4d", a[head].data);
head = a[head].next;
}
printf("\n");
}
void main()
{
node a[10];
int i, n = 10, max;
max = 0x80000000;
srand(time(NULL));
int i;
printf("%s","随机数:");
for(i=1;i<=10;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
printf("\n");
heapsort(a,10);
printf("%s","排序后:");
}
void heapsort(int *a,int n)
{
createheap(a,n);
for(int i=n;i>=2;--i)
{
int x=a[1];
a[1]=a[i];
a[i]=x;
sift(a,1,i-1);
}
}
int main()
{
int *a;
a=new int[10];
for(i=0;i<10;i++){
k=i;
for(j=i+1;j<n;j++)
if(array[j]<array[k])
k=j;
temp=array[k];
array[k]=array[i];
array[i]=temp;
}
}
直接插入排序
#include"stdio.h"
#define MAXSIZE 20
int Insertsort(int r[],int n)
{
int i,j;
for(i=2;i<=n;i++)
{ r[0]=r[i];j=i-1;
while(r[0]<r[j])
finished=true;
else
{
a[i]=a[j];
i=j;
j=2*i;
}
}
a[i]=t;
}
void createheap(int *a,int length)
{
int n=length;
for(int i=n/2;i>=1;--i)
{
sift(a,i,n);
}
srand( (unsigned int)time(NULL) );
for (i = 0; i < 10; i++)
{
a[i] = rand() % 100;
}
MergeSort(a,low,high);
for(i=low;i<=high;i+);
C语言8大经典排序方法
冒泡排序
main()
{
int i,j,temp;
int a[10];
for(i=0;i<10;i++)
scanf ("%d,",&a[i]);
for(j=0;j<=9;j++)
{ for (i=0;i<10-j;i++)
if (a[i]>a[i+1])
{
for(j=i;j<size;j++)
{
temp=arr[j];
for(k=j-i;k>=0&&temp<arr[k];k-=i)
{
arr[k+i]=arr[k];
}
arr[k+i]=temp;
}
}
for(i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
head = 0;
a[n - 1].next = -1;
Output(a, head);
for (i=1; i<=max; i*=10) {
Distribute(a, i);
Out(a, i);
Collect(a);