Skip to content

Commit 6e71f8c

Browse files
imkakaMadhavBahl
authored andcommitted
Quick Sort (#233)
* Add @imkaka as a contributor * day4 * Merge Sort
1 parent 115c8d6 commit 6e71f8c

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

day35/C++/quickSort.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @author : imkaka
3+
* @date : 6/2/2019
4+
*/
5+
6+
#include<iostream>
7+
8+
using namespace std;
9+
10+
void swap(int& a, int& b){
11+
int temp = a;
12+
a = b;
13+
b = temp;
14+
}
15+
16+
void quickSort(int arr[], int left, int right){
17+
18+
if(right - left <= 1)
19+
return;
20+
21+
//Pivot arr[left]
22+
int yellow = left +1;
23+
24+
for(int green = left+1; green < right; ++green){
25+
if(arr[green] < arr[left]){
26+
swap(arr[yellow], arr[green]);
27+
yellow++;
28+
}
29+
}
30+
31+
//Move Pivot to original place
32+
swap(arr[left], arr[yellow-1]);
33+
34+
quickSort(arr, left, yellow-1);
35+
quickSort(arr, yellow, right);
36+
}
37+
38+
void print(int arr[],int size){
39+
for(int i = 0; i < size; ++i){
40+
cout << arr[i] << " ";
41+
}
42+
cout << endl;
43+
}
44+
45+
int main(){
46+
47+
int arr[] = {10, 25, 0, 23, 36, -1, 3, 1, 6};
48+
int size = sizeof(arr)/sizeof(arr[0]);
49+
50+
cout << "Before Sorting: ";
51+
print(arr, size);
52+
53+
quickSort(arr, 0, size);
54+
55+
cout << "After Sorting: ";
56+
print(arr, size);
57+
}

day35/Python/quick_sort.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
@author : imkaka
3+
@date : 5/2/2019
4+
"""
5+
6+
import sys
7+
8+
9+
def QuickSort(A, l, r): # sort A[l:r]
10+
if r - l <= 1:
11+
return()
12+
13+
#Pivot = A[l]
14+
yellow = l + 1
15+
16+
for green in range(l + 1, r):
17+
if(A[green] <= A[l]):
18+
(A[yellow], A[green]) = (A[green], A[yellow])
19+
yellow += 1
20+
# Move Pivot into place
21+
(A[l], A[yellow - 1]) = (A[yellow - 1], A[l])
22+
23+
QuickSort(A, l, yellow - 1)
24+
QuickSort(A, yellow, r)
25+
26+
27+
def main():
28+
A = [20, 34, 1, 3, -2, 34, 65, 100, 0]
29+
print('Before Sorting', A)
30+
print(len(A))
31+
QuickSort(A, 0, len(A))
32+
33+
print('After Sorting', A)
34+
35+
36+
if __name__ == '__main__':
37+
main()

day35/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,108 @@ output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
4444
```js
4545
to be added
4646
```
47+
48+
### [C++ Implementation](./C++/quickSort.cpp)
49+
50+
```cpp
51+
/*
52+
* @author : imkaka
53+
* @date : 6/2/2019
54+
*/
55+
56+
#include<iostream>
57+
58+
using namespace std;
59+
60+
void swap(int& a, int& b){
61+
int temp = a;
62+
a = b;
63+
b = temp;
64+
}
65+
66+
void quickSort(int arr[], int left, int right){
67+
68+
if(right - left <= 1)
69+
return;
70+
71+
//Pivot arr[left]
72+
int yellow = left +1;
73+
74+
for(int green = left+1; green < right; ++green){
75+
if(arr[green] < arr[left]){
76+
swap(arr[yellow], arr[green]);
77+
yellow++;
78+
}
79+
}
80+
81+
//Move Pivot to original place
82+
swap(arr[left], arr[yellow-1]);
83+
84+
quickSort(arr, left, yellow-1);
85+
quickSort(arr, yellow, right);
86+
}
87+
88+
void print(int arr[],int size){
89+
for(int i = 0; i < size; ++i){
90+
cout << arr[i] << " ";
91+
}
92+
cout << endl;
93+
}
94+
95+
int main(){
96+
97+
int arr[] = {10, 25, 0, 23, 36, -1, 3, 1, 6};
98+
int size = sizeof(arr)/sizeof(arr[0]);
99+
100+
cout << "Before Sorting: ";
101+
print(arr, size);
102+
103+
quickSort(arr, 0, size);
104+
105+
cout << "After Sorting: ";
106+
print(arr, size);
107+
}
108+
```
109+
110+
### [Python Implementation](./Python/quick_sort.py)
111+
112+
```py
113+
"""
114+
@author : imkaka
115+
@date : 5/2/2019
116+
"""
117+
118+
import sys
119+
120+
121+
def QuickSort(A, l, r): # sort A[l:r]
122+
if r - l <= 1:
123+
return()
124+
125+
#Pivot = A[l]
126+
yellow = l + 1
127+
128+
for green in range(l + 1, r):
129+
if(A[green] <= A[l]):
130+
(A[yellow], A[green]) = (A[green], A[yellow])
131+
yellow += 1
132+
# Move Pivot into place
133+
(A[l], A[yellow - 1]) = (A[yellow - 1], A[l])
134+
135+
QuickSort(A, l, yellow - 1)
136+
QuickSort(A, yellow, r)
137+
138+
139+
def main():
140+
A = [20, 34, 1, 3, -2, 34, 65, 100, 0]
141+
print('Before Sorting', A)
142+
print(len(A))
143+
QuickSort(A, 0, len(A))
144+
145+
print('After Sorting', A)
146+
147+
148+
if __name__ == '__main__':
149+
main()
150+
```
151+

0 commit comments

Comments
 (0)