简体   繁体   English

如何将这个快速排序代码更改为我想要的3个不同部分?

[英]how can I change this quicksort code into 3 different parts that I want to?

For my assignment, my code must have 3 source files: 对于我的任务,我的代码必须具有3个源文件:

main.c (Handles input and output, as well as top-level program logic.) node.h (Declares the data structure and function quicksort, which sorts a given doubly linked list with the ascending order), and printlist, which prints a linked list to the screen. main.c(处理输入和输出以及顶级程序逻辑。)node.h(声明数据结构和功能quicksort,该函数对给定的双向链表以升序进行排序)和printlist,其打印出链接列表到屏幕。 node.c (Defines the function quicksort and printlist, as declared in node.h.) node.c(定义函数quicksort和printlist,如在node.h中声明的那样。)

The main function must use scanf function call to read the input data from keybord (note that the input redirection can be used to directly read the data from a data file). main函数必须使用scanf函数调用从keybord读取输入数据(请注意,输入重定向可用于直接从数据文件读取数据)。 The number of data (in the data file) is not pre-determined. 数据数量(在数据文件中)是不确定的。

this is the code that my instructor gave me. 这是我的老师给我的代码。 I'm really confused, do I need to break the code into 3 parts, if so how? 我真的很困惑,我需要将代码分为三部分吗?

#include<stdio.h>
void qsort(int a[10], int first, int last);
int main() {

    int i, n, a[10], j, pivot, last, t;
    printf("enter the no of elements\n");
    scanf("%d", &n);
    printf("enter the elements\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
    qsort(a, 0, n - 1);
    printf("sorted elements is\n");
    for (i = 0; i < n; i++)
        printf("\n%d", a[i]);

}

void qsort(int a[10], int first, int last) {
    int i, j, t, pivot, n;
    if (first < last) {
        i = first;
        j = last;
        pivot = first;
        while (i < j) {
            while (a[i] <= a[pivot] && i < last)
                i++;
            while (a[j] > a[pivot])
                j--;
            if (i < j) {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }

        t = a[pivot];
        a[pivot] = a[j];
        a[j] = t;
        qsort(a, first, j - 1);
        qsort(a, j + 1, last);
    }
}

Although your instructor gave you a qsort that sorts an array, she expects you to implement it on linked lists. 尽管您的讲师给您提供了对数组进行排序的qsort,但她希望您在链接列表上实现它。 Maybe that's just an example of how quicksort works? 也许这只是quicksort如何工作的一个示例?

Anyway, you will probably need to copy that main into a main.c file. 无论如何,您可能需要将该main复制到main.c文件中。 That file will need to include node.h. 该文件将需要包含node.h。 Inside of it you will declare the list structures, a way to create them, quicksort and a function that prints the list on screen. 在其中,您将声明列表结构,创建它们的方法,快速排序以及在屏幕上打印列表的函数。 Inside o node.c you will implement everything you declared on node.h. 在node.c内,您将实现在node.h上声明的所有内容。

You should use the defined qsort only as a reference and you probably need to make changes to main so it creates a list you coded instead of an array. 您应该仅将定义的qsort用作参考,并且可能需要对main进行更改,以便它创建您编码的列表而不是数组。

That's what I make of your assignment, but you should probably clear things up with your instructor. 这就是我对您的作业的看法,但是您可能应该与您的老师一起清理一下。

暂无
暂无

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

相关问题 如何使用固定枢轴将迭代快速排序更改为使用随机数据块的迭代快速排序? - How do I change iterative quicksort with fixed pivot to iterative quicksort with random pivot? 我可以让代码读取用户输入的字符串并剪切它的一部分,然后将这些部分中的每一个输入到一个数组中吗? - Can i make the code read a user inputted string and cut parts of it then input each one of those parts in an array? c - 如何在C中将字符串的一部分转换为不同的整数? - How do I make parts of a string into different integers in C? 我应该如何修复此快速排序功能? - How should I fix this quicksort function? 在C语言中,如何连接不同长型的部分? - In C, how do I concatenate parts of different longs? 如何比较MPI代码两部分的运行时间? - How do I compare the runtime of two parts of MPI code? 我的快速排序算法给了我一个跟踪陷阱,我该如何解决? - My quicksort algorithm is giving me a trace trap, how can I fix it? 如何从不同部分的二进制文件读取var? (说2个字节,再从另一个地方再发送2个字节?) - How can I read a var from a binary file from different parts? (Say, 2 bytes, and another 2 bytes from another place?) 为什么我的代码不能像我想要的那样工作? 以及如何检查日期的有效性? - Why my code doesn't work like I want? And how can I check the validty of the date? 如何使用 O(n) 额外空间实现稳定的快速排序算法? - How can I implement a stable quicksort algorithm using O(n) additional space?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM