Skip to content
This repository has been archived by the owner on Nov 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request #40 from ConanChou/master
Browse files Browse the repository at this point in the history
Addition of in place quick sort and some tiny optimizations.
  • Loading branch information
nryoung committed Apr 15, 2013
2 parents c54f1c0 + f2632fe commit 6937eeb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
4 changes: 2 additions & 2 deletions algorithms/sorting/heap_sort.py
Expand Up @@ -36,14 +36,14 @@ def max_heapify(seq, i, n):

def build_heap(seq):
n = len(seq) - 1
for i in range(n, -1, -1):
for i in range(n/2, -1, -1):
max_heapify(seq, i, n)


def sort(seq):
build_heap(seq)
heap_size = len(seq) - 1
for x in range(heap_size, -1, -1):
for x in range(heap_size, 0, -1):
seq[0], seq[x] = seq[x], seq[0]
heap_size = heap_size - 1
max_heapify(seq, 0, heap_size)
Expand Down
43 changes: 43 additions & 0 deletions algorithms/sorting/quick_sort_in_place.py
@@ -0,0 +1,43 @@
"""
quick_sort_in_place.py
Implementation of quick sort on a list and returns a sorted list.
In-place version.
Quick Sort Overview:
------------------------
Uses partitioning to recursively divide and sort the list
Time Complexity: O(n**2) worst case
Space Complexity: O(log n) this version
Stable: No
Psuedo Code: http://en.wikipedia.org/wiki/Quicksort#In-place_version
"""

def partition(seq, left, right, pivot_index):
pivot_value = seq[pivot_index]
seq[pivot_index], seq[right] = seq[right], seq[pivot_index]
store_index = left
for i in range( left, right ):
if seq[i] < pivot_value:
seq[i], seq[store_index] = seq[store_index], seq[i]
store_index += 1
seq[store_index], seq[right] = seq[right], seq[store_index]
return store_index

def sort(seq, left, right):
"""in-place version of quicksort"""
from random import randrange
if len(seq) <= 1:
return seq
elif left < right:
#pivot = (left+right)/2
pivot = randrange(left, right)
pivot_new_index = partition(seq, left, right, pivot)
sort(seq, left, pivot_new_index - 1)
sort(seq, pivot_new_index + 1, right)
return seq
19 changes: 18 additions & 1 deletion algorithms/tests/test_sorting.py
@@ -1,7 +1,8 @@
import random
import unittest
from ..sorting import bubble_sort, selection_sort, insertion_sort, \
merge_sort, quick_sort, heap_sort, shell_sort, comb_sort, cocktail_sort
merge_sort, quick_sort, heap_sort, shell_sort, comb_sort, cocktail_sort, \
quick_sort_in_place


class SortingAlgorithmTestCase(unittest.TestCase):
Expand Down Expand Up @@ -73,6 +74,22 @@ def test_quicksort(self):
self.assertEqual(self.correct, self.output)


class TestQuickSortInPlace(SortingAlgorithmTestCase):
"""
Tests Quick sort in place version on a small range from 0-9
also tests partition function included in quick sort
"""
def test_quicksort_in_place(self):
self.output = quick_sort_in_place.sort(self.input, 0,
len(self.input)-1)
self.assertEqual(self.correct, self.output)

def test_partition(self):
self.seq = range(10)
self.assertIs(quick_sort_in_place.partition(self.seq, 0,
len(self.seq)-1, 5), 5)


class TestHeapSort(SortingAlgorithmTestCase):
"""
Test Heap sort on a small range from 0-9
Expand Down

0 comments on commit 6937eeb

Please sign in to comment.