if(a[i]>a[i+1]) //若大小顺序不符,就交换
{t=a[i]; a[i]=a[i+1]; a[i+1]=t;
选择排序:
int main(void)
{int i,j,k,a[10];
for (i=0;i<10;i++)
scanf("%d",&a[i]);
for (i=0;i<9;i++)
{k=i;
for (j=i+1;j<10;++j)
if (a[k]>a[j]) k=j;
{j=a[i];
a[i]=a[k];
a[k]=j;
for (i=0;i<10;++i) printf("%d ",a[i]);
}冒泡排序:
int main(void)
{int i,j,k,a[10];
for (i=0;i<10;i++)
scanf("%d",&a[i]);
for (i=0;i<9;i++)
{for (j=0;j<8-i;++j)
if (a[j]>a[j+1])
{k=a[j];
a[j]=a[j+1];
a[j+1]=k;
for (i=0;i<10;++i) printf("%d ",a[i]);
以前写的,有5种排序,自己研究一下吧。
#include
#include
#include
#define MAXSIZE 20
typedef int KeyType;
typedef char InfoType;
typedef struct
{KeyType key;
InfoType data;
}RedType;
typedef struct
{RedType r[MAXSIZE + 1];
int length;
}SqList;
//折半插入排序
void BInsertSort(SqList &L)
{int i, j, low, high, m;
for(i = 2;i <= L.length; i++)
{L.r[0] = L.r[i];
low = 1;
high = i - 1;
while(low <= high)
{m=(low + high) / 2;
if(L.r[0].key < L.r[m].key)
high = m - 1;
else
low = m + 1;
}for(j = i - 1; j >= high + 1; j--)
L.r[j + 1] = L.r[j];
L.r[high + 1] = L.r[0];
for(int k = 1; k <= L.length; k++)
printf("%2d", L.r[k].key);
printf("n");
//冒泡排序
void BubbleSort(SqList &L)
{int i, j, k, n;
RedType temp;
n=L.length;
for(i = 1; i <= n - 1; i++)
{for(j = n; j > i; j--)
if(L.r[j].key < L.r[j-1].key)
{temp = L.r[j];
L.r[j] = L.r[j - 1];
L.r[j - 1]=temp;
}for(k = 1;k <= L.length; k++)
printf("%2d",L.r[k].key);
printf("n");
//快exit(0);速排序
void QuickSort(SqList &L, int s, int t)
{int i = s, j = t, k;
RedType temp;
if(s < t)
{temp = L.r[s];
while(i != j)
{while(j > i && L.r[j].key >= temp.key)
if(i < j)
{L.r[i] = L.r[j];
i++;
}while(j > i && L.r[i].key <= temp.key)
i++;
if(i < j)
{L.r[j] = L.r[i];
L.r[i] = temp;
for(k = 1; k <= L.length; k++)
printf("%2d", L.r[k].key);
printf("n");
QuickSort(L, s, i - 1);
QuickSort(L, i + 1, t);
//简单选择排序
int SelectMinkey(SqList &L, int i)
{int k = L.r[i].key;
int count = i;
for(int j = i; j <= L.length; j++)
{if (k < L.r[j].key)
{}
else
{count = j;
k = L.r[j].key;
return count;
}void SelectSort(SqList &L)
{int i,j;
RedType temp;
{j = SelectMinkey(L, i);
if(i != j)
L.r[i] = L.r[j];
L.r[j] = temp;
}for(int k = 1; k <= L.length; k++)
printf("%2d", L.r[k].key);
printf("n");
//归并排序
{RedType r1;
int i = low, j = mid + 1, k = 0; //k是r1的下标,i,j分别为段和第二段的下标
r1 = (RedType )malloc((high - low + 1)sizeof(RedType));
while(i <= mid && j <= high)//在段和第二段都没扫描完时循环
if(r[i].key <= r[j].key) //将段中的记录放入r1中
{r1[k] = r[i];
i++;
k++;
}else //将第二段中的记录放到r1中
{r1[k] = r[j];
j++;
k++;
}while(i <= mid) //将段剩余部分到r1
{r1[k] = r[i];
i++;
k++;
}while(j <= high) //将第二段剩余部分到r1中
{r1[k] = r[j];
j++;
k++;
}for(k = 0, i = low; i <= high; k++,i++) //将r1回r中
r[i] = r1[k];
}void MergePass(RedType r[], int length, int n) //实现一趟归并
{int i;
for(i = 0; i + 2 length - 1 < n;i = i + 2 length) //归并length长的两个相邻子表
Merge(r, i, i + length - 1, i + 2 length - 1);
if(i + length - 1 < n) //余下2个子表,后者长度小于length
Merge(r, i, i + length - 1, n - 1); //归并这2个表
}void MergeSort(RedType r[], int n) //二路归并排序
{int length, k, i = 1;//i用于累计归并趟数
for(length = 1; length < n; length = 2 length)
{MergePass(r, length, n);
printf("第%d趟归并:n",i++);
for(k = 0; k < n; k++)
printf("%2d", r[k].key);
printf("n");
//堆排序
void DispHeap(RedType r[],int i,int n)
{if(i <= n)
printf("%d", r[i].key); //输出根结点
if(2 i <= n || 2 i + 1 < n)
{printf("(");
if(2 i <= n)
DispHeap(r, 2 i, n); //递归调用输出左子树
printf(",");
if(2 i + 1 <= n)
DispHeap(r, 2 i + 1, n); //递归调用输出右子树
printf(")");
void Shift(RedType r[], int low, int high) //调整堆
{int i = low, j = 2 i; //r[j]是r[i]的左孩子
RedType temp = r[i];
while(j <= high)
{if(j < high && r[j].key < r[j + 1].key) //若右孩子大,把j指向右孩子
j++; //变为2i+1
if(temp.key < r[j].key)
{r[i] = r[j]; //将r[i]调整到双亲结点位置
i = j; //修改i,j,以便继续向下筛选
}else break;
}r[i] = temp;
}void HeapSort(RedType r[], int n) //堆排序
{int i;
RedType temp;
for(i = n / 2; i >= 1; i--) //循环建立初始堆
Shift(r, i, n);
printf("初始堆:n");
DispHeap(r, 1, n); //输出初始堆
printf("n");
for(i = n; i >= 2; i--) //进行n-1次循环,完成堆排序
{printf("交换%d与%d,输出%dn",r[i].key, r[1].key, r[1].key);
temp = r[1]; //将个元素与当前区间内r[1]对换
r[1] = r[i];
r[i] = temp;
Shift(r, 1, i - 1); //筛选r[1]结点,得到i-1个结点的堆
printf("筛选调整得到堆:");
DispHeap(r, 1, i - 1);
printf("n"); //输出每一趟的排序结果
void main()
{SqList L;
L.length = 10;
KeyType a[] = {9,8,7,6,5,4,3,2,1,0};
RedType r[MAXSIZE];
int i, j, k, cho;
printf("常见排序方法演示n");
printf("1、折半插入排序n2、冒泡排序n3、快速排序n4、简单选择排序n5、归并排序n6、堆排序n0、退出n");
while(true)
{scanf("%d", &cho);
for(i = 1; i <= L.length; i++)
L.r[i].key = a[i - 1];
printf("初始关键字为: ");
switch(cho)
{case 1:
printf("n");
BInsertSort(L);
printf("折半排序后为:n");
break;
case 2:
printf("n");
BubbleSort(L);
printf("冒泡排序后为:n");
break;
case 3:
printf("n");
QuickSort(L,1,10);
printf("快速排序后为n");
break;
cright--;ase 4:
printf("n");
printf("简单选择排序:n");
break;
case 5:
for(i = 0; i < L.length; i++)
r[i].key = a[i];
printf("n");
MergeSort(r, L.length);
printf("归并排序后为n");
break;
case 6:
for(i = 1; i <= L.length; i++)
r[i].key=a[i - 1];
printf("n");
for(i = L.length / 2; i >= 1; i--)
Shift(r, i, L.length);
HeapSort(r, L.length);
printf("堆排序后为:n");
break;
case 0:
break;
printf("选择有误请重新选择!n");
break;
}for(k = 1; k <= L.length; k++)
printf("%2d", L.r[k].key);
printf("n");