Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python列表索引中的递归排序超出范围_Python_Algorithm_Sorting_Recursion - Fatal编程技术网

Python列表索引中的递归排序超出范围

Python列表索引中的递归排序超出范围,python,algorithm,sorting,recursion,Python,Algorithm,Sorting,Recursion,我试图读取一个文件并递归地对数组中的数据进行排序。但是在我运行程序之后,数据没有被排序。文本文件包含许多数字,每行一个数字。我想按升序排序。 例如: input file: 5 1 2 4 3 Expect output: 1 2 3 4 5 Actual output: 2 1 3 5 4 A=[] f=打开(系统argv[1],“r”) 对于f中的行: A.追加(行) def分配(p,r): x=A[r] i=p-1 对于范围(p,r-1)内的j: 如果(A[j]这些线: 在j=p到

我试图读取一个文件并递归地对数组中的数据进行排序。但是在我运行程序之后,数据没有被排序。文本文件包含许多数字,每行一个数字。我想按升序排序。 例如:

input file:
5
1
2
4
3

Expect output:
1
2
3
4
5

Actual output:
2
1
3
5
4
A=[]
f=打开(系统argv[1],“r”)
对于f中的行:
A.追加(行)
def分配(p,r):
x=A[r]
i=p-1
对于范围(p,r-1)内的j:
如果(A[j]这些线:

  • 在j=p到r-1的伪代码中
  • 在范围(p,r-1)内的j的python
    中:
不具有相同的含义。python
range
排除了它的第二个参数。您可能打算对range(p,r)中的j执行
以下几行:

  • 在j=p到r-1的伪代码中
  • 在范围(p,r-1)内的j的python
    中:

不具有相同的含义。python
range
排除了它的第二个参数。您可能打算对range(p,r)中的j执行

请不要将错误消息以图像形式发布,而是以文本形式发布。您的代码打开了一个文件,请提供该文件内容的示例。谢谢,我已使用内容示例编辑了此帖子。我现在遇到的问题是,在运行程序后,数据未排序。如果这恰好有帮助,则会出现此问题是或类似。youtube和其他网站上的quick sort上有大量资源。请不要将错误消息作为图像发布,而是作为文本发布。您的代码打开了一个文件,请提供该文件的内容示例。谢谢,我已使用内容示例编辑了该文章。我现在遇到的问题是数据不完整在我运行程序后,t排序。如果这恰好有用,这似乎是或类似的东西。youtube和其他地方的快速排序上有大量资源。此外,I的任务是获取较小元素的索引。同样,I的任务是获取较小元素的索引
A=[]
f = open(sys.argv[1], "r")
for row in f:
    A.append(row)
def divide(p, r):
    x = A[r]
    i =p-1
    for j in range(p,r-1):
        if (A[j] <= x):
            i+=1
            temp=A[i] 
            A[i]=A[j]
            A[j]=temp          
    temp2=A[i+1]
    A[i+1]=A[r]
    A[r]=temp2
    return i+1
    
def sort(p, r): 
    if (p < r) :
        q = divide(p,r)
        sort(p, q-1)
        sort(q+1,r)
sort(0,len(A)-1)
for a in A:
    print(a)
function sort205(p, r) {
        if (p < r) {
                q = divide(p,r);
                sort205(p, q-1);
                sort205(q+1, r);
        }
}
function divide(p, r) {
        x = A[r];
        i = p-1;

        for j = p to r-1 {
                if (A[j] <= x) {
                        i += 1;
            exchange A[i] with A[j];
                }
        }
    exchange A[i+1] with A[r]
        return (i+1);
}