Skip to content

Commit 4c0b93f

Browse files
Merge pull request raviprakashdev#24 from saksham-git/master
Added Quick Sort and Updated Readme File
2 parents 622cb5a + bf3a890 commit 4c0b93f

2 files changed

Lines changed: 77 additions & 16 deletions

File tree

Quick_sort.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Python program for implementation of Quicksort Sort
2+
3+
# This function takes last element as pivot, places
4+
# the pivot element at its correct position in sorted
5+
# array, and places all smaller (smaller than pivot)
6+
# to left of pivot and all greater elements to right
7+
# of pivot
8+
9+
10+
def partition(arr, low, high):
11+
i = (low-1) # index of smaller element
12+
pivot = arr[high] # pivot
13+
14+
for j in range(low, high):
15+
16+
# If current element is smaller than or
17+
# equal to pivot
18+
if arr[j] <= pivot:
19+
20+
# increment index of smaller element
21+
i = i+1
22+
arr[i], arr[j] = arr[j], arr[i]
23+
24+
arr[i+1], arr[high] = arr[high], arr[i+1]
25+
return (i+1)
26+
27+
# The main function that implements QuickSort
28+
# arr[] --> Array to be sorted,
29+
# low --> Starting index,
30+
# high --> Ending index
31+
32+
# Function to do Quick sort
33+
34+
35+
def quickSort(arr, low, high):
36+
if len(arr) == 1:
37+
return arr
38+
if low < high:
39+
40+
# pi is partitioning index, arr[p] is now
41+
# at right place
42+
pi = partition(arr, low, high)
43+
44+
# Separately sort elements before
45+
# partition and after partition
46+
quickSort(arr, low, pi-1)
47+
quickSort(arr, pi+1, high)
48+
49+
50+
# Driver code to test above
51+
arr = [10, 7, 8, 9, 1, 5]
52+
n = len(arr)
53+
quickSort(arr, 0, n-1)
54+
print("Sorted array is:")
55+
for i in range(n):
56+
print("%d" % arr[i]),
57+
58+
# This code is contributed by Mohit Kumra
59+
#This code in improved by https://github.com/anushkrishnav

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
# simplePythonProgram
2-
3-
This repo contains the following files:
4-
- `Bubble Sort_2.py`: a bubble sort program.
5-
- `BubbleSort.py`: another bubble sort program.
6-
- `Merge Sort.py`: a merge sort implementation program with user-received input.
7-
- `Fibonacci.py`: a program that takes number of digits given as input and generates a Fibonacci Sequence based on input.
8-
- `README.md`: this file.
9-
- `helloworld.py`: a simple hello world program.
10-
- `greet.py` : a simple program that requests user name and greets them with a specfic message.
11-
- `spam.py`: a simple program to give spam message to anyone.
12-
13-
To run spam.py install pyautogui
14-
<pre>
15-
<code>pip install pyautogui</code>
16-
</pre>
1+
# simplePythonProgram
2+
3+
This repo contains the following files:
4+
- `Bubble Sort_2.py`: a bubble sort program.
5+
- `BubbleSort.py`: another bubble sort program.
6+
- `Merge Sort.py`: a merge sort implementation program with user-received input.
7+
- `Fibonacci.py`: a program that takes number of digits given as input and generates a Fibonacci Sequence based on input.
8+
- `README.md`: this file.
9+
- `helloworld.py`: a simple hello world program.
10+
- `greet.py` : a simple program that requests user name and greets them with a specfic message.
11+
- `spam.py`: a simple program to give spam message to anyone
12+
- `Quick_sort.py`: quick sort program
13+
14+
15+
To run spam.py install pyautogui
16+
<pre>
17+
<code>pip install pyautogui</code>
18+
</pre>

0 commit comments

Comments
 (0)