Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4305a29

Browse files
authored
Merge pull requestabranhe#25 from BubbaBeans/patch-1
Create quicksort.py
2 parents1192952 +454cb10 commit4305a29

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

‎allalgorithms/sorting/quicksort.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Quick Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Brian D. Hopper
7+
# Github: @bubbabeans
8+
#
9+
defpartition(xs,start,end):
10+
follower=leader=start
11+
whileleader<end:
12+
ifxs[leader]<=xs[end]:
13+
xs[follower],xs[leader]=xs[leader],xs[follower]
14+
follower+=1
15+
leader+=1
16+
xs[follower],xs[end]=xs[end],xs[follower]
17+
returnfollower
18+
19+
def_quicksort(xs,start,end):
20+
ifstart>=end:
21+
return
22+
p=partition(xs,start,end)
23+
_quicksort(xs,start,p-1)
24+
_quicksort(xs,p+1,end)
25+
26+
defquicksort(xs):
27+
_quicksort(xs,0,len(xs)-1)
28+
29+
# To use: create a list and send it to quicksort: quicksort(list placed here)

‎docs/sorting/quicksort.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#Quicksort
2+
3+
A quicksort is a quicker method of sorting, and there are four different ways of implementing it. The example given uses a pivot point.
4+
5+
A pivot point is created in the middle of an array, and all larger items go after the pivot point, and smaller items are placed in front
6+
of the pivot point.
7+
8+
The pivot point is then moved to the middle of either the smaller or larger items, and the sort is run again on that half.
9+
10+
This continues over and over again until everything is in the proper place.
11+
12+
##Install
13+
14+
```
15+
pip install allalgorithms
16+
```
17+
18+
##Usage
19+
20+
```py
21+
from allalgorithms.sortingimport quicksort
22+
23+
arr= [77,2,10,-2,1,7]
24+
25+
print(quicksort(arr))
26+
# -> [-2, 1, 2, 7, 10, 77]
27+
```
28+
29+
##API
30+
31+
```
32+
quicksort(array)
33+
```
34+
35+
>Returns a sorted array
36+
37+
#####Params:
38+
39+
-`array`: Sorted Array

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp