Skip to content

Commit

Permalink
Merge branch 'master' into VishweshPatel
Browse files Browse the repository at this point in the history
  • Loading branch information
codervishwesh authored Oct 1, 2021
2 parents 7c70ef4 + e07ca3c commit f2b2ba2
Show file tree
Hide file tree
Showing 13 changed files with 1,919 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.iml
.git/objects/
*.class
189 changes: 99 additions & 90 deletions Contributors.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
*
* In linear search algorithm, we iterate through all the elements of the array and compare it with the target, if we find the target then we return the index of the target, or else we return -1.
* @author Tumin Sheth (https://github.com/tuminzee)
* Time Complexity - O(n)
* Space Complexity - O(1)
*
* Resources
* https://en.wikipedia.org/wiki/Linear_search
* https://www.geeksforgeeks.org/linear-search/
*
*/


public class LinearSearch {
public static void main(String[] args) {
int target = 10;
int [] arr = {1,2,3,4,5,6,7,8,9,10};
int ans = Search(arr, target);
if(ans == -1 ) System.out.println("Not Found");
else System.out.println("Found at index: " + ans);
}

public static int Search(int [] arr, int target){
for(int i = 0; i < arr.length; i++){
if(arr[i] == target){
return i;
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.*;
class BubbleSort
//select a cell and compare it with next cell
{
public static void main(String args[])
{
Scanner d=new Scanner(System.in);
int i,j,t,k,min,p,n;
n=d.nextInt();
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=d.nextInt();
for(i=0;i<n;i++)//0 ->n
{
for(j=0;j<n-1-i;j++)//0 ->n-1-i
{
if(a[j]>a[j+1])//next element
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/**
*
* In selection sort algorithm, we search for the lowest element and arrange it to the proper location. We swap the current element with the next lowest number.
* @author Tumin Sheth (https://github.com/tuminzee)
* Time Complexity - O(n^2)
* Space Complexity - O(1)
*
* Resources
* https://en.wikipedia.org/wiki/Selection_sort
* https://www.geeksforgeeks.org/selection-sort/
*
*
*
*/


import java.util.Arrays;

public class SelectionSort {
public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 1};
System.out.println("Before sort\n" + Arrays.toString(arr));
Sort(arr);
System.out.println("After sort\n" +Arrays.toString(arr));

}

public static void Sort(int [] arr){
for(int i = 0; i < arr.length; i++){
int min = i;
//find the smallest element in the array
for(int j = i + 1; j < arr.length; j++){
if(arr[j] < arr[min]){
min = j;
}
}
//swap the lowest with the current element
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
38 changes: 38 additions & 0 deletions Program's_Contributed_By_Contributors/MajorityElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<bits/stdc++.h>
using namespace std;

//Finding Majority element in a given vector in O(1) space using Moore's voting algorithm
int majorityElement(vector<int>& v) {
int c1=0;
int ans1=INT_MIN;
for(int i=0;i<v.size();i++)
{
if(ans1==v[i])
{
c1++;
}
else if(ans1==INT_MIN)
{
c1=1;
ans1=v[i];
}
else
{
c1--;
if(c1==0) ans1=INT_MIN;
}
}
return ans1;
}

int main()
{
//input vector
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++)
cin>>v[i];
int ans=majorityElement(v);
cout<<ans<<endl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
clc;
f=@(x) (x^2-2);
a=input('Enter value of a=');
b=input('Enter value of b=');
N=input('Enter value of N=');

h=(b-a)/N;
for i=1:N
pr=f(a+(i-1)*h)*f(a+i*h);
if pr<0
fprintf('Interval is %f %f \n',a+(i-1)*h,a+i*h);
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections import deque

def bfs(graph,start_node,visited):
queue=deque([start_node])
visited[ord(start_node)-64]=True
while queue:
node=queue.popleft()
print(node,end=" ")
for i in graph[node]:
if not visited[ord(i)-64]:
queue.append(i)
visited(ord(i)-64)=True

# Data Example
graph = {
0:[],
'A': ['B'],
'B': ['A', 'C', 'H'],
'C': ['B', 'D'],
'D': ['C', 'E', 'G'],
'E': ['D', 'F'],
'F': ['E'],
'G': ['D'],
'H': ['B', 'I', 'J', 'M'],
'I': ['H'],
'J': ['H', 'K'],
'K': ['J', 'L'],
'L': ['K'],
'M': ['H']
}

visited_bfs=[False]*(len(graph))

bfs(graph,'A',visited_bfs)
print()
Loading

0 comments on commit f2b2ba2

Please sign in to comment.