Skip to content

Commit 3f37491

Browse files
authored
Add files via upload
0 parents  commit 3f37491

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

QuickSort.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
#Setting up parameters
3+
def QuickSort(ArrayToSort):
4+
5+
Array = ArrayToSort.copy()
6+
SortedArray = QuickSortRecursion(Array, 0, len(Array)-1)
7+
return (SortedArray)
8+
#Def QuickSort
9+
10+
11+
# Will do the recursion part
12+
def QuickSortRecursion(Array, Low, High):
13+
14+
if (Low < High):
15+
16+
Pi = Particiones(Array, Low, High)
17+
QuickSortRecursion(Array, Low, Pi - 1)
18+
QuickSortRecursion(Array, Pi+1, High)
19+
# if
20+
21+
return(Array)
22+
23+
#def QuickSortRecursion
24+
25+
26+
# Sorts the array
27+
def Particiones(Array, Low, High):
28+
29+
Lengh = len(Array)
30+
Pivot = Array[High]
31+
Small = Low - 1
32+
33+
for Loop in range(Low, High):
34+
35+
#Sets up all nums inferior to the pivot to the left of the array
36+
if (Array[Loop] < Pivot):
37+
Small = Small + 1
38+
Temp = Array[Small]
39+
Array[Small] = Array[Loop]
40+
Array[Loop] = Temp
41+
#if (Array[Loop] < Pivot)
42+
43+
#For Loop in range
44+
45+
46+
#Sets the pivot in its respective spot
47+
Temp = Array[Small + 1]
48+
Array[Small + 1] = Array[High]
49+
Array[High] = Temp
50+
51+
return (Small + 1)
52+
53+
# def Particiones
54+
55+
56+
57+
58+
# Test of the use // Feel free to delete the following lines to use the funtions only
59+
Array = [3, 2, 1, 4, 5, 14, 22, 63, 6, 53, 22, 12, 45, 213, 53, 233, 41, 98]
60+
61+
#Call the QuickSort function and send only the array to sort as parameter
62+
SortedArray = QuickSort(Array)
63+
64+
print("Original array: ", Array)
65+
print("Sorted array: ", SortedArray)
66+

0 commit comments

Comments
 (0)