冒泡排序,直接插入排序,直接选择排序,希尔排序,快速排序,堆排序
程序参考如下:
直接插入排序
include<stdio.h>
void InsertSort(int *list,int index)
{
int i,j,k;
for(i=2;i<=index;++i)
{
if(list[i]<list[i-1])
{
list[0]=list[i];
list[i]=list[i-1];
for(j=i-2;list[0]<list[j];--j)
list[j+1]=list[j];
list[j+1]=list[0];
}
printf("\nThe current sorting result:\n");
for(k=1; k<=index; k++)
printf("%d ",list[k]);
}
}
void main()
{
int list[20];
int node;
int i,index;
printf("\nPlease input the values you want to sort(exit for 0):\n");
index=0;
scanf("%d",&node);
while(node!=0)
{
list[index+1]=node;
index++;
scanf("%d",&node);
}
InsertSort(list,index);
printf("\n The Final sorting result:");
for(i=1;i<=index;i++)
printf("%d ",list[i]);
}
希尔排序
#include<stdio.h>
/*希尔排序法*/
void ShellSort(int *list,int index)
{
int i,j;
int length;
length=index/2;
while(length!=0)
{
for(i=1+length; i<=index; ++i)
{
if(list[i]<list[i-length])
{
list[0]=list[i];
for(j=i-length;j>0&&list[0]<list[j];j-=length)
list[j+length]=list[j];
list[j+length]=list[0];
}
}
length=length/2;
}
}
void main()
{
int list[20];
int node;
int i,index;
printf("\nPlease input the values you want to sort(exit for 0):\n");
index=0;
scanf("%d",&node);
while(node!=0)
{
list[index+1]=node;
index++;
scanf("%d",&node);
}
printf("The Source values:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
printf("\n"); ;
printf("index=%d",index);
ShellSort(list,index);
printf("\nThe final sorting result:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
getch();
}
快速排序
#include<stdio.h>
void bubble sort(int *list,int index)
{
int i,j;
int change=1;
int temp;
for(j=index,change=1; j>=1; j--)
{
change=0;
for(i=1; i<j; i++)
{
if(list[i]>list[i+1])
{
temp=list[i+1];
list[i+1]=list[i];
list[i]=temp;
change=1;
}
}
printf("\nThe current sortint result:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
}
}
void main()
{
int list[20];
int i,index;
int node;
printf("\nPlease input the values you want to sort(exit for 0):\n");
index=0;
scanf("%d",&node);
while(node!=0)
{
list[index+1]=node;
index++;
scanf("%d",&node);
}
printf("The Source values:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
printf("\n");
bubblesort(list,index);
printf("\nThe final sorting result:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
}
快速排序
#include<stdio.h>/*快速排序法*/
int Partition(int *list,int low,int high)
{
int pivot;
list[0]=list[low];
pivot=list[low];
while(low<high)
{
while(low<high && list[high]>=pivot)
--high;
list[low]=list[high];
while(low<high && list[low]<=pivot)
++low;
list[high]=list[low];
}
list[low]=list[0];
return low;
}
void QSort(int *list,int low,int high,int index)
{
int i;
int pivotloc;
if(low<high)
{
pivotloc=Partition(list,low,high);
QSort(list,low,pivotloc-1,index);
QSort(list,pivotloc+1,high,index);
}
printf("Current sorting result:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
printf("\n");
}
void QuickSort(int *list,int index)
{
QSort(list,1,index,index);
}
void main()
{
int list[20];
int i,index;
int node;
printf("\nPlease input the values you want to sort(exit for 0):\n");
index=0;
scanf("%d",&node);
while(node!=0)
{
list[index+1]=node;
index++;
scanf("%d",&node);
}
printf("The Source values:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
printf("\n");
QuickSort(list,index);
printf("\nThe final sorting result:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
}
堆排序
#include<stdio.h>
/*建立堆*/
void HeapAdjust(int *Heap,int root,int index)
{
int j;
int temp;
int finish;
j=2*root;
temp=Heap[root];
finish=0;
while(j<=index && finish==0)
{
if(j<index)
if(Heap[j]<Heap[j+1])
j++;
if(temp>=Heap[j])
finish=1;
else
{
Heap[j/2]=Heap[j];
j=2*j;
}
}
Heap[j/2]=temp;
}
/*进行堆排序*/
void HeapSort(int *Heap,int index)
{
int i,j,temp;
/*将二叉树转换成堆*/
for( i=(index/2); i>=1; i--)
HeapAdjust(Heap,i,index);
/*开始进行堆排序*/
for(i=index-1; i>=1;i--)
{
temp=Heap[i+1];
Heap[i+1]=Heap[1];
Heap[1]=temp;
HeapAdjust(Heap,1,i);
printf("Sorting process:\n");
for(j=1; j<=index; j++)
printf("%d ",Heap[j]);
printf("\n");
}
}
void main()
{
int list[20];
int node;
int i,index;
printf("\n Please input the values you want to sort:\n");
index=0;
scanf("%d",&node);
while(node!=0)
{
list[index+1]=node;
index++;
scanf("%d",&node);
}
printf("Source Values:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
printf("\n");
HeapSort(list,index);
printf("\nSorting result:");
for(i=1; i<=index; i++)
printf("%d ",list[i]);
printf("\n");
getch();
}