Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python - python使用bubblesort对列表进行排序(python sort a list using bubblesort)

I have a diary that looks like this

(我有一本这样的日记)

  DAY 1

  Date1 2019-12-02 02:52:14.
  Date2 2019-12-02 02:51:38.
  Date3 2019-12-01 16:52:14.
  Date4 2019-12-01 07:52:14.
  Date5 2019-12-01 06:52:14.

  DAY 2

  Date6 2019-12-02 01:52:14.
  Date7 2019-12-01 01:05:26.
  Date8 2019-12-01 00:59:26.
  Date9 2019-12-01 01:28:14.
  Date20 2019-12-01 00:58:14.

I am using bubblesort to Sort the list.

(我正在使用Bubblesort对列表进行排序。)

It should give me the latest date and latest time.

(它应该给我最新的日期和最新的时间。)

So the first entry in the list would be Date1, then Date2, then Date6 then Date3 and so on.

(因此,列表中的第一个条目将是Date1,然后是Date2,然后是Date6,然后是Date3,依此类推。)

The sorted list should look like this;

(排序后的列表应如下所示;)

 Diary
 Date1 2019-12-02 02:52:14.
 Date2 2019-12-02 02:51:38.
 Date6 2019-12-02 01:52:14.

The function to create diary

(创建日记的功能)

def create_diary(P, N):
    S = []
    for i in range(0, N):
        for j in range(len(P)-2):
            for k in range(len(P)-1,0,-1):
               if P[j][0].timestamp > P[j+1][0].timestamp:
                 P[j][0],P[j+1][0] = P[j+1][0],P[j][0]
                 S.append(Posting(P[j+1][0].content, P[j+1][0].timestamp))


return S

how the function is defined in main:

(在main中如何定义函数:)

N = 3 #number of entries in the diary code
    S = create_Diary(P, N)
    print("Diary")
    for s in S:
         print(" ", s.content, s.timestamp)

However when i run this code it only prints out the first entry

(但是,当我运行此代码时,它只会打印出第一个条目)

Diary
Date1 2019-12-02 02:52:14.

Am I using the bubble-sort algorithm correctly?

(我是否正确使用冒泡排序算法?)

  ask by momonosuke translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...