十大排序算法:补充
1.bubble sort
void bubblesort(int vector[],int n)
{
for(int i=0;i<n;i++)
{
int j=n-i-1;
while(j--)
{
if(vector[j]<vetor[j-1])
int temp = vector[j];
vector[j] = vector[j-1];
vector[j-1] = temp;
}
}
}
2.
3.insertionsort
void select_sort(int A[],int n)
{
register int i,j,min,m;
for(i=0;i<n-1;i++)
{
min=i;//查找最小值
for(j=i+1;j<n;j++)
{
if(A[min]>A[j])
{
min=j;
}
}
if(min!=i)
{
swap(&A[min],&A[i]);
printf("第%d趟排序结果为:\n",i+1);
for(m=0;m<n;m++)
{
if(m>0)
{
printf("");
}
printf("%d",A[m]);
}
printf("\n");
}
}
}
void insertion_sort(int arr[],int n)
{
for(int i=0;i<n;i++)
{
int temp = arr[i];
for(int j=i;j>0&&temp<arr[j];j--)
arr[j]=arr[j-1];
arr[j] =temp;
}
}
4.shell sort
void insertion_sort(int arr[],int n)
{
do{
int gap=gap/3+1;
for(int i=1;i<n;i++)
{
if(arr[i]<a[i-gap])
int temp =arr[i];
for(int j=i-gap;temp<arr[j]&&j>0;j-=gap)
arr[j+gap]=arr[j];
arr[j+gap]=temp;
}
}while(gap>1)
}
5.Heap sort
void swap(int k[],int i, int j)
{
int temp;
temp = k[i];
k[i]=k[j];
k[j]=temp;
}
void HeapSort(int k[],int n)
{
int i;
for(i=n/2;i>0;i--)
{
Heapadjust(k,i,n);
}
for(i=n;i>1;i--)
{
swap(k,1,i)
Heapadjust(k,1,i-1);
}
}
void Heapadjust(int k[],int s,int n)
{
int i,temp=k[s] ;
for(i=2*s;i<=n;i*=2)
{
if(i<n && k[i]<k[i+1])
{
i++;
}
if (temp >= k[i])
{
break;
}
k[s] =k[i];
s=i;
}
k[s]=temp;
}
6.MergeSort
define Maxsize 10
Merge(int*list1,int list_size1,int*list2,int list_size2)
{
int temp[Maxsize];
int i=j=k=0;
while(i<list_size1 && j<list_size2)
{
if(list1[i]<list2[j])
temp[k++]=list1[i++];
else
temp[k++]=list2[j++];
while(i<list_size1)
temp[k++]=list1[i++];
while(j<list_size2)
temp[k++]=list1[j++];
for(int n=0;n<list_size1+list_size2;n++)
{
list1[n]=temp[n];
}
}
}
Mergesort(int k[],int n)
{
int *list1 = k;
int list_size1=n/2;
int *list2 = k+list_size1;
int list_size2=n-list_size1;
Mergesort(list1,list_size1);
Mergesort(list2,list_size2);
Merge(list1,list_size1,list2,list_size2);
}
7.QuickSort
int partition(int k[],int low,int high)
{
int point;
point=k[low];
while(low<high)
{
while(low<high&&k[high]>=point)
{
high--;
}
swop(k,point,k[high]);
while(low<high&&k[low]<=point)
{
low++;
}
swop(k,point,k[low]);
}
return low;
}
void Qsort(int k[],int low,int high)
{
int point;
if(low<high){
point = partition(k,low,high);
Qsort(k,low,point-1);
Qsort(k,point+1,high)
}
}
QuickSort(int k[],int n)
{
Qsort(int k[],0,int n);
}