简体   繁体   English

这是什么样的排序算法?

[英]What kind of a sorting algorithm is this?

This is an example of ac program to sort a list of names... I'm new to algorithms that's why I need to know what type it is! 这是一个用于对名称列表进行排序的ac程序示例...我是算法的新手,这就是为什么我需要知道它是什么类型的原因! What are real life examples could I use it for too? 我也可以将其用于现实生活中的哪些例子?

 #include<stdio.h>
 #include<string.h>
 #include<stdlib.h>

 int main() {
 char *str[5], *temp;
 int i, j, n;

 printf("\nHow many names do you want to have?");
 scanf("%d", &n);

 for (i = 0; i < n; i++) {
 printf("\nEnter the name %d: ", i);
 flushall();
 gets(str[i]);
 }

 for (i = 0; i < n; i++) {
  for (j = 0; j < n - 1; j++) {
     if (strcmp(str[j], str[j + 1]) > 0) {
        strcpy(temp, str[j]);
        strcpy(str[j], str[j + 1]);
        strcpy(str[j + 1], temp);
     }
  }
}

flushall();

printf("\nSorted List : ");
for (i = 0; i < n; i++)
  puts(str[i]);

return (0);
}

I hope its a bubble sorting. 我希望它能进行气泡分类。 And with this you can arrange numbers in ascending or descending order. 这样,您可以按升序或降序排列数字。

First of all the program is invalid. 首先程序无效。 It does not allocate memory where it is going to store strings. 它不会在要存储字符串的位置分配内存。 As result it is a very risky step to ask the user how many names does he want to have because there is no space to store strings in the program.:) 结果,由于没有足够的空间在程序中存储字符串,询问用户要拥有多少个名称是非常冒险的步骤。

You could use a two-dimensional variable length character array for the strings and standard function fgets instead of gets to enter the strings. 您可以对字符串和标准函数fgets使用二维可变长度字符数组,而不要使用gets输入字符串。

As for the sort algorithm then it is a bad realization of the bubble sort.:) 至于排序算法,那就是气泡排序的不好实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM