Skip to content

Commit 12993f8

Browse files
committed
Quick Sort added
1 parent 0017565 commit 12993f8

File tree

2 files changed

+116
-86
lines changed

2 files changed

+116
-86
lines changed

Sorting/MergeSort-Copy1.ipynb

Lines changed: 0 additions & 86 deletions
This file was deleted.

Sorting/QuickSort.ipynb

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"Sorted array is:\n",
13+
"1\n",
14+
"5\n",
15+
"7\n",
16+
"8\n",
17+
"9\n",
18+
"10\n"
19+
]
20+
}
21+
],
22+
"source": [
23+
"def merge(arr, l, m, r): \n",
24+
" n1 = m - l + 1\n",
25+
" n2 = r- m \n",
26+
" \n",
27+
" # create temp arrays \n",
28+
" L = [0] * (n1) \n",
29+
" R = [0] * (n2) \n",
30+
" \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",
34+
" \n",
35+
" for j in range(0 , n2): \n",
36+
" R[j] = arr[m + 1 + j] \n",
37+
" \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",
42+
" \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",
51+
" \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",
79+
" \n",
80+
" \n",
81+
"# Driver code to test above \n",
82+
"arr = [12, 11, 13, 5, 6, 7] \n",
83+
"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",
90+
"for i in range(n): \n",
91+
" print (\"%d\" %arr[i]), "
92+
]
93+
}
94+
],
95+
"metadata": {
96+
"kernelspec": {
97+
"display_name": "Python 3",
98+
"language": "python",
99+
"name": "python3"
100+
},
101+
"language_info": {
102+
"codemirror_mode": {
103+
"name": "ipython",
104+
"version": 3
105+
},
106+
"file_extension": ".py",
107+
"mimetype": "text/x-python",
108+
"name": "python",
109+
"nbconvert_exporter": "python",
110+
"pygments_lexer": "ipython3",
111+
"version": "3.7.4"
112+
}
113+
},
114+
"nbformat": 4,
115+
"nbformat_minor": 4
116+
}

0 commit comments

Comments
 (0)