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
339 views
in Technique[技术] by (71.8m points)

prolog - 在序言中,如何编写Quicksort是使用第二元素作为枢纽?(In prolog, how to write Quicksort is use second element as pivot?)

In my example, I have done to use the first (left-most) element of the given list is selected as the pivot.

(在我的示例中,我已完成将给定列表的第一个(最左侧)元素用作枢轴的选择。)

qsort([],[]):- !.
qsort([Pivot|Tail],Sorted):-
        split(Pivot,Tail,Less,Greater),
        qsort(Less,SortedLess),
        qsort(Greater,SortedGreater),
        append(SortedLess,[Pivot|SortedGreater],Sorted).
split(_,[],[],[]).
split(Pivot,[X|T],[X|Less],Greater):-
        X=<Pivot,split(Pivot,T,Less,Greater).
split(Pivot,[X|T],Less,[X|Greater]):-
        X>Pivot,split(Pivot,T,Less,Greater).

However, I am wondering how to use the second element as pivot

(但是,我想知道如何使用第二个元素作为枢轴)

  ask by SkaraBrae 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

...