主题:请教!插入排序法!
nameizhi
[专家分:70] 发布于 2006-06-01 17:04:00
谁能告诉我字符串的插入排序法怎么写?????谢谢!!!!
回复列表 (共1个回复)
沙发
chentianqiao [专家分:180] 发布于 2006-06-02 00:53:00
# include <stdio.h>
# include <string.h>
# define ROW 10
# define COLUMN 20
static void Input(char s[][COLUMN], int n);
static void Sort(char s[][COLUMN], int n);
static void Output(char s[][COLUMN], int n);
int main(void)
{
char s[ROW][COLUMN];
int n;
printf("Enter string number: ");
scanf("%d", &n);
Input(s, n); /* input every string */
Sort(s, n); /* insert sort */
printf("The after sort:\n");
Output(s, n); /* output after sort string */
return 0;
}
static void Input(char s[][COLUMN], int n) /* read every string */
{
int i;
printf("Enter every string:\n");
fflush(stdin);
for (i = 0; i < n; i++)
{
gets(s[i]);
}
}
static void Sort(char s[][COLUMN], int n)
{
int i, j;
char temp[COLUMN];
for (i = 0; i < n - 1; i++)
{
if (strcmp(s[i], s[i + 1]) > 0)
{
strcpy(temp, s[i + 1]);
for (j = i; j >= 0; j--)
{
if (strcmp(s[j], temp) > 0)
{
strcpy(s[j + 1], s[j]);
}
else
{
break;
}
}
strcpy(s[j + 1], temp);
}
}
}
static void Output(char s[][COLUMN], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%s\n", s[i]);
}
}
我来回复