GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithms/sort/quick_sort.py at master · marcosfede/algorithms

Expand file tree

/

quick_sort.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
29 lines (24 loc) · 853 Bytes
/

quick_sort.py

Copy path

File metadata and controls

29 lines (24 loc) · 853 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""https://en.wikipedia.org/wiki/Quicksort"""
def quick_sort(arr, first, last):
""" Quicksort
Complexity: best O(n) avg O(n log(n)), worst O(N^2)
"""
if first < last:
pos = partition(arr, first, last)
print(arr[first:pos - 1], arr[pos + 1:last])
# Start our two recursive calls
quick_sort(arr, first, pos - 1)
quick_sort(arr, pos + 1, last)
def partition(arr, first, last):
wall = first
for pos in range(first, last):
if arr[pos] < arr[last]: # last is the pivot
arr[pos], arr[wall] = arr[wall], arr[pos]
wall += 1
arr[wall], arr[last] = arr[last], arr[wall]
print(wall)
return wall
array = [1, 5, 65, 23, 57, 1232, -1, -5, -2, 242, 100, 4, 423, 2, 564, 9, 0, 10, 43, 64]
print(array)
quick_sort(array, 0, len(array) - 1)
print(array)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.