Skip to content

Commit 28c5b39

Browse files
authored
Merge pull request larissalages#1 from larissalages/master
Pull
2 parents 0168f00 + 0563ba3 commit 28c5b39

File tree

7 files changed

+310
-0
lines changed

7 files changed

+310
-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+

classical_algorithms/MergeSort.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
4+
5+
class MergeSort
6+
{
7+
8+
public void sort(int a[], int low, int high)
9+
{
10+
if(low<high)
11+
{
12+
int mid=(low+high)/2;
13+
sort(a,low,mid);
14+
sort(a,mid+1,high);
15+
merge(a,low,mid,high);
16+
}
17+
}
18+
19+
public void merge(int a[], int low, int mid, int high)
20+
{
21+
int h=low, i=low, j=mid+1,k;
22+
int b[]=new int[high+1];
23+
24+
while((h<=mid) && (j<=high))
25+
{
26+
if(a[h]<a[j])
27+
{
28+
b[i]=a[h];
29+
h=h+1;
30+
}
31+
else
32+
{
33+
b[i]=a[j];
34+
j=j+1;
35+
}
36+
i=i+1;
37+
}
38+
39+
40+
if(h>mid)
41+
{
42+
for(k=j;k<=high;k++)
43+
{
44+
b[i]=a[k];
45+
i=i+1;
46+
}
47+
}
48+
49+
50+
if(j>high)
51+
{
52+
for(k=h;k<=mid;k++)
53+
{
54+
b[i]=a[k];
55+
i=i+1;
56+
}
57+
}
58+
59+
for(k=low;k<=high;k++)
60+
{
61+
a[k]=b[k];
62+
}
63+
}
64+
65+
static void printArray(int a[], int n)
66+
{
67+
68+
for(int i=0;i<n;i++)
69+
{
70+
System.out.print( a[i]+ "\t");
71+
}
72+
}
73+
74+
public static void main(String args[])
75+
{
76+
try{
77+
int low,high,mid;
78+
int n,i;
79+
80+
Scanner scan= new Scanner(System.in);
81+
82+
System.out.println("How many elements do you want in the array?");
83+
n=scan.nextInt();
84+
int a[]= new int[n];
85+
System.out.println("Enter the array elements one by one");
86+
for(i=0;i<n;i++)
87+
{
88+
a[i]=scan.nextInt();
89+
}
90+
91+
System.out.println("Array before sorting is:");
92+
printArray(a, n);
93+
low=0;
94+
high=n-1;
95+
96+
MergeSort obj = new MergeSort();
97+
obj.sort(a, low, high);
98+
System.out.println();
99+
100+
System.out.println("The sorted array is: ");
101+
printArray(a, n);
102+
103+
}catch(Exception e){e.printStackTrace();}
104+
105+
}
106+
}
107+
108+
109+
110+
111+
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 = " ")

classical_algorithms/mergesort.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
void merge(int a[], int l, int m, int r)
2+
{
3+
int l1 = m - l + 1;
4+
int l2 = r - m;
5+
6+
int temp1[l1];
7+
int temp2[l2];
8+
9+
for (int i = 0; i < l1; i++)
10+
{
11+
temp1[i] = a[l + i];
12+
}
13+
for (int j = 0; j < l2; j++)
14+
{
15+
temp2[j] = a[m + j + 1];
16+
}
17+
int i = 0;
18+
int j = 0;
19+
int k = l;
20+
21+
while (i < l1 && j < l2)
22+
{
23+
if (temp1[i] < temp2[j])
24+
{
25+
a[k] = temp1[i];
26+
i++;
27+
}
28+
29+
else
30+
{
31+
a[k] = temp2[j];
32+
j++;
33+
}
34+
35+
while (i < l1)
36+
{
37+
a[k] = temp1[i];
38+
i++;
39+
k++;
40+
}
41+
42+
while (j < l2)
43+
{
44+
a[k] = temp2[j];
45+
j++;
46+
k++;
47+
}
48+
}
49+
}
50+
51+
void mergesort(int a[], int l, int r)
52+
{
53+
if (l < r)
54+
{
55+
int m = r + (l - r) / 2;
56+
57+
mergesort(a, l, m);
58+
mergesort(a, m + 1, r);
59+
60+
merge(a, l, m, r);
61+
}
62+
}
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)