Update index.md (#32513)
Added python code and mentioned randomized pivot
This commit is contained in:
		
				
					committed by
					
						
						Christopher McCormack
					
				
			
			
				
	
			
			
			
						parent
						
							76958731f0
						
					
				
				
					commit
					7b51ec6487
				
			@@ -12,7 +12,7 @@ The steps involved in Quick Sort are:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## Exmaple Implementations in Various Languages
 | 
					## Exmaple Implementations in Various Languages
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### A Quick-Sort Implementation in JavaScript:
 | 
					### Implementation in JavaScript:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```javascript
 | 
					```javascript
 | 
				
			||||||
const arr = [6, 2, 5, 3, 8, 7, 1, 4]
 | 
					const arr = [6, 2, 5, 3, 8, 7, 1, 4]
 | 
				
			||||||
@@ -75,7 +75,7 @@ quickSort(arr, 0, arr.length - 1)
 | 
				
			|||||||
console.log(arr)
 | 
					console.log(arr)
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### A Quick-Sort Implementation in C
 | 
					### Implementation in C
 | 
				
			||||||
```C
 | 
					```C
 | 
				
			||||||
#include<stdio.h>  
 | 
					#include<stdio.h>  
 | 
				
			||||||
void swap(int* a, int* b) 
 | 
					void swap(int* a, int* b) 
 | 
				
			||||||
@@ -132,7 +132,31 @@ int main()
 | 
				
			|||||||
} 
 | 
					} 
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### A Quick-Sort Implementation in MATLAB
 | 
					### Implementation in Python3
 | 
				
			||||||
 | 
					```python
 | 
				
			||||||
 | 
					import random
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					z=[random.randint(0,100) for i in range(0,20)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def quicksort(z):
 | 
				
			||||||
 | 
					    if(len(z)>1):        
 | 
				
			||||||
 | 
					        piv=int(len(z)/2)
 | 
				
			||||||
 | 
					        val=z[piv]
 | 
				
			||||||
 | 
					        lft=[i for i in z if i<val]
 | 
				
			||||||
 | 
					        mid=[i for i in z if i==val]
 | 
				
			||||||
 | 
					        rgt=[i for i in z if i>val]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        res=quicksort(lft)+mid+quicksort(rgt)
 | 
				
			||||||
 | 
					        return res
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        return z
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					ans1=quicksort(z)
 | 
				
			||||||
 | 
					print(ans1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Implementation in MATLAB
 | 
				
			||||||
```matlab
 | 
					```matlab
 | 
				
			||||||
a = [9,4,7,3,8,5,1,6,2];
 | 
					a = [9,4,7,3,8,5,1,6,2];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -163,7 +187,8 @@ end
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The space complexity of quick sort is O(n). This is an improvement over other divide and conquer sorting algorithms, which take O(nlong(n)) space. Quick sort achieves this by changing the order of elements within the given array. Compare this with the <a href='https://guide.freecodecamp.org/algorithms/sorting-algorithms/merge-sort' target='_blank' rel='nofollow'>merge sort</a> algorithm which creates 2 arrays, each length n/2, in each function call.
 | 
					The space complexity of quick sort is `O(n)`. This is an improvement over other divide and conquer sorting algorithms, which take `O(nlong(n))` space. Quick sort achieves this by changing the order of elements within the given array. Compare this with the <a href='https://guide.freecodecamp.org/algorithms/sorting-algorithms/merge-sort' target='_blank' rel='nofollow'>merge sort</a> algorithm which creates 2 arrays, each length `n/2`, in each function call.
 | 
				
			||||||
 | 
					However there does exist the problem of this sorting algorithm being of time `O(n*n)` if the pivot is always kept at the middle. This can be overcomed by utilizing a random pivot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Complexity
 | 
					## Complexity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user