#include <stdio.h>
#include <stdlib.h>
void mp(int a[], int n)
{
int s ;
for (int i = 0 ; i < n - 1 ; i++)
for (int j = 0 ; j < n -1 - i; j++)
if (a[j] > a[j+1])
s = a[j], a[j] = a[j+1], a[j+1] = s ;
}
void djh(int a[], int n)
{
int s ;
for (int i = 0 ; i < n - 1 ; i++)
for (int j = i + 1 ; j < n ; j++)
if (a[i] > a[j])
s = a[i], a[i] = a[j], a[j] = s ;
}
void sjh(int a[], int n)
{
int s ;
for (int i = 0 ; i < n / 2 ; i++)
{
for (int j = i ; j < n - i ; j++)
{
if (a[i] > a[j])
s = a[i], a[i] = a[j], a[j] = s ;
if (a[n-1-i] < a[j])
s = a[n-1-i], a[n-1-i] = a[j], a[j] = s ;
}
}
}
void dxz(int a[], int n)
{
int s ;
for (int i = 0 ; i < n - 1 ; i++)
{
int k = i ;
for (int j = i +1 ; j < n ; j++)
if (a[k] < a[j])
k = j ;
if (k != i)
s = a[k], a[k] = a[i], a[i] = s ;
}
}
void sxz(int a[], int n)
{
int s, k, m;
for (int i = 0 ; i < n / 2 ; i++)
{
s = i, k = i ;
for (int j = i + 1 ; j < n - i ; j++)
{
if (a[s] > a[j])
s = j ;
if (a[k] < a[j])
k = j ;
}
if (s == n-1-i && k == i)
m = a[s], a[s] = a[k], a[k] = m ;
else if (s == n-1-i)
{
if (s != i)
m = a[s], a[s] = a[i], a[i] = m ;
if (k != n-1-i)
m = a[k], a[k] = a[n-1-i], a[n-1-i] = m ;
}
else
{
if (k != n-1-i)
m = a[k], a[k] = a[n-1-i], a[n-1-i] = m ;
if (s != i)
m = a[s], a[s] = a[i], a[i] = m ;
}
}
}
void cr(int a[], int n)
{
int s ;
for (int i = 1 ; i < n ; i++)
{
int j = i - 1 ;
s = a[i] ;
while (j >= 0 && a[j] > s)
{
a[j+1] = a[j] ;
j-- ;
}
a[j+1] = s ;
}
}
void ps(int a[], int n, void (*p)(int a[], int n))
{
(*p)(a, n) ;
}
void main(void)
{
int n, x, q ;
printf("请输入数的个数n:\n") ;
scanf("%d", &n) ;
int a[n] ;
printf("请输入%d个数:\n", n) ;
for (int i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
printf("请选择排序方法, 输入序号:\n 1.冒泡\n 2.交换\n 3.选择\n 4.插入\n") ;
scanf("%d", &x) ;
if (x == 1) ps(a, n, mp) ;
else if (x == 4) ps(a, n, cr) ;
else if (x == 2 || x == 3)
{
printf("请选择排序具体方法, 输入序号:\n 1.单向\n 2.双向\n") ;
scanf("%d", &q) ;
if (q == 1)
{
if (x == 2) ps(a, n, djh) ;
else ps(a, n, sjh) ;
}
else if (q == 2)
{
if (x == 3) ps(a, n, dxz) ;
else ps(a, n, sxz) ;
}
else printf("输入无效\n") ;
}
else printf("输入无效\n") ;
for (int i = 0 ; i < n ; i++)
printf("%d ", a[i]) ;
}
|