- 论坛徽章:
- 0
|
冒泡排序、选择排序、插入排序
#include stdio.h>
#include string.h>
int strsrc[10] = {10, 1, 2, 5, 3, 9, 11, 12, 20, 9};
int str[10];
void load_str(void)
{
int len = sizeof(str);
memcpy(str, strsrc, len);
}
void swap(int* x, int* y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void exch(int* x, int* y)
{
if(*x > *y) swap(x, y);
}
void sort_print(void)
{
int i;
int len = sizeof(str) / 4;
for(i = 0;i len; i++) printf("%d ", str);
printf("\n");
}
void sort_bubble(void)
{
int i, j, l;
l = sizeof(str) / 4;
for(i = 0; i l; i++)
{
for(j = l - 1; j > i; j--)
{
exch(&str, &str[j]);
}
}
}
void sort_select(void)
{
int i, j, l;
int min;
int index = 0;
l = sizeof(str) / 4;
for(i = 0; i l; i++)
{
min = str;
for(j = i + 1; j l; j++)
{
if(str[j] min)
{
min = str[j];
index = j;
}
}
swap(&str, &str[index]);
}
}
void sort_insert(void)
{
int i, j, l;
l = sizeof(str) / 4;
for(i = 1; i l; i++)
{
for(j = 0; j i; j++)
{
if(str str[j])
{
swap(&str, &str[j]);
}
}
}
}
int main()
{
printf("------------------------------sort_bubble------------------------------\n");
load_str();
sort_print();
sort_bubble();
sort_print();
printf("------------------------------sort_select------------------------------\n");
load_str();
sort_print();
sort_select();
sort_print();
printf("------------------------------sort_insert------------------------------\n");
load_str();
sort_print();
sort_select();
sort_print();
return 0;
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/99982/showart_2126444.html |
|