Skip to content

Commit cc2dac7

Browse files
committed
QuickSort added
1 parent 12993f8 commit cc2dac7

File tree

1 file changed

+31
-58
lines changed

1 file changed

+31
-58
lines changed

Sorting/QuickSort.ipynb

Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 2,
66
"metadata": {},
77
"outputs": [
88
{
@@ -20,73 +20,46 @@
2020
}
2121
],
2222
"source": [
23-
"def merge(arr, l, m, r): \n",
24-
" n1 = m - l + 1\n",
25-
" n2 = r- m \n",
23+
"def partition(arr,low,high): \n",
24+
" i = ( low-1 ) \n",
25+
" pivot = arr[high]\n",
2626
" \n",
27-
" # create temp arrays \n",
28-
" L = [0] * (n1) \n",
29-
" R = [0] * (n2) \n",
27+
" for j in range(low , high): \n",
3028
" \n",
31-
" # Copy data to temp arrays L[] and R[] \n",
32-
" for i in range(0 , n1): \n",
33-
" L[i] = arr[l + i] \n",
29+
" # If current element is smaller than or \n",
30+
" # equal to pivot \n",
31+
" if arr[j] <= pivot: \n",
32+
" \n",
33+
" # increment index of smaller element \n",
34+
" i = i+1 \n",
35+
" arr[i],arr[j] = arr[j],arr[i] \n",
3436
" \n",
35-
" for j in range(0 , n2): \n",
36-
" R[j] = arr[m + 1 + j] \n",
37+
" arr[i+1],arr[high] = arr[high],arr[i+1] \n",
38+
" return ( i+1 ) \n",
3739
" \n",
38-
" # Merge the temp arrays back into arr[l..r] \n",
39-
" i = 0 # Initial index of first subarray \n",
40-
" j = 0 # Initial index of second subarray \n",
41-
" k = l # Initial index of merged subarray \n",
40+
"# The main function that implements QuickSort \n",
41+
"# arr[] --> Array to be sorted, \n",
42+
"# low --> Starting index, \n",
43+
"# high --> Ending index \n",
4244
" \n",
43-
" while i < n1 and j < n2 : \n",
44-
" if L[i] <= R[j]: \n",
45-
" arr[k] = L[i] \n",
46-
" i += 1\n",
47-
" else: \n",
48-
" arr[k] = R[j] \n",
49-
" j += 1\n",
50-
" k += 1\n",
45+
"# Function to do Quick sort \n",
46+
"def quickSort(arr,low,high): \n",
47+
" if low < high: \n",
5148
" \n",
52-
" # Copy the remaining elements of L[], if there \n",
53-
" # are any \n",
54-
" while i < n1: \n",
55-
" arr[k] = L[i] \n",
56-
" i += 1\n",
57-
" k += 1\n",
58-
" \n",
59-
" # Copy the remaining elements of R[], if there \n",
60-
" # are any \n",
61-
" while j < n2: \n",
62-
" arr[k] = R[j] \n",
63-
" j += 1\n",
64-
" k += 1\n",
65-
" \n",
66-
"# l is for left index and r is right index of the \n",
67-
"# sub-array of arr to be sorted \n",
68-
"def mergeSort(arr,l,r): \n",
69-
" if l < r: \n",
70-
" \n",
71-
" # Same as (l+r)/2, but avoids overflow for \n",
72-
" # large l and h \n",
73-
" m = (l+(r-1))/2\n",
74-
" \n",
75-
" # Sort first and second halves \n",
76-
" mergeSort(arr, l, m) \n",
77-
" mergeSort(arr, m+1, r) \n",
78-
" merge(arr, l, m, r) \n",
49+
" # pi is partitioning index, arr[p] is now \n",
50+
" # at right place \n",
51+
" pi = partition(arr,low,high) \n",
7952
" \n",
53+
" # Separately sort elements before \n",
54+
" # partition and after partition \n",
55+
" quickSort(arr, low, pi-1) \n",
56+
" quickSort(arr, pi+1, high) \n",
8057
" \n",
8158
"# Driver code to test above \n",
82-
"arr = [12, 11, 13, 5, 6, 7] \n",
59+
"arr = [10, 7, 8, 9, 1, 5] \n",
8360
"n = len(arr) \n",
84-
"print (\"Given array is\") \n",
85-
"for i in range(n): \n",
86-
" print (\"%d\" %arr[i]), \n",
87-
" \n",
88-
"mergeSort(arr,0,n-1) \n",
89-
"print (\"\\n\\nSorted array is\") \n",
61+
"quickSort(arr,0,n-1) \n",
62+
"print (\"Sorted array is:\") \n",
9063
"for i in range(n): \n",
9164
" print (\"%d\" %arr[i]), "
9265
]

0 commit comments

Comments
 (0)