希尔排序
#include<stdio.h>
void shellSort(int k[],int n){
int i,j,temp;
int gap=n;
do{
gap=gap/3+1;
for(i=gap;i<n;i++){
if(k[i]<k[i-gap]){
temp=k[i];
for(j=i-gap;k[j]>temp;j-=gap){
k[j+gap]=k[j];
}
k[j+gap]=temp;
}
}
}while(gap>1);
}
int main(){
int i,a[10]={0,3,2,1,5,6,9,8,7,4};
shellSort(a,10);
for(i=0;i<10;i++){
printf("%d ",a[i]);
}
return 0;
}
插入排序
void insertSort(int k[],int n){
int i,j,temp;
for(i=1;i<n;i++){
if(k[i]<k[i-1]){
temp=k[i];
for(j=i-1;k[j]>temp;j--){
k[j+1]=k[j];//向后移动
}
k[j+1]=temp;//插入相应的位置
}
}
}
归并排序
#include<stdio.h>
#define MAXSIZE 10
void merging(int *list1,int list1_size,int *list2,int list2_size){
int temp[MAXSIZE],m;
int i,j,k;
i=j=k=0;
while(i<list1_size&&j<list2_size){
if(list1[i]<list2[j]){
temp[k++]=list1[i++];
}else{
temp[k++]=list2[j++];
}
}
while(i<list1_size){
temp[k++]=list1[i++];
}
while(j<list2_size){
temp[k++]=list2[j++];
}
for(m=0;m<list1_size+list2_size;m++){
list1[m]=temp[m];
}
}
void MergeSort(int k[],int n){
if(n>1){
int *list1=k;
int list1_size=n/2;
int *list2=k+n/2;
int list2_size=n-list1_size;
MergeSort(list1,list1_size);
MergeSort(list2,list2_size);
merging(list1,list1_size,list2,list2_size);
}
}
int main(){
int a[10]={0,3,2,1,6,5,8,7,9,4},i;
MergeSort(a,10);
for(i=0;i<10;i++){
printf("%d ",a[i]);
}
}
堆排序
#include<stdio.h>
void swap(int k[],int i,int j){
int temp;
temp=k[i];
k[i]=k[j];
k[j]=temp;
}
void HeapAdjust(int k[],int s,int n){
int i,temp;
temp=k[s];
for(i=2*s;i<=n;i++){
if(i<n&&k[i]<k[i+1]){
i++;
}
if(temp>=k[i]){
break;
}
k[s]=k[i];
s=i;
}
k[s]=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);
}
}
int main(){
int i,a[11]={-1,1,2,3,6,0,9,8,5,4,7};
HeapSort(a,10);
for(i=1;i<=10;i++){
printf("%d ",a[i]);
}
return 0;
}
选择排序
void selectSort(int k[],int n){
int i,j,temp;
for(i=0;i<n-1;i++){
min=i;
for(j=i+1;j<n;j++){
if(k[j]<k[min]){
min=j;
}
}
if(min!=i){
temp=k[min];
k[min]=k[i];
k[i]=temp;
}
}
}
冒泡排序
#include<stdio.h>
void BubbleSort1(int k[],int n){
int i,j,temp;
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(k[i]>k[j]){
temp=k[j];
k[j]=k[i];
k[i]=temp;
}
}
}
}
void BubbleSort2(int k[],int n){
int i,j,temp,flag;
flag=1;
for(i=0;i<n-1&&flag==1;i++){
flag=0;
for(j=n-1;j>i;j--){
if(k[j-1]>k[j]){
temp=k[j];
k[j]=k[j-1];
k[j-1]=temp;
flag=1;
}
}
}
}
int main(){
int i,a[10]={2,3,4,5,1,9,7,8,6,0};
BubbleSort2(a,10);
for(i=0;i<10;i++){
printf("%d ",a[i]);
}
return 0;
}
快速排序
#include<stdio.h>
void swap(int k[],int low,int high){
int temp;
temp=k[low];
k[low]=k[high];
k[high]=temp;
}
int Partition(int k[],int low,int high){
int point;
point=k[low];
while(low<high){
while(low<high&&k[high]>=point){
high--;
}
swap(k,low,high);
while(low<high&&k[low]<=point){
low++;
}
swap(k,low,high);
}
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);
}
}
void QuickSort(int k[],int n){
QSort(k,0,n-1);
}
int main(){
int i,a[10]={5,2,6,0,3,9,1,7,4,8};
QuickSort(a,10);
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\n");
return 0;
}