简体   繁体   English

从高到低排序输出

[英]Sorting output from high to low

I made a simple program that would just show the input as the output. 我编写了一个简单的程序,将输入显示为输出。 My main problem is that I want to sort the output from high to low. 我的主要问题是我想将输出从高到低排序。 Instead of being sorted from high to low, the output is just the same order as the input. 输出不是从高到低排序,而是与输入顺序相同。 Can someone check my codes and see why it is not sorting. 有人可以检查我的代码,看看为什么不排序。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define size 7
#include<stdlib.h>
struct books
{
    int profit;
};
void load(struct books b[], int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf("Enter profit:\n");
        scanf("%d", &b[i].profit );
    }
}
void print(struct books b[], int n)
{
    int i;
    for (i = 0; i<n; i++)
    {
        printf("Profit is:%d\n",b[i].profit);
    }
}
void sort(struct books b[], int n)
{
    int i; int j;
    books t;
    for (i = 0; i < n-1; i++)
        for (j = 0; j < n-1 ; j++)
            if (b[j].profit < b[j + 1].profit)
            {
                t = b[j];
                b[j] = b[j + 1];
                b[j+1] = t;
            }
}
void main()
{
    books b[size];
    load(b, size);
    print(b, size);
    sort(b, size);
    system("pause");
}

If you want to print the sorted list, you need to call sort before calling print: 如果要打印排序列表,则需要在调用print之前调用sort:

void main()
{
    books b[size];
    load(b, size);

    sort(b, size);
    print(b, size);

    system("pause");
}

Also, I think you need to define the books struct as 另外,我认为您需要将图书结构定义为

    struct books b[size];

if you want to avoid compiler errors. 如果要避免编译器错误。

Finally, to print the list from low to high rather than high to low you can either modify the sorting algorithm as suggested in the another answer, or you can modify the printing algorithm as below: 最后,要从低到高而不是从高到低打印列表,您可以按照另一个答案中的建议修改排序算法,也可以如下修改打印算法:

void print(struct books b[], int n)
{
    int i;
    for (i = n-1; i>0; i--)
    {
        printf("Profit is:%d\n",b[i].profit);
    }
}

Use something like this(inverted bubble sort): 使用这样的东西(倒泡排序):

void inverted_sort(books b[], int size){
    int profit;
    bool swap;

    do{
        swap = false;
        for (int i= 0; i < (size - 1); i++){
            if (b[i].profit < b[i + 1].profit){
                profit = b[i].profit;
                b[i].profit = b[i + 1].profit;
                b[i + 1].profit = profit;
                swap = true;
            }
        }

    } while (swap);
}

And remember to change the functions order, inverted_sort() must go before print() . 并记住要更改功能顺序, inverted_sort()必须在print()之前。

void main()
{
    books b[size];
    load(b, size);
    inverted_sort(b, size);
    print(b, size);

}

Hope this helps! 希望这可以帮助!

use this : 用这个 :

void sort(struct books b[], int n)
{
    int i; int j;
    books t;
    for (i = 0; i < n; i++)
        for (j = i + 1; j < n ; j++)
            if (b[j].profit > b[i].profit)
            {
                t = b[j];
                b[j] = b[i];
                b[i] = t;
            }
}

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

相关问题 将要显示的功能输出从高到低排序 - Sorting the output of a function to display from High to Low C中的数组从低到高排序(不使用qsort) - Sorting an Array in C from low to high (without using qsort) C链表中数字从高到低排序 - Sorting numbers high to low in C Linked List 设置输出端口高低C - Set Output Port High Low C 从低到高的位顺序/ C中具有位域的映射结构 - Bit order from low to high / Mapping structs with bitfields in C 如何将高/低字节从16位地址分频? - Ways to divide the high/low byte from a 16bit address? 在从高地址到低地址的调用堆栈中,数据元素是否从低内存地址保存到高内存地址,反之亦然? - In a call stack that grows from high to low address, are the data elements saved from low memory address to high memory address or vice versa? 具有低/高字和低/高字节的 DWORD 变量 - DWORD variable with low/high word and low/high byte 如何从高到低排列多个计数器并同时显示计数器名称标签(这是一个字符)? - How do i arrange multiple counters from high to low and showing the counter name tag aswell (which is a char)? 可以计算数组A中有多少个数字的函数,范围从低到高。 - Function that can count how many numbers in array A fall in the range from low to high.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM