Skip to content

Commit 5aa47c0

Browse files
authored
Merge branch 'master' into master
2 parents 4e14c45 + 677ce59 commit 5aa47c0

11 files changed

Lines changed: 368 additions & 17 deletions

File tree

BinarySearch/C/BinarySearch.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#include <stdio.h>
3+
4+
int main()
5+
{
6+
int first, last, n=10, array[10],mid,search,; //n is the number of input elements
7+
8+
int array[10] //Array to store the input elements
9+
10+
printf("Enter value to find\n");
11+
scanf("%d", &search);
12+
13+
first = 0;
14+
last = n - 1;
15+
mid = (first+last)/2;
16+
17+
while (first <= last) {
18+
if (array[mid] < search)
19+
first = mid + 1;
20+
else if (array[mid] == search) {
21+
printf("%d found at location %d.\n", search, mid+1);
22+
break;
23+
}
24+
else
25+
last = mid - 1;
26+
27+
mid = (first + last)/2;
28+
}
29+
if (first > last)
30+
printf("Not found! %d is not present in the list.\n", search);
31+
32+
return 0;
33+
}

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,10 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
129129
- [mekisiel](https://github.com/mekisiel)
130130
- [tushar-dtu](https://github.com/tushar-dtu)
131131
- [AymanASamyM](https://github.com/AymanASamyM)
132+
- [arunpyasi](https://github.com/arunpyasi)
132133
- [Akos Kovacs](https://github.com/plaidshirtakos)
134+
- [AtoMc](https://github.com/AtoMc)
135+
- [robertmihai26](https://github.com/robertmihai26)
136+
- [Gowtham R](https://github.com/gowtham1997)
137+
- [SrGrace](https://github.com/SrGrace)
138+
- [d-grossman](https://github.com/d-grossman)

Doomsday/Java/Doomsday.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Doomsday {
1818
public static int dow(int y, int m, int d) {
1919
final int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
2020
y -= (m < 3) ? 1 : 0;
21-
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
21+
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
2222
}
2323

2424
/**

FloodFill/FloodFill.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import java.awt.Color;
2+
import java.awt.Graphics;
3+
import java.awt.event.KeyEvent;
4+
import java.awt.event.KeyListener;
5+
6+
import javax.swing.JFrame;
7+
8+
public class FloodFill extends JFrame{
9+
private int myPixel = 25;
10+
private int loops = 1;
11+
12+
public FloodFill() {
13+
KeyListener listener = new MyKeyListener();
14+
addKeyListener(listener);
15+
setFocusable(true);
16+
}
17+
18+
19+
private void drawGrid(Graphics g){
20+
g.setColor(Color.white);
21+
for(int i=0; i < 500; i+=myPixel){
22+
g.drawLine(0, i, 500, i);
23+
g.drawLine(i, 0, i, 500);
24+
}
25+
}
26+
27+
private Color[][] fill(int x, int y, int loops){
28+
Color[][] toFill = new Color[20][20];
29+
toFill = floodFill(x, y, toFill, loops);
30+
31+
return toFill;
32+
}
33+
34+
private Color[][] floodFill(int x, int y, Color[][] toFill, int loops){
35+
if(loops <= 0){
36+
return toFill;
37+
}
38+
if(x < 3 || x >= 17 || y < 3 || y >= 17){
39+
return toFill;
40+
}
41+
if(toFill[x][y] != null && (!toFill[x][y].equals(Color.red))){
42+
return toFill;
43+
}
44+
else{
45+
toFill[x][y] = Color.red;
46+
toFill = floodFill(x+1, y, toFill, loops-1);
47+
toFill = floodFill(x, y+1, toFill, loops-1);
48+
toFill = floodFill(x-1, y, toFill, loops-1);
49+
toFill = floodFill(x, y-1, toFill, loops-1);
50+
}
51+
return toFill;
52+
}
53+
54+
private void drawFill(Graphics g, Color[][] toFill){
55+
for(int i=0; i < toFill.length; i++){
56+
for(int j=0; j< toFill[i].length; j++){
57+
g.setColor(toFill[i][j]);
58+
g.fillRect(i*myPixel, j*myPixel, myPixel, myPixel);
59+
g.setColor(Color.black);
60+
}
61+
}
62+
}
63+
64+
public void paint(Graphics g){
65+
Color[][] toFill = fill(10,10,loops);
66+
drawFill(g, toFill);
67+
drawGrid(g);
68+
repaint();
69+
}
70+
71+
public static void main(String[] args){
72+
FloodFill frame = new FloodFill();
73+
frame.setTitle("Flood Fill");
74+
frame.setSize(500,500);
75+
frame.setVisible(true);
76+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
77+
}
78+
public class MyKeyListener implements KeyListener{
79+
@Override
80+
public void keyPressed(KeyEvent arg0) {
81+
}
82+
83+
@Override
84+
public void keyReleased(KeyEvent arg0) {
85+
if(arg0.getKeyCode() == 39){
86+
loops++;
87+
}
88+
if(arg0.getKeyCode() == 37){
89+
loops--;
90+
}
91+
System.out.println(loops);
92+
}
93+
94+
@Override
95+
public void keyTyped(KeyEvent arg0) {
96+
// TODO Auto-generated method stub
97+
98+
}
99+
}
100+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "string.h"
2+
3+
int HammingDistance(char *str1, char *str2){
4+
int i;
5+
int dist = 0;
6+
7+
if(str1 == NULL || str2 == NULL){
8+
return -1;
9+
}
10+
if(strlen(str1) != strlen(str2)){
11+
/*Strings must have the same length*/
12+
return -1;
13+
}
14+
15+
for(i = 0; i < strlen(str1); i++){
16+
if(str1[i] != str2[i]){
17+
dist++;
18+
}
19+
}
20+
21+
return dist;
22+
}

LinearSearch/C/LinearSearch.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "stdlib.h"
2+
int LinearSearch(int *array, int len, int key){
3+
int i;
4+
5+
if(array == NULL){
6+
return -1;
7+
}
8+
9+
for(i = 0; i < len; i++){
10+
if(array[i] == key){
11+
return i;
12+
}
13+
}
14+
15+
return -1;
16+
}

QuickSelect/Java/QuickSelect.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Find the kth smallest element in an unordered array.
3+
*
4+
* @author Atom
5+
* @see <a href="https://en.wikipedia.org/wiki/Quickselect">Quickselect</a>
6+
*/
7+
public class QuickSelect {
8+
9+
/**
10+
* Lomuto partition scheme
11+
*
12+
* @param arr
13+
* @param low
14+
* @param high
15+
* @return the pivot's final location
16+
*/
17+
private static <T extends Comparable<? super T>> int partition(final T[] arr, final int low, final int high) {
18+
T pivot = arr[high];
19+
int index = low;
20+
for (int i = low; i < high; i++) {
21+
if (arr[i].compareTo(pivot) <= 0) {
22+
swap(arr, i, index);
23+
index++;
24+
}
25+
}
26+
swap(arr, index, high);
27+
return index;
28+
}
29+
30+
/**
31+
* Find the kth smallest element in an unordered array.
32+
*
33+
* @param arr
34+
* @param kth from 0 to arr.length - 1
35+
* @return kth smallest element
36+
*/
37+
public static <T extends Comparable<? super T>> T select(final T[] arr, final int kth) {
38+
if (kth < 0 || kth > arr.length - 1) throw new IllegalArgumentException("elements in the array = " + arr.length + ", kth(0..length-1) = " + kth);
39+
40+
int left = 0;
41+
int right = arr.length - 1;
42+
while (right >= left) {
43+
int pivotIndex = partition(arr, left, right);
44+
if (kth == pivotIndex) {
45+
return arr[pivotIndex];
46+
} else if (kth < pivotIndex) {
47+
right = pivotIndex - 1;
48+
} else {
49+
left = pivotIndex + 1;
50+
}
51+
}
52+
return null;
53+
}
54+
55+
private static void swap(Object arr[], int i1, int i2) {
56+
if (i1 == i2) { return; }
57+
58+
Object temp = arr[i1];
59+
arr[i1] = arr[i2];
60+
arr[i2] = temp;
61+
}
62+
63+
public static void main(String[] args) {
64+
for (int kth = 0; kth < 10; kth++) {
65+
Integer[] a = { 1, 4, 2, 5, 0, 3, 9, 7, 6, 8 };
66+
System.out.println("kth(" + kth + ") smallest element = " + select(a, kth));
67+
}
68+
}
69+
70+
}

QuickSort/C++/QuickSort.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void quicksort(int num[21],int first,int last){
6+
int i, j, pivot, temp;
7+
8+
if(first<last){
9+
pivot=first;
10+
i=first;
11+
j=last;
12+
13+
while(i<j){
14+
while(num[i]<=num[pivot]&&i<last)
15+
i++;
16+
while(num[j]>num[pivot])
17+
j--;
18+
if(i<j){
19+
temp=num[i];
20+
num[i]=num[j];
21+
num[j]=temp;
22+
}
23+
}
24+
temp=num[pivot];
25+
num[pivot]=num[j];
26+
num[j]=temp;
27+
quicksort(num,first,j-1);
28+
quicksort(num,j+1,last);
29+
30+
}
31+
}
32+
33+
int main(){
34+
int i, count, num[21];
35+
36+
cout<<"Enter the number of elements you wanna enter: ";
37+
cin>>count;
38+
39+
cout<<"Enter your "<<count<<" elements: ";
40+
for(i=0;i<count;i++)
41+
cin>>num[i];
42+
43+
quicksort(num,0,count-1);
44+
45+
cout<<"Quick Sorted elements: ";
46+
for(i=0;i<count;i++)
47+
cout<<num[i]<<",";
48+
return 0;
49+
}

0 commit comments

Comments
 (0)