Skip to content

Commit bd01767

Browse files
authored
Merge pull request larissalages#17 from shaswatsingh19/master
searching and sorting algorithm
2 parents 661ae36 + fbb945d commit bd01767

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

classical_algorithms/Binary search.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def binary(arr,low,high,x):
2+
while low < high:
3+
mid = low + (high-1) // 2
4+
if(arr[mid] == x):
5+
return mid
6+
elif(arr[mid] <x):
7+
low = mid + 1
8+
else:
9+
high = mid - 1
10+
return -1
11+
12+
n =int(input("Enter a sorted list of number\n"))
13+
arr =[]
14+
for i in range(n):
15+
arr.append(int(input()))
16+
17+
x =int(input("ENter the number to find\n"))
18+
19+
result = binary(arr,0,len(arr)-1,x)
20+
if(result == -1):
21+
print("Element not found")
22+
else:
23+
print("Element found at ",result+1," position")

classical_algorithms/Heap Sort.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def heapify(arr, n, i):
2+
largest = i # Initialize largest as root
3+
l = 2 * i + 1 # left = 2*i + 1
4+
r = 2 * i + 2 # right = 2*i + 2
5+
6+
# See if left child of root exists and is
7+
# greater than root
8+
if l < n and arr[i] < arr[l]:
9+
largest = l
10+
11+
# See if right child of root exists and is
12+
# greater than root
13+
if r < n and arr[largest] < arr[r]:
14+
largest = r
15+
16+
# Change root, if needed
17+
if largest != i:
18+
arr[i],arr[largest] = arr[largest],arr[i] # swap
19+
20+
# Heapify the root.
21+
heapify(arr, n, largest)
22+
23+
def heapSort(arr):
24+
n = len(arr)
25+
26+
# Build a maxheap.
27+
for i in range(n//2 - 1, -1, -1):
28+
heapify(arr, n, i)
29+
30+
# One by one extract elements
31+
for i in range(n-1, 0, -1):
32+
arr[i], arr[0] = arr[0], arr[i] # swap
33+
heapify(arr, i, 0)
34+
35+
print('Enter a list of numbers')
36+
arr = list(map(int,input().split()))
37+
heapSort(arr)
38+
print('After performing heap sort')
39+
for i in arr:
40+
print(i,end =' ')
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def linear(arr,n,x):
2+
for i in range(n):
3+
if(arr[i] == x):
4+
return i
5+
return -1
6+
7+
n = int(input("Enter the total number of elements\n"))
8+
arr =list(map(int,input().split()))
9+
x = int(input("Enter which element you want to find\n"))
10+
result = linear(arr,n,x)
11+
if (result == -1):
12+
print("Element not found")
13+
else:
14+
print("Element found at" ,result+1,"position")
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import math
2+
# taking input
3+
n = int(input())
4+
# making a list named primes
5+
# initialize primes with 1 only
6+
# here 1 represent the value is prime
7+
primes = [1]*(n+1)
8+
9+
# making first and second values as 0
10+
# as 0, 1 is not a prime and start by checking
11+
# from second value
12+
primes[0] = 0
13+
primes[1] = 0
14+
15+
# looping through 2,sqrt(n)+1 as every number
16+
# comes with 2 factor if first factor is seen then
17+
# we don't have to look for second one
18+
# in second loop we are initialize all multiple of
19+
# i in first loop
20+
for i in range(2,int(math.sqrt(n)+1)):
21+
if primes[i] ==1:
22+
for j in range(i*i,n+1,i):
23+
primes[j] = 0
24+
25+
# printing all primes
26+
for i in range(len(primes)):
27+
if primes[i] == 1:
28+
print(i,end = " ")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
n = int(input())
2+
li = []
3+
for i in range(n):
4+
m = int(input())
5+
li.append(m)
6+
7+
for i in range(n):
8+
mini = i
9+
for j in range(i+1,n):
10+
if(li[j] < li[mini]):
11+
mini = j
12+
temp = li[i]
13+
li[i] = li[mini]
14+
li[mini] = temp
15+
print(li)

0 commit comments

Comments
 (0)