Download code

Jump to: navigation, search

Back to Quicksort_(Python,_arrays)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

build.log

1 SyntaxError: ('invalid syntax', ('/tmp/litprog908796/qsort.py', 22, 27, '    a[pivotIndex] = a[end]@ text\n'))
2 


qsort.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Quicksort_(Python,_arrays)?oldid=18499
14 
15 from array import *
16 from random import *
17 
18 def partition(a, start, end, pivotIndex):
19     low = start
20     high = end - 1  # After we remove pivot it will be one smaller
21     pivotValue = a[pivotIndex]
22     a[pivotIndex] = a[end]@ text
23 
24     while True:
25         while low <= high and a[low] < pivotValue:
26             low = low + 1
27         while low <= high and a[high] >= pivotValue:
28             high = high - 1
29         if low > high:
30             break
31         a[low], a[high] = a[high], a[low]
32     a[end] = a[low]
33     a[low] = pivotValue
34     return low
35 
36 
37 def insertionSort(a, start, end):
38     for i in xrange(start, end + 1):
39         # Insert a[i] into the sorted sublist
40         v = a[i]
41         for j in reversed(xrange(0, i)):
42             if a[j] <= v:
43                 a[j + 1] = v
44                 break
45             a[j + 1] = a[j]
46         else:
47             a[0] = v
48     return a
49 
50 def qsortRange(a, start, end):
51     if end - start + 1 < 32:
52         insertionSort(a, start, end)
53     else:
54         pivotIndex = partition(a, start, end, randint(start, end))
55         qsortRange(a, start, pivotIndex - 1)
56         qsortRange(a, pivotIndex + 1, end)
57     return a
58 def qsort(a):
59     return qsortRange(a, 0, len(a) - 1)
60 
61 def randarray(typecode, numElements, minValue, maxValue):
62     a = array(typecode)
63     for i in xrange(0, numElements):
64         a.append(randint(minValue, maxValue))
65     return a
66 
67 def checkArraySorted(a):
68     for i in xrange(1, len(a) - 1):
69         if a[i] < a[i-1]:
70             return False
71     return True
72 
73 a = randarray('i', 50, 1, 200)
74 print a
75 print qsort(a)
76 if checkArraySorted(qsort(randarray('i', 10000, 0, 999999999))):
77     print "Test passed"
78 else:
79     print "Test failed"


hijacker
hijacker
hijacker
hijacker