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
def count(a,x, left, right): | |
count = 0 | |
for i in range(left, right+1): | |
if a[i] == x: | |
count+=1 | |
return count | |
def maj(a,left, right): | |
if left == right: | |
return a[left] | |
middle = (left+right)//2 |
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
def partition(a,p,r): | |
pivot = p | |
for i in range(p+1,r+1): | |
if(a[i]<a[pivot]): | |
tmp = a[i] | |
a[i] = a[pivot+1] | |
a[pivot+1] = a[pivot] | |
a[pivot] = tmp | |
pivot = pivot + 1 | |
return pivot |
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
# >> Code for quick sort | |
def partition(a,p,r): | |
pivot = p | |
for i in range(p+1,r+1): | |
if(a[i]<a[pivot]): | |
tmp = a[i] | |
a[i] = a[pivot+1] | |
a[pivot+1] = a[pivot] | |
a[pivot] = tmp | |
pivot = pivot + 1 |
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
def areCircular(s1,s2): | |
result = False | |
size = len(s1) | |
nbPermut = size | |
tmp = s1 | |
while( not result and nbPermut > 0): | |
circularPermutation = [] | |
for i in range(size): | |
circularPermutation.append(tmp[(i - 1)%size]) | |
tmp = ''.join(circularPermutation) |
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
def bubble(tab): | |
permutation = True | |
while(permutation): | |
permutation = False | |
for i in range(0,len(tab)-1): | |
if(tab[i] > tab[i+1]): | |
tmp = tab[i] | |
tab[i] = tab[i+1] | |
tab[i+1] = tmp | |
permutation = True |
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
def selection(array): | |
for i in range(len(array)): | |
key = i | |
for j in range(i+1, len(array)): | |
if(array[key] > array[j]): | |
key = j | |
tmp = array[i] | |
array[i] = array[key] | |
array[key] = tmp |
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
def insertion(array): | |
for i in range(1, len(array)): | |
key = array[i] | |
j = i-1 | |
while(j>-1 and array[j] > key): | |
array[j+1] = array[j] | |
j-=1 | |
array[j+1] = key |
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
def merge(array, p, q, r): | |
sub1 = array[p:q+1] | |
sub2 = array[q+1:r+1] | |
k, i, j = p, 0, 0 | |
for k in range(p,r+1): | |
if(i > len(sub1) - 1): | |
array[k] = sub2[j] | |
j+=1 | |
elif(j > len(sub2) - 1): | |
array[k] = sub1[i] |