Skip to content

Commit b38e3eb

Browse files
committed
Heap Sort added
1 parent cc2dac7 commit b38e3eb

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

Sorting/QuickSort-Copy1.ipynb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 3,
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 partition(arr,low,high): \n",
24+
" i = ( low-1 ) \n",
25+
" pivot = arr[high]\n",
26+
" \n",
27+
" for j in range(low , high): \n",
28+
" if arr[j] <= pivot: \n",
29+
" i = i+1 \n",
30+
" arr[i],arr[j] = arr[j],arr[i] \n",
31+
" arr[i+1],arr[high] = arr[high],arr[i+1] \n",
32+
" return ( i+1 ) \n",
33+
" \n",
34+
"def quickSort(arr,low,high): \n",
35+
" if low < high:\n",
36+
" pi = partition(arr,low,high) \n",
37+
" quickSort(arr, low, pi-1) \n",
38+
" quickSort(arr, pi+1, high) \n",
39+
" \n",
40+
"\n",
41+
"arr = [10, 7, 8, 9, 1, 5] \n",
42+
"n = len(arr) \n",
43+
"quickSort(arr,0,n-1) \n",
44+
"print (\"Sorted array is:\") \n",
45+
"for i in range(n): \n",
46+
" print (\"%d\" %arr[i]), "
47+
]
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "Python 3",
53+
"language": "python",
54+
"name": "python3"
55+
},
56+
"language_info": {
57+
"codemirror_mode": {
58+
"name": "ipython",
59+
"version": 3
60+
},
61+
"file_extension": ".py",
62+
"mimetype": "text/x-python",
63+
"name": "python",
64+
"nbconvert_exporter": "python",
65+
"pygments_lexer": "ipython3",
66+
"version": "3.7.4"
67+
}
68+
},
69+
"nbformat": 4,
70+
"nbformat_minor": 4
71+
}

Sorting/QuickSort.ipynb

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 2,
5+
"execution_count": 3,
66
"metadata": {},
77
"outputs": [
88
{
@@ -25,37 +25,19 @@
2525
" pivot = arr[high]\n",
2626
" \n",
2727
" for j in range(low , high): \n",
28-
" \n",
29-
" # If current element is smaller than or \n",
30-
" # equal to pivot \n",
3128
" if arr[j] <= pivot: \n",
32-
" \n",
33-
" # increment index of smaller element \n",
3429
" i = i+1 \n",
3530
" arr[i],arr[j] = arr[j],arr[i] \n",
36-
" \n",
3731
" arr[i+1],arr[high] = arr[high],arr[i+1] \n",
3832
" return ( i+1 ) \n",
3933
" \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",
44-
" \n",
45-
"# Function to do Quick sort \n",
4634
"def quickSort(arr,low,high): \n",
47-
" if low < high: \n",
48-
" \n",
49-
" # pi is partitioning index, arr[p] is now \n",
50-
" # at right place \n",
35+
" if low < high:\n",
5136
" pi = partition(arr,low,high) \n",
52-
" \n",
53-
" # Separately sort elements before \n",
54-
" # partition and after partition \n",
5537
" quickSort(arr, low, pi-1) \n",
5638
" quickSort(arr, pi+1, high) \n",
5739
" \n",
58-
"# Driver code to test above \n",
40+
"\n",
5941
"arr = [10, 7, 8, 9, 1, 5] \n",
6042
"n = len(arr) \n",
6143
"quickSort(arr,0,n-1) \n",

0 commit comments

Comments
 (0)