这是一种什么样的排序算法?

时间:2021-07-12 20:00:05

This is an example of a c program to sort a list of names... I'm new to algorithms that's why I need to know what type it is! What are real life examples could I use it for too?

这是一个用于对名称列表进行排序的c程序示例...我是算法的新手,这就是我需要知道它是什么类型的原因!什么是现实生活中的例子我也可以使用它?

 #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);
}

2 个解决方案

#1


3  

I hope its a bubble sorting. And with this you can arrange numbers in ascending or descending order.

我希望它的泡沫排序。有了它,您可以按升序或降序排列数字。

#2


2  

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使用二维可变长度字符数组,而不是输入字符串。

As for the sort algorithm then it is a bad realization of the bubble sort.:)

至于排序算法那么它是冒泡排序的一个糟糕的实现。:)

#1


3  

I hope its a bubble sorting. And with this you can arrange numbers in ascending or descending order.

我希望它的泡沫排序。有了它,您可以按升序或降序排列数字。

#2


2  

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使用二维可变长度字符数组,而不是输入字符串。

As for the sort algorithm then it is a bad realization of the bubble sort.:)

至于排序算法那么它是冒泡排序的一个糟糕的实现。:)