forked from Burnout-Devil/HacktoberFest-DSA2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Burnout-Devil#11 from Akash1437/main
Create quicksort.py
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void SieveOfEratosthenes(int n) | ||
{ | ||
bool prime[n+1]; | ||
memset(prime, true, sizeof(prime)); | ||
|
||
for (int p=2; p*p<=n; p++) | ||
{ | ||
if (prime[p] == true) | ||
{ | ||
for (int i=p*2; i<=n; i += p) | ||
prime[i] = false; | ||
} | ||
} | ||
for (int p=2; p<=n; p++) | ||
if (prime[p]) | ||
cout << p << " "; | ||
} | ||
|
||
int main() | ||
{ | ||
int n = 30; | ||
cout << "Following are the prime numbers smaller " | ||
<< " than or equal to " << n << endl; | ||
SieveOfEratosthenes(n); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def pushZerosToEnd(arr, n): | ||
count = 0 | ||
|
||
for i in range(n): | ||
if arr[i] != 0: | ||
arr[count] = arr[i] | ||
count+=1 | ||
|
||
while count < n: | ||
arr[count] = 0 | ||
count += 1 | ||
|
||
arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9] | ||
n = len(arr) | ||
pushZerosToEnd(arr, n) | ||
print("Array after pushing all zeros to end of array:") | ||
print(arr) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def partition(array,low,high): | ||
|
||
pivot= array[high] | ||
|
||
i=low-1 | ||
|
||
for j in range(low,high): | ||
if array[j] <= pivot: | ||
|
||
i=i+1 | ||
(array[i],array[j]) = (array[high],array[i+1]) | ||
|
||
return i+1 | ||
def quickSort(array,low,high): | ||
if low<high: | ||
|
||
pi = partition(array,low,pi-1) | ||
|
||
quickSort(array,low,pi - 1) | ||
|
||
quickSort(array,pi+1,high) | ||
|
||
data=[8,7,2,1,0,9,6] | ||
print("Unsorted Array) | ||
print(data) | ||
|
||
size =len(data) | ||
|
||
quickSort(data,0,size-1) | ||
print('sorted array is') | ||
print(data) | ||
|
||
|