#include<stdio.h>
#include<stdlib.h>
//第三体,整型,字符型冒泡排序,选择排序
void bubble_sort(int array[],int len);
void output_array(int array[], int len);
void char_bubble_sort(char c[], int len);//字符数组排序
void char_output(char c[], int len);//字符数组输出
void select_sort(int array[], int len);//选择排序
int main()
{
int a[20] = { 86, 76, 62, 58, 77, 85, 92, 80, 96, 88, 77, 67, 80, 68, 88, 87, 64, 59, 61, 76 };
int b[15] = { 27, 61, 49, 88, 4, 20, 28, 31, 42, 62, 64, 14, 88, 27, 73 };
char c[20] = { 's', 'o', 'r', 't', 'b', 'u', 'b', 'b', 'l', 'e', 's', 'e', 'l', 'e', 'c', 't', 'o', 'k', 'o', 'k' };
char d[15] = { 'a', 'b', 'a', 's', 'o', 'r', 't', 'b', 'u', 'b', 'b', 'l', 'e', 's', 'e' }; //自己补足
bubble_sort(a, 20); //用冒泡法按降序排序a中元素
output_array(a, 20); //输出排序后的数组
bubble_sort(b, 15); //用冒泡法按降序排序b中元素
output_array(b, 15); //输出排序后的数组
char_bubble_sort(c, 20);
char_output(c,20);
char_bubble_sort(d, 15);
char_output(d, 15);
select_sort(a, 20);
output_array(a, 20);
return 0;
}
void bubble_sort(int array[], int len)
{
int i,j,temp;
for (i = 0; i < len - 1; i++)
for (j = 0; j<len-i-1; j++)
{
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
void output_array(int array[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("%d ", array[i]);
printf("\n");
}
void char_bubble_sort(char c[], int len)
{
int i;
int *array;
array = (int*)malloc(len*sizeof(int));//动态定义len大小的整型数组
for (i = 0; i < len; i++)
array[i] = c[i];
bubble_sort(array, len);
for (i = 0; i < len; i++)
c[i] = array[i];
free(array);
}
void char_output(char c[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("%c ", c[i]);
printf("\n");
}
void select_sort(int array[],int len)
{
int i,j,temp;
for (i = 0; i < len; i++)
for (j = i; j < len;j++)
if (array[j] < array[i])
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}