@@ -44,3 +44,108 @@ output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
4444``` js
4545to 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