-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-cache.js
5134 lines (4019 loc) · 299 KB
/
test-cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var testHtml = `<div class="inner-content clearfix">
<div id="question-header" class="grid sm:fd-column">
<h1 itemprop="name" class="grid--cell fs-headline1 fl1 ow-break-word mb8"><a href="/questions/18262306/quicksort-with-python" class="question-hyperlink">Quicksort with Python</a></h1>
<div class="ml12 aside-cta grid--cell print:d-none sm:ml0 sm:mb12 sm:order-first sm:as-end">
<a href="/questions/ask" class="ws-nowrap s-btn s-btn__primary">
Ask Question
</a>
</div>
</div>
<div class="grid fw-wrap pb8 mb16 bb bc-black-2">
<div class="grid--cell ws-nowrap mr16 mb8" title="2013-08-15 21:37:08Z">
<span class="fc-light mr2">Asked</span>
<time itemprop="dateCreated" datetime="2013-08-15T21:37:08">7 years ago</time>
</div>
<div class="grid--cell ws-nowrap mr16 mb8">
<span class="fc-light mr2">Active</span>
<a href="?lastactivity" class="s-link s-link__inherit" title="2020-06-29 08:34:14Z">2 months ago</a>
</div>
<div class="grid--cell ws-nowrap mb8" title="Viewed 190,218 times">
<span class="fc-light mr2">Viewed</span>
190k times
</div>
</div>
<div id="mainbar" role="main" aria-label="question and answers">
<div class="question" data-questionid="18262306" id="question">
<style>
</style><style class="darkreader darkreader--sync" media="screen"></style>
<div class="js-zone-container zone-container-main">
<div id="dfp-tlb" class="everyonelovesstackoverflow everyoneloves__top-leaderboard everyoneloves__leaderboard"></div>
<div class="js-report-ad-button-container " style="width: 300px"></div>
</div>
<div class="post-layout">
<div class="votecell post-layout--left">
<div class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200" data-post-id="18262306">
<button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This question shows research effort; it is useful and clear" aria-pressed="false" aria-label="Up vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button>
<div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="90">90</div>
<button class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This question does not show any research effort; it is unclear or not useful" aria-pressed="false" aria-label="Down vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowDownLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 10h32L18 26 2 10z"></path></svg></button>
<button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4 js-gps-track" data-controller="s-tooltip" data-s-tooltip-placement="right" title="Bookmark this question." aria-pressed="false" aria-label="Bookmark (54)" data-selected-classes="fc-yellow-600" data-gps-track="post.click({ item: 1, priv: 0, post_type: 1 })">
<svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg>
<div class="js-bookmark-count mt4" data-value="54">54</div>
</button>
<a class="js-post-issue grid--cell s-btn s-btn__unset c-pointer py6 mx-auto" href="/posts/18262306/timeline" data-shortcut="T" data-controller="s-tooltip" data-s-tooltip-placement="right" title="Show activity on this post." aria-label="Timeline"><svg aria-hidden="true" class="mln2 mr0 svg-icon iconHistory" width="19" height="18" viewBox="0 0 19 18"><path d="M3 9a8 8 0 113.73 6.77L8.2 14.3A6 6 0 105 9l3.01-.01-4 4-4-4h3L3 9zm7-4h1.01L11 9.36l3.22 2.1-.6.93L10 10V5z"></path></svg></a>
</div>
</div>
<div class="postcell post-layout--right">
<div class="s-prose s-prose__disable-spoiler-hover js-post-body" itemprop="text">
<p>I am totally new to python and I am trying to implement quicksort in it.
Could someone please help me complete my code?</p>
<p>I do not know how to concatenate the three arrays and printing them. </p>
<pre><code>def sort(array=[12,4,5,6,7,3,1,15]):
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
if x == pivot:
equal.append(x)
if x > pivot:
greater.append(x)
sort(less)
sort(pivot)
sort(greater)
</code></pre>
</div>
<div class="post-taglist grid gs4 gsy fd-column">
<div class="grid ps-relative d-block">
<a href="/questions/tagged/python" class="post-tag" title="show questions tagged 'python'" rel="tag">python</a> <a href="/questions/tagged/algorithm" class="post-tag" title="show questions tagged 'algorithm'" rel="tag">algorithm</a> <a href="/questions/tagged/sorting" class="post-tag" title="show questions tagged 'sorting'" rel="tag">sorting</a> <a href="/questions/tagged/quicksort" class="post-tag" title="show questions tagged 'quicksort'" rel="tag">quicksort</a>
</div>
</div>
<div class="mb0 ">
<div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4">
<div class="grid--cell mr16" style="flex: 1 1 100px;">
<div class="post-menu">
<a href="/q/18262306" rel="nofollow" itemprop="url" class="js-share-link js-gps-track" title="short permalink to this question" data-gps-track="post.click({ item: 2, priv: 0, post_type: 1 })" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this question" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="question" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="1" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">share</a>
<span class="lsep">|</span>
<a href="/posts/18262306/edit" class="suggest-edit-post js-gps-track" data-gps-track="post.click({ item: 6, priv: 0, post_type: 1 })" title="">improve this question</a>
<span class="lsep">|</span>
<button id="btnFollowPost-18262306" class="s-btn s-btn__link fc-black-400 h:fc-black-700 pb2 js-follow-post js-follow-question js-gps-track" role="button" data-gps-track="post.click({ item: 14, priv: 0, post_type: 1 })" data-controller="s-tooltip " data-s-tooltip-placement="bottom" data-s-popover-placement="bottom" aria-controls="" title="Follow this question to receive notifications">
follow
</button>
<span class="lsep">|</span>
</div>
</div>
<div class="post-signature grid--cell">
<div class="user-info user-hover">
<div class="user-action-time">
<a href="/posts/18262306/revisions" title="show all edits to this post" class="js-gps-track" data-gps-track="post.click({ item: 4, priv: 0, post_type: 1 })">edited <span title="2016-12-18 21:16:43Z" class="relativetime">Dec 18 '16 at 21:16</span></a>
</div>
<div class="user-gravatar32">
<a href="/users/541136/aaron-hall"><div class="gravatar-wrapper-32"><img src="https://i.stack.imgur.com/wftMn.jpg?s=32&g=1" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details">
<a href="/users/541136/aaron-hall">Aaron Hall</a><span class="mod-flair " title="moderator">♦</span>
<div class="-flair">
<span class="reputation-score" title="reputation score 255,561" dir="ltr">256k</span><span title="68 gold badges" aria-hidden="true"><span class="badge1"></span><span class="badgecount">68</span></span><span class="v-visible-sr">68 gold badges</span><span title="350 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">350</span></span><span class="v-visible-sr">350 silver badges</span><span title="300 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">300</span></span><span class="v-visible-sr">300 bronze badges</span>
</div>
</div>
</div> </div>
<div class="post-signature owner grid--cell">
<div class="user-info ">
<div class="user-action-time">
asked <span title="2013-08-15 21:37:08Z" class="relativetime">Aug 15 '13 at 21:37</span>
</div>
<div class="user-gravatar32">
<a href="/users/2687481/user2687481"><div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/492599f7bbc76da58c85a0c36b856e5b?s=32&d=identicon&r=PG&f=1" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<a href="/users/2687481/user2687481">user2687481</a><span class="d-none" itemprop="name">user2687481</span>
<div class="-flair">
<span class="reputation-score" title="reputation score " dir="ltr">1,007</span><span title="2 gold badges" aria-hidden="true"><span class="badge1"></span><span class="badgecount">2</span></span><span class="v-visible-sr">2 gold badges</span><span title="9 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">9</span></span><span class="v-visible-sr">9 silver badges</span><span title="7 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">7</span></span><span class="v-visible-sr">7 bronze badges</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="post-layout--right">
<div id="comments-18262306" class="comments js-comments-container bt bc-black-2 mt12 " data-post-id="18262306" data-min-length="15">
<ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true">
<li id="comment-105360887" class="comment js-comment " data-comment-id="105360887">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">To combine lists you can use plus operator <code>my_list = list1 + list2 + ...</code>. Or unpack lists to new list <code>my_list = [*list1, *list2]</code></span>
– <a href="/users/3607488/mark-mishyn" title="2,331 reputation" class="comment-user">Mark Mishyn</a>
<span class="comment-date" dir="ltr"><span title="2020-01-05 08:40:35Z, License: CC BY-SA 4.0" class="relativetime-clean">Jan 5 at 8:40</span></span>
<span title="this comment was edited 6 times">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
</ul>
</div>
<div id="comments-link-18262306" data-rep="50" data-anon="true">
<a class="js-add-link comments-link disabled-link" title="Use comments to ask for more information or suggest improvements. Avoid answering questions in comments." href="#" role="button">add a comment</a>
<span class="js-link-separator dno"> | </span>
<a class="js-show-link comments-link dno" title="expand to show all comments on this post" href="#" onclick="" role="button"></a>
</div>
</div>
</div>
</div>
<div class="js-zone-container zone-container-responsive">
<div id="dfp-isb" class="everyonelovesstackoverflow everyoneloves__inline-sidebar mx-auto"></div>
<div class="js-report-ad-button-container mx-auto" style="width: 300px"></div>
</div>
<div id="answers">
<a name="tab-top"></a>
<div id="answers-header">
<div class="answers-subheader grid ai-center mb8">
<div class="grid--cell fl1">
<h2 class="mb0" data-answercount="36">
36 Answers
<span style="display:none;" itemprop="answerCount">36</span>
</h2>
</div>
<div class="grid--cell">
<div class=" grid s-btn-group js-filter-btn">
<a class="grid--cell s-btn s-btn__muted s-btn__outlined" href="/questions/18262306/quicksort-with-python?answertab=active#tab-top" data-nav-xhref="" title="Answers with the latest activity first" data-value="active" data-shortcut="A">
Active</a>
<a class="grid--cell s-btn s-btn__muted s-btn__outlined" href="/questions/18262306/quicksort-with-python?answertab=oldest#tab-top" data-nav-xhref="" title="Answers in the order they were provided" data-value="oldest" data-shortcut="O">
Oldest</a>
<a class="youarehere is-selected grid--cell s-btn s-btn__muted s-btn__outlined" href="/questions/18262306/quicksort-with-python?answertab=votes#tab-top" data-nav-xhref="" title="Answers with the highest score first" data-value="votes" data-shortcut="V">
Votes</a>
</div>
</div>
</div>
</div>
<div class="s-pagination pager-answers">
<div class="s-pagination--item is-selected">1</div>
<a class="s-pagination--item js-pagination-item" href="/questions/18262306/quicksort-with-python?page=2&tab=votes#tab-top" rel="" title="Go to page 2">2</a>
<a class="s-pagination--item js-pagination-item" href="/questions/18262306/quicksort-with-python?page=2&tab=votes#tab-top" rel="next" title="Go to page 2"> Next</a></div>
<a name="18262384"></a>
<div id="answer-18262384" class="answer accepted-answer" data-answerid="18262384" itemprop="acceptedAnswer" itemscope="" itemtype="http://schema.org/Answer">
<div class="post-layout">
<div class="votecell post-layout--left">
<div class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200" data-post-id="18262384">
<button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is useful" aria-pressed="false" aria-label="Up vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button>
<div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="251">251</div>
<button class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is not useful" aria-pressed="false" aria-label="Down vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowDownLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 10h32L18 26 2 10z"></path></svg></button>
<div class="js-accepted-answer-indicator grid--cell fc-green-500 ta-center py4" data-s-tooltip-placement="right" title="Loading when this answer was accepted…" tabindex="0" role="note" aria-label="Accepted">
<svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="M6 14l8 8L30 6v8L14 30l-8-8v-8z"></path></svg>
</div>
<a class="js-post-issue grid--cell s-btn s-btn__unset c-pointer py6 mx-auto" href="/posts/18262384/timeline" data-shortcut="T" data-controller="s-tooltip" data-s-tooltip-placement="right" title="Show activity on this post." aria-label="Timeline"><svg aria-hidden="true" class="mln2 mr0 svg-icon iconHistory" width="19" height="18" viewBox="0 0 19 18"><path d="M3 9a8 8 0 113.73 6.77L8.2 14.3A6 6 0 105 9l3.01-.01-4 4-4-4h3L3 9zm7-4h1.01L11 9.36l3.22 2.1-.6.93L10 10V5z"></path></svg></a>
</div>
</div>
<div class="answercell post-layout--right">
<div class="s-prose s-prose__disable-spoiler-hover js-post-body" itemprop="text">
<pre><code>def sort(array=[12,4,5,6,7,3,1,15]):
"""Sort the array by using quicksort."""
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
elif x == pivot:
equal.append(x)
elif x > pivot:
greater.append(x)
# Don't forget to return something!
return sort(less)+equal+sort(greater) # Just use the + operator to join lists
# Note that you want equal ^^^^^ not pivot
else: # You need to handle the part at the end of the recursion - when you only have one element in your array, just return the array.
return array
</code></pre>
</div>
<div class="grid mb0 fw-wrap ai-start jc-end gs8 gsy">
<time itemprop="dateCreated" datetime="2013-08-15T21:42:31"></time>
<div class="grid--cell mr16" style="flex: 1 1 100px;">
<div class="post-menu">
<a href="/a/18262384" rel="nofollow" itemprop="url" class="js-share-link js-gps-track" title="short permalink to this answer" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-license-name="CC BY-SA 4.0" data-s-popover-placement="bottom-start">share</a>
<span class="lsep">|</span>
<a href="/posts/18262384/edit" class="suggest-edit-post js-gps-track" data-gps-track="post.click({ item: 6, priv: 0, post_type: 2 })" title="">improve this answer</a>
<span class="lsep">|</span>
<button id="btnFollowPost-18262384" class="s-btn s-btn__link fc-black-400 h:fc-black-700 pb2 js-follow-post js-follow-answer js-gps-track" role="button" data-gps-track="post.click({ item: 14, priv: 0, post_type: 2 })" data-controller="s-tooltip " data-s-tooltip-placement="bottom" data-s-popover-placement="bottom" aria-controls="" title="Follow this answer to receive notifications">
follow
</button>
<span class="lsep">|</span>
</div>
</div>
<div class="post-signature grid--cell fl0">
<div class="user-info ">
<div class="user-action-time">
<a href="/posts/18262384/revisions" title="show all edits to this post" class="js-gps-track" data-gps-track="post.click({ item: 4, priv: 0, post_type: 2 })">edited <span title="2019-05-12 08:18:59Z" class="relativetime">May 12 '19 at 8:18</span></a>
</div>
<div class="user-gravatar32">
<a href="/users/2135035/ahmed"><div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/3b776d90a1f60cea10dec51558e2a14c?s=32&d=identicon&r=PG" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details">
<a href="/users/2135035/ahmed">Ahmed</a>
<div class="-flair">
<span class="reputation-score" title="reputation score " dir="ltr">2,167</span><span title="1 gold badge" aria-hidden="true"><span class="badge1"></span><span class="badgecount">1</span></span><span class="v-visible-sr">1 gold badge</span><span title="17 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">17</span></span><span class="v-visible-sr">17 silver badges</span><span title="31 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">31</span></span><span class="v-visible-sr">31 bronze badges</span>
</div>
</div>
</div> </div>
<div class="post-signature grid--cell fl0">
<div class="user-info ">
<div class="user-action-time">
answered <span title="2013-08-15 21:42:31Z" class="relativetime">Aug 15 '13 at 21:42</span>
</div>
<div class="user-gravatar32">
<a href="/users/1460057/brionius"><div class="gravatar-wrapper-32"><img src="https://i.stack.imgur.com/T8zcA.jpg?s=32&g=1" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<a href="/users/1460057/brionius">Brionius</a><span class="d-none" itemprop="name">Brionius</span>
<div class="-flair">
<span class="reputation-score" title="reputation score 11,678" dir="ltr">11.7k</span><span title="2 gold badges" aria-hidden="true"><span class="badge1"></span><span class="badgecount">2</span></span><span class="v-visible-sr">2 gold badges</span><span title="27 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">27</span></span><span class="v-visible-sr">27 silver badges</span><span title="41 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">41</span></span><span class="v-visible-sr">41 bronze badges</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="post-layout--right">
<div id="comments-18262384" class="comments js-comments-container bt bc-black-2 mt12 " data-post-id="18262384" data-min-length="15">
<ul class="comments-list js-comments-list" data-remaining-comments-count="21" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true">
<li id="comment-51893103" class="comment js-comment " data-comment-id="51893103">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="warm">8</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">You could also swap out the 2nd <code>if</code>s in the for loop with <code>elif</code> and <code>else</code> to avoid doing unnecessary comparisons</span>
– <a href="/users/2620539/slimpdx" title="524 reputation" class="comment-user">SlimPDX</a>
<span class="comment-date" dir="ltr"><span title="2015-08-13 17:11:45Z, License: CC BY-SA 3.0" class="relativetime-clean">Aug 13 '15 at 17:11</span></span>
</div>
</div>
</li>
<li id="comment-54782568" class="comment js-comment " data-comment-id="54782568">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="warm">13</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">This is sound like merge sort not quick sort</span>
– <a href="/users/373051/emad-mokhtar" title="2,815 reputation" class="comment-user">Emad Mokhtar</a>
<span class="comment-date" dir="ltr"><span title="2015-11-03 12:45:56Z, License: CC BY-SA 3.0" class="relativetime-clean">Nov 3 '15 at 12:45</span></span>
<span title="this comment was edited 1 time">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
<li id="comment-56745675" class="comment js-comment " data-comment-id="56745675">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="supernova">46</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">It's actually the <b>best</b> and most readable python code I found for quicksort <b>anywhere</b>. No indices, no helper functions, clearly shows the gist of the algorithm (divide and conquer). (The default value for the array is rather unnecessary)</span>
– <a href="/users/2144401/cmantas" title="1,110 reputation" class="comment-user">cmantas</a>
<span class="comment-date" dir="ltr"><span title="2015-12-28 22:50:44Z, License: CC BY-SA 3.0" class="relativetime-clean">Dec 28 '15 at 22:50</span></span>
<span title="this comment was edited 2 times">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
<li id="comment-62292727" class="comment js-comment " data-comment-id="62292727">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="hot">19</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">@jsmedmar it will use more memory than an in place version, see suquant's answer for an in place quick sort.</span>
– <a href="/users/322909/john" title="11,432 reputation" class="comment-user">John</a>
<span class="comment-date" dir="ltr"><span title="2016-05-23 13:03:44Z, License: CC BY-SA 3.0" class="relativetime-clean">May 23 '16 at 13:03</span></span>
</div>
</div>
</li>
<li id="comment-63977550" class="comment js-comment " data-comment-id="63977550">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="warm">14</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">Very readable but does not this defeat the purpose of quick-sort since this won't achieve 'in place' sort? @RasmiRanjanNayak sort here is the user defined function (its a recursive call), not any built in function.</span>
– <a href="/users/3512788/maksood" title="893 reputation" class="comment-user">Maksood</a>
<span class="comment-date" dir="ltr"><span title="2016-07-09 07:30:42Z, License: CC BY-SA 3.0" class="relativetime-clean">Jul 9 '16 at 7:30</span></span>
<span title="this comment was edited 3 times">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
</ul>
</div>
<div id="comments-link-18262384" data-rep="50" data-anon="true">
<a class="js-add-link comments-link dno" title="Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”." href="#" role="button"></a>
<span class="js-link-separator dno"> | </span>
<a class="js-show-link comments-link " title="expand to show all comments on this post" href="#" onclick="" role="button">show <b>21</b> more comments</a>
</div>
</div>
</div>
</div>
<div class="js-zone-container zone-container-main">
<div id="dfp-mlb" class="everyonelovesstackoverflow everyoneloves__mid-leaderboard everyoneloves__leaderboard"></div>
<div class="js-report-ad-button-container " style="width: 300px"></div>
</div>
<a name="27461889"></a>
<div id="answer-27461889" class="answer" data-answerid="27461889" itemprop="suggestedAnswer" itemscope="" itemtype="http://schema.org/Answer">
<div class="post-layout">
<div class="votecell post-layout--left">
<div class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200" data-post-id="27461889">
<button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is useful" aria-pressed="false" aria-label="Up vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button>
<div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="161">161</div>
<button class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is not useful" aria-pressed="false" aria-label="Down vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowDownLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 10h32L18 26 2 10z"></path></svg></button>
<div class="js-accepted-answer-indicator grid--cell fc-green-500 ta-center py4 d-none" data-s-tooltip-placement="right" title="Loading when this answer was accepted…" tabindex="0" role="note" aria-label="Accepted">
<svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="M6 14l8 8L30 6v8L14 30l-8-8v-8z"></path></svg>
</div>
<a class="js-post-issue grid--cell s-btn s-btn__unset c-pointer py6 mx-auto" href="/posts/27461889/timeline" data-shortcut="T" data-controller="s-tooltip" data-s-tooltip-placement="right" title="Show activity on this post." aria-label="Timeline"><svg aria-hidden="true" class="mln2 mr0 svg-icon iconHistory" width="19" height="18" viewBox="0 0 19 18"><path d="M3 9a8 8 0 113.73 6.77L8.2 14.3A6 6 0 105 9l3.01-.01-4 4-4-4h3L3 9zm7-4h1.01L11 9.36l3.22 2.1-.6.93L10 10V5z"></path></svg></a>
</div>
</div>
<div class="answercell post-layout--right">
<div class="s-prose s-prose__disable-spoiler-hover js-post-body" itemprop="text">
<p>Quick sort without additional memory (in place)</p>
<p>Usage:
</p>
<pre><code>array = [97, 200, 100, 101, 211, 107]
quicksort(array)
# array -> [97, 100, 101, 107, 200, 211]
</code></pre>
<pre class="lang-py prettyprint-override"><code>def partition(array, begin, end):
pivot = begin
for i in xrange(begin+1, end+1):
if array[i] <= array[begin]:
pivot += 1
array[i], array[pivot] = array[pivot], array[i]
array[pivot], array[begin] = array[begin], array[pivot]
return pivot
def quicksort(array, begin=0, end=None):
if end is None:
end = len(array) - 1
def _quicksort(array, begin, end):
if begin >= end:
return
pivot = partition(array, begin, end)
_quicksort(array, begin, pivot-1)
_quicksort(array, pivot+1, end)
return _quicksort(array, begin, end)
</code></pre>
</div>
<div class="grid mb0 fw-wrap ai-start jc-end gs8 gsy">
<time itemprop="dateCreated" datetime="2014-12-13T17:53:52"></time>
<div class="grid--cell mr16" style="flex: 1 1 100px;">
<div class="post-menu">
<a href="/a/27461889" rel="nofollow" itemprop="url" class="js-share-link js-gps-track" title="short permalink to this answer" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">share</a>
<span class="lsep">|</span>
<a href="/posts/27461889/edit" class="suggest-edit-post js-gps-track" data-gps-track="post.click({ item: 6, priv: 0, post_type: 2 })" title="">improve this answer</a>
<span class="lsep">|</span>
<button id="btnFollowPost-27461889" class="s-btn s-btn__link fc-black-400 h:fc-black-700 pb2 js-follow-post js-follow-answer js-gps-track" role="button" data-gps-track="post.click({ item: 14, priv: 0, post_type: 2 })" data-controller="s-tooltip " data-s-tooltip-placement="bottom" data-s-popover-placement="bottom" aria-controls="" title="Follow this answer to receive notifications">
follow
</button>
<span class="lsep">|</span>
</div>
</div>
<div class="post-signature grid--cell fl0">
<div class="user-info ">
<div class="user-action-time">
<a href="/posts/27461889/revisions" title="show all edits to this post" class="js-gps-track" data-gps-track="post.click({ item: 4, priv: 0, post_type: 2 })">edited <span title="2018-01-17 16:13:43Z" class="relativetime">Jan 17 '18 at 16:13</span></a>
</div>
<div class="user-gravatar32">
</div>
<div class="user-details">
<div class="-flair">
</div>
</div>
</div> </div>
<div class="post-signature grid--cell fl0">
<div class="user-info ">
<div class="user-action-time">
answered <span title="2014-12-13 17:53:52Z" class="relativetime">Dec 13 '14 at 17:53</span>
</div>
<div class="user-gravatar32">
<a href="/users/3702918/suquant"><div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/795b1a94f672912c5f9d83f6e6ebf509?s=32&d=identicon&r=PG" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<a href="/users/3702918/suquant">suquant</a><span class="d-none" itemprop="name">suquant</span>
<div class="-flair">
<span class="reputation-score" title="reputation score " dir="ltr">1,737</span><span title="1 gold badge" aria-hidden="true"><span class="badge1"></span><span class="badgecount">1</span></span><span class="v-visible-sr">1 gold badge</span><span title="8 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">8</span></span><span class="v-visible-sr">8 silver badges</span><span title="8 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">8</span></span><span class="v-visible-sr">8 bronze badges</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="post-layout--right">
<div id="comments-27461889" class="comments js-comments-container bt bc-black-2 mt12 " data-post-id="27461889" data-min-length="15">
<ul class="comments-list js-comments-list" data-remaining-comments-count="1" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true">
<li id="comment-66765103" class="comment js-comment " data-comment-id="66765103">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="hot">23</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">This should be the selected answer, as quicksort is often the algorithm of choice (over e.g. merge sort) because it sorts in place (and still gives O(nlogn) runtime).</span>
– <a href="/users/3765905/boltzmannbrain" title="3,886 reputation" class="comment-user">BoltzmannBrain</a>
<span class="comment-date" dir="ltr"><span title="2016-09-27 20:22:50Z, License: CC BY-SA 3.0" class="relativetime-clean">Sep 27 '16 at 20:22</span></span>
</div>
</div>
</li>
<li id="comment-70763675" class="comment js-comment " data-comment-id="70763675">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="cool">3</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy"><code>if end is None:</code> is going to be checked a lot of times, and only once is it going to be <code>True</code>. You should put this in a wrapper function so it is only called once.</span>
– <a href="/users/2516916/gillespie" title="3,814 reputation" class="comment-user">Gillespie</a>
<span class="comment-date" dir="ltr"><span title="2017-01-22 03:18:30Z, License: CC BY-SA 3.0" class="relativetime-clean">Jan 22 '17 at 3:18</span></span>
</div>
</div>
</li>
<li id="comment-93149540" class="comment js-comment " data-comment-id="93149540">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">Ackchyually, bruhs, @mksteve is right and this line is incorrect. Additionally, <code>array[pivot], array[begin] = array[begin], array[pivot]</code> should replace <code>begin</code> with <code>end</code>.</span>
– <a href="/users/2486966/ryan-j-mccall" title="327 reputation" class="comment-user">Ryan J McCall</a>
<span class="comment-date" dir="ltr"><span title="2018-11-03 01:03:25Z, License: CC BY-SA 4.0" class="relativetime-clean">Nov 3 '18 at 1:03</span></span>
<span title="this comment was edited 2 times">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
<li id="comment-93184274" class="comment js-comment " data-comment-id="93184274">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="cool">2</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">Although in-place is good, this is slow and it errors out due to hitting the max recursion depth when there is a lot of items. see <a href="https://repl.it/@almenon/quicksorts?language=python3" rel="nofollow noreferrer">repl.it/@almenon/quicksorts?language=python3</a></span>
– <a href="/users/6629672/almenon" title="630 reputation" class="comment-user">Almenon</a>
<span class="comment-date" dir="ltr"><span title="2018-11-04 23:25:36Z, License: CC BY-SA 4.0" class="relativetime-clean">Nov 4 '18 at 23:25</span></span>
</div>
</div>
</li>
<li id="comment-93611890" class="comment js-comment " data-comment-id="93611890">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">@mksteve and Ryan, I tested these changes and it breaks the sortings</span>
– <a href="/users/3577695/aljgom" title="3,489 reputation" class="comment-user">aljgom</a>
<span class="comment-date" dir="ltr"><span title="2018-11-19 02:45:11Z, License: CC BY-SA 4.0" class="relativetime-clean">Nov 19 '18 at 2:45</span></span>
<span title="this comment was edited 1 time">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
</ul>
</div>
<div id="comments-link-27461889" data-rep="50" data-anon="true">
<a class="js-add-link comments-link dno" title="Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”." href="#" role="button"></a>
<span class="js-link-separator dno"> | </span>
<a class="js-show-link comments-link " title="expand to show all comments on this post" href="#" onclick="" role="button">show <b>1</b> more comment</a>
</div>
</div>
</div>
</div>
<a name="20258416"></a>
<div id="answer-20258416" class="answer" data-answerid="20258416" itemprop="suggestedAnswer" itemscope="" itemtype="http://schema.org/Answer">
<div class="post-layout">
<div class="votecell post-layout--left">
<div class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200" data-post-id="20258416">
<button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is useful" aria-pressed="false" aria-label="Up vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button>
<div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="68">68</div>
<button class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is not useful" aria-pressed="false" aria-label="Down vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowDownLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 10h32L18 26 2 10z"></path></svg></button>
<div class="js-accepted-answer-indicator grid--cell fc-green-500 ta-center py4 d-none" data-s-tooltip-placement="right" title="Loading when this answer was accepted…" tabindex="0" role="note" aria-label="Accepted">
<svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="M6 14l8 8L30 6v8L14 30l-8-8v-8z"></path></svg>
</div>
<a class="js-post-issue grid--cell s-btn s-btn__unset c-pointer py6 mx-auto" href="/posts/20258416/timeline" data-shortcut="T" data-controller="s-tooltip" data-s-tooltip-placement="right" title="Show activity on this post." aria-label="Timeline"><svg aria-hidden="true" class="mln2 mr0 svg-icon iconHistory" width="19" height="18" viewBox="0 0 19 18"><path d="M3 9a8 8 0 113.73 6.77L8.2 14.3A6 6 0 105 9l3.01-.01-4 4-4-4h3L3 9zm7-4h1.01L11 9.36l3.22 2.1-.6.93L10 10V5z"></path></svg></a>
</div>
</div>
<div class="answercell post-layout--right">
<div class="s-prose s-prose__disable-spoiler-hover js-post-body" itemprop="text">
<p>There is another concise and beautiful version</p>
<pre><code>def qsort(arr):
if len(arr) <= 1:
return arr
else:
return qsort([x for x in arr[1:] if x < arr[0]]) + \
[arr[0]] + \
qsort([x for x in arr[1:] if x >= arr[0]])
# this comment is just to improve readability due to horizontal scroll!!!
</code></pre>
<p>Let me explain the above codes for details</p>
<ol>
<li><p>pick the first element of array <code>arr[0]</code> as pivot</p>
<p><code>[arr[0]]</code></p></li>
<li><p><code>qsort</code> those elements of array which are less than pivot with <a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="noreferrer"><code>List Comprehension</code></a></p>
<p><code>qsort([x for x in arr[1:] if x < arr[0]])</code></p></li>
<li><p><code>qsort</code> those elements of array which are larger than pivot with <code>List Comprehension</code></p>
<p><code>qsort([x for x in arr[1:] if x >= arr[0]])</code></p></li>
</ol>
</div>
<div class="grid mb0 fw-wrap ai-start jc-end gs8 gsy">
<time itemprop="dateCreated" datetime="2013-11-28T05:32:06"></time>
<div class="grid--cell mr16" style="flex: 1 1 100px;">
<div class="post-menu">
<a href="/a/20258416" rel="nofollow" itemprop="url" class="js-share-link js-gps-track" title="short permalink to this answer" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-license-name="CC BY-SA 4.0" data-s-popover-placement="bottom-start">share</a>
<span class="lsep">|</span>
<a href="/posts/20258416/edit" class="suggest-edit-post js-gps-track" data-gps-track="post.click({ item: 6, priv: 0, post_type: 2 })" title="">improve this answer</a>
<span class="lsep">|</span>
<button id="btnFollowPost-20258416" class="s-btn s-btn__link fc-black-400 h:fc-black-700 pb2 js-follow-post js-follow-answer js-gps-track" role="button" data-gps-track="post.click({ item: 14, priv: 0, post_type: 2 })" data-controller="s-tooltip " data-s-tooltip-placement="bottom" data-s-popover-placement="bottom" aria-controls="" title="Follow this answer to receive notifications">
follow
</button>
<span class="lsep">|</span>
</div>
</div>
<div class="post-signature grid--cell fl0">
<div class="user-info user-hover">
<div class="user-action-time">
<a href="/posts/20258416/revisions" title="show all edits to this post" class="js-gps-track" data-gps-track="post.click({ item: 4, priv: 0, post_type: 2 })">edited <span title="2018-12-09 19:02:36Z" class="relativetime">Dec 9 '18 at 19:02</span></a>
</div>
<div class="user-gravatar32">
<a href="/users/2745140/yoonghm"><div class="gravatar-wrapper-32"><img src="https://i.stack.imgur.com/OGCDP.jpg?s=32&g=1" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details">
<a href="/users/2745140/yoonghm">yoonghm</a>
<div class="-flair">
<span class="reputation-score" title="reputation score " dir="ltr">1,880</span><span title="12 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">12</span></span><span class="v-visible-sr">12 silver badges</span><span title="28 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">28</span></span><span class="v-visible-sr">28 bronze badges</span>
</div>
</div>
</div> </div>
<div class="post-signature grid--cell fl0">
<div class="user-info user-hover">
<div class="user-action-time">
answered <span title="2013-11-28 05:32:06Z" class="relativetime">Nov 28 '13 at 5:32</span>
</div>
<div class="user-gravatar32">
<a href="/users/3011380/zangw"><div class="gravatar-wrapper-32"><img src="https://i.stack.imgur.com/oN76M.jpg?s=32&g=1" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<a href="/users/3011380/zangw">zangw</a><span class="d-none" itemprop="name">zangw</span>
<div class="-flair">
<span class="reputation-score" title="reputation score 31,449" dir="ltr">31.4k</span><span title="11 gold badges" aria-hidden="true"><span class="badge1"></span><span class="badgecount">11</span></span><span class="v-visible-sr">11 gold badges</span><span title="116 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">116</span></span><span class="v-visible-sr">116 silver badges</span><span title="137 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">137</span></span><span class="v-visible-sr">137 bronze badges</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="post-layout--right">
<div id="comments-20258416" class="comments js-comments-container bt bc-black-2 mt12 " data-post-id="20258416" data-min-length="15">
<ul class="comments-list js-comments-list" data-remaining-comments-count="3" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true">
<li id="comment-57268750" class="comment js-comment " data-comment-id="57268750">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="hot">15</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">@zangw possible reasons for a downvote: 1) Quadratic runtime on already sorted or reversed arrays 2) The solution is not in-place. Therefore, a terrible implementation, sorry.</span>
– <a href="/users/1269892/alisianoi" title="1,297 reputation" class="comment-user">alisianoi</a>
<span class="comment-date" dir="ltr"><span title="2016-01-13 08:35:52Z, License: CC BY-SA 3.0" class="relativetime-clean">Jan 13 '16 at 8:35</span></span>
<span title="this comment was edited 2 times">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
<li id="comment-59703581" class="comment js-comment " data-comment-id="59703581">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="hot">16</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">not readable at all, are you truly trying to minimize the number of lines? Code is interpreted by machines, but understood by humans.</span>
– <a href="/users/4058679/jsmedmar" title="696 reputation" class="comment-user">jsmedmar</a>
<span class="comment-date" dir="ltr"><span title="2016-03-16 03:52:23Z, License: CC BY-SA 3.0" class="relativetime-clean">Mar 16 '16 at 3:52</span></span>
</div>
</div>
</li>
<li id="comment-67112370" class="comment js-comment " data-comment-id="67112370">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="cool">4</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">@AlfredoGallegos, the whole point of quicksort is it happens in place, you may as well implement merge-sort if you are going to do this.</span>
– <a href="/users/2141635/padraic-cunningham" title="152,548 reputation" class="comment-user">Padraic Cunningham</a>
<span class="comment-date" dir="ltr"><span title="2016-10-07 09:41:43Z, License: CC BY-SA 3.0" class="relativetime-clean">Oct 7 '16 at 9:41</span></span>
<span title="this comment was edited 1 time">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
<li id="comment-84907899" class="comment js-comment " data-comment-id="84907899">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="warm">13</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">Are these comment for real? If you want performance, use <code>sorted</code>, this is clearly for educational purposes. And it is readable, more readable than the accepted answer.</span>
– <a href="/users/1006955/nobilis" title="6,270 reputation" class="comment-user">Nobilis</a>
<span class="comment-date" dir="ltr"><span title="2018-02-23 15:03:14Z, License: CC BY-SA 3.0" class="relativetime-clean">Feb 23 '18 at 15:03</span></span>
<span title="this comment was edited 1 time">
<svg aria-hidden="true" class="va-text-bottom o50 svg-icon iconPencilSm" width="14" height="14" viewBox="0 0 14 14"><path d="M11.1 1.71l1.13 1.12c.2.2.2.51 0 .71L11.1 4.7 9.21 2.86l1.17-1.15c.2-.2.51-.2.71 0zM2 10.12l6.37-6.43 1.88 1.88L3.88 12H2v-1.88z"></path></svg>
</span>
</div>
</div>
</li>
<li id="comment-86878097" class="comment js-comment " data-comment-id="86878097">
<div class="js-comment-actions comment-actions">
<div class="comment-score js-comment-edit-hide">
<span title="number of 'useful comment' votes received" class="cool">3</span>
</div>
</div>
<div class="comment-text js-comment-text-and-form">
<div class="comment-body js-comment-edit-hide">
<span class="comment-copy">FWIW, I thought this was the most readable implementation of them all. It shows the recursive structure of the algorithm better than any other answer. Of course, the performance isn't going to be too great.</span>
– <a href="/users/3847202/solveit" title="101 reputation" class="comment-user">SolveIt</a>
<span class="comment-date" dir="ltr"><span title="2018-04-19 22:25:42Z, License: CC BY-SA 3.0" class="relativetime-clean">Apr 19 '18 at 22:25</span></span>
</div>
</div>
</li>
</ul>
</div>
<div id="comments-link-20258416" data-rep="50" data-anon="true">
<a class="js-add-link comments-link dno" title="Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”." href="#" role="button"></a>
<span class="js-link-separator dno"> | </span>
<a class="js-show-link comments-link " title="expand to show all comments on this post" href="#" onclick="" role="button">show <b>3</b> more comments</a>
</div>
</div>
</div>
</div>
<div class="js-zone-container zone-container-main">
<div id="dfp-smlb" class="everyonelovesstackoverflow everyoneloves__mid-second-leaderboard everyoneloves__leaderboard"></div>
<div class="js-report-ad-button-container " style="width: 300px"></div>
</div>
<a name="31102672"></a>
<div id="answer-31102672" class="answer" data-answerid="31102672" itemprop="suggestedAnswer" itemscope="" itemtype="http://schema.org/Answer">
<div class="post-layout">
<div class="votecell post-layout--left">
<div class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200" data-post-id="31102672">
<button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is useful" aria-pressed="false" aria-label="Up vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button>
<div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="35">35</div>
<button class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" title="This answer is not useful" aria-pressed="false" aria-label="Down vote" data-selected-classes="fc-theme-primary"><svg aria-hidden="true" class="m0 svg-icon iconArrowDownLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 10h32L18 26 2 10z"></path></svg></button>
<div class="js-accepted-answer-indicator grid--cell fc-green-500 ta-center py4 d-none" data-s-tooltip-placement="right" title="Loading when this answer was accepted…" tabindex="0" role="note" aria-label="Accepted">
<svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="M6 14l8 8L30 6v8L14 30l-8-8v-8z"></path></svg>
</div>
<a class="js-post-issue grid--cell s-btn s-btn__unset c-pointer py6 mx-auto" href="/posts/31102672/timeline" data-shortcut="T" data-controller="s-tooltip" data-s-tooltip-placement="right" title="Show activity on this post." aria-label="Timeline"><svg aria-hidden="true" class="mln2 mr0 svg-icon iconHistory" width="19" height="18" viewBox="0 0 19 18"><path d="M3 9a8 8 0 113.73 6.77L8.2 14.3A6 6 0 105 9l3.01-.01-4 4-4-4h3L3 9zm7-4h1.01L11 9.36l3.22 2.1-.6.93L10 10V5z"></path></svg></a>
</div>
</div>
<div class="answercell post-layout--right">
<div class="s-prose s-prose__disable-spoiler-hover js-post-body" itemprop="text">
<p><a href="https://stackoverflow.com/a/27461889/1269892">This answer</a> is an in-place QuickSort for <code>Python 2.x</code>. My answer is an interpretation of the in-place solution from <a href="http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Python" rel="nofollow noreferrer">Rosetta Code</a> which works for <code>Python 3</code> too:</p>
<pre><code>import random
def qsort(xs, fst, lst):
'''
Sort the range xs[fst, lst] in-place with vanilla QuickSort
:param xs: the list of numbers to sort
:param fst: the first index from xs to begin sorting from,
must be in the range [0, len(xs))
:param lst: the last index from xs to stop sorting at
must be in the range [fst, len(xs))
:return: nothing, the side effect is that xs[fst, lst] is sorted
'''
if fst >= lst:
return
i, j = fst, lst
pivot = xs[random.randint(fst, lst)]
while i <= j:
while xs[i] < pivot:
i += 1
while xs[j] > pivot:
j -= 1
if i <= j:
xs[i], xs[j] = xs[j], xs[i]
i, j = i + 1, j - 1
qsort(xs, fst, j)
qsort(xs, i, lst)
</code></pre>
<p>And if you are willing to forgo the in-place property, below is yet another version which better illustrates the basic ideas behind quicksort. Apart from readability, its other advantage is that it is <em>stable</em> (equal elements appear in the sorted list in the same order that they used to have in the unsorted list). This stability property does not hold with the less memory-hungry in-place implementation presented above.</p>
<pre><code>def qsort(xs):
if not xs: return xs # empty sequence case
pivot = xs[random.choice(range(0, len(xs)))]
head = qsort([x for x in xs if x < pivot])
tail = qsort([x for x in xs if x > pivot])
return head + [x for x in xs if x == pivot] + tail
</code></pre>
</div>
<div class="grid mb0 fw-wrap ai-start jc-end gs8 gsy">
<time itemprop="dateCreated" datetime="2015-06-28T17:31:19"></time>
<div class="grid--cell mr16" style="flex: 1 1 100px;">
<div class="post-menu">
<a href="/a/31102672" rel="nofollow" itemprop="url" class="js-share-link js-gps-track" title="short permalink to this answer" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-license-name="CC BY-SA 4.0" data-s-popover-placement="bottom-start">share</a>
<span class="lsep">|</span>
<a href="/posts/31102672/edit" class="suggest-edit-post js-gps-track" data-gps-track="post.click({ item: 6, priv: 0, post_type: 2 })" title="">improve this answer</a>
<span class="lsep">|</span>
<button id="btnFollowPost-31102672" class="s-btn s-btn__link fc-black-400 h:fc-black-700 pb2 js-follow-post js-follow-answer js-gps-track" role="button" data-gps-track="post.click({ item: 14, priv: 0, post_type: 2 })" data-controller="s-tooltip " data-s-tooltip-placement="bottom" data-s-popover-placement="bottom" aria-controls="" title="Follow this answer to receive notifications">
follow
</button>
<span class="lsep">|</span>
</div>
</div>
<div class="post-signature grid--cell fl0">
<div class="user-info ">
<div class="user-action-time">
<a href="/posts/31102672/revisions" title="show all edits to this post" class="js-gps-track" data-gps-track="post.click({ item: 4, priv: 0, post_type: 2 })">edited <span title="2020-06-29 08:34:14Z" class="relativetime">Jun 29 at 8:34</span></a>
</div>
<div class="user-gravatar32">
</div>
<div class="user-details">
<div class="-flair">
</div>
</div>
</div> </div>
<div class="post-signature grid--cell fl0">
<div class="user-info ">
<div class="user-action-time">
answered <span title="2015-06-28 17:31:19Z" class="relativetime">Jun 28 '15 at 17:31</span>
</div>
<div class="user-gravatar32">
<a href="/users/1269892/alisianoi"><div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/4db01f60604ea40e5391f7875d19040a?s=32&d=identicon&r=PG" alt="" class="bar-sm" width="32" height="32"></div></a>
</div>
<div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<a href="/users/1269892/alisianoi">alisianoi</a><span class="d-none" itemprop="name">alisianoi</span>
<div class="-flair">
<span class="reputation-score" title="reputation score " dir="ltr">1,297</span><span title="2 gold badges" aria-hidden="true"><span class="badge1"></span><span class="badgecount">2</span></span><span class="v-visible-sr">2 gold badges</span><span title="20 silver badges" aria-hidden="true"><span class="badge2"></span><span class="badgecount">20</span></span><span class="v-visible-sr">20 silver badges</span><span title="38 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">38</span></span><span class="v-visible-sr">38 bronze badges</span>
</div>
</div>
</div>
</div>
</div>