Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update codebases #254

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Calculator.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import java.util.Scanner;

//Calculator in java for Calculations

public class calculator {
public static void main(String[] args) {

char operator;
Double number1, number2, result;
Scanner input = new Scanner(System.in);
System.out.println("Choose an operator: +, -, *, / or ^ ");
System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
System.out.println("Enter first number");
number1 = input.nextDouble();
Expand All @@ -21,7 +19,6 @@ public static void main(String[] args) {
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;

case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
Expand All @@ -31,17 +28,11 @@ public static void main(String[] args) {
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;

case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;

case '^':
result = Math.pow(number1, number2);
System.out.println(number1 + " ^ " + number2 + " = " + result);
break;


default:
System.out.println("Invalid operator!");
break;
Expand Down
100 changes: 50 additions & 50 deletions HeapSort.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
#include<bits/stdc++.h>
using namespace std ;
void heapify(int arr[], int n, int i)
{
int largest= i;
int l=2*i+1;
int r= 2*i+2 ;
if (l<n&& arr[l]>arr[largest])
{
largest= l;
}
if (r<n && arr[r]> arr[largest])
{
largest= r;
}
if (largest!=i)
{
swap(arr[largest],arr[i]);
heapify(arr,n,largest);
}
}
void heapsort(int arr[], int n )
{
int i;
for ( i = n/2-1; i >=0; i--)
{
heapify(arr,n,i);
}
for ( i = n-1 ; i> 0; i--)
{
swap(arr[0],arr[i]);
heapify(arr,i,0);
}
}
int main()
{
int arr[]={24,44,65,5,52,5,24}, n= sizeof(arr)/sizeof(int);
int i ;
cout<<"Array inserted : ";
for ( i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}
heapsort(arr, n);
cout<<"\n Array after heapsort : ";
for ( i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}

#include<bits/stdc++.h>
using namespace std ;
void heapify(int arr[], int n, int i)
{
int largest= i;
int l=2*i+1;
int r= 2*i+2 ;
if (l<n&& arr[l]>arr[largest])
{
largest= l;
}
if (r<n && arr[r]> arr[largest])
{
largest= r;
}
if (largest!=i)
{
swap(arr[largest],arr[i]);
heapify(arr,n,largest);
}
}
void heapsort(int arr[], int n )
{
int i;
for ( i = n/2-1; i >=0; i--)
{
heapify(arr,n,i);
}
for ( i = n-1 ; i> 0; i--)
{
swap(arr[0],arr[i]);
heapify(arr,i,0);
}
}
int main()
{
int arr[]={24,44,65,5,52,5,24}, n= sizeof(arr)/sizeof(int);
int i ;
cout<<"Array inserted : ";
for ( i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}
heapsort(arr, n);
cout<<"\n Array after heapsort : ";
for ( i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}
}
50 changes: 25 additions & 25 deletions InsertionSort.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
public class insertionsort {
public static void sort(int arr[])
{
for (int i = 1; i < arr.length; ++i) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j --;
}
arr[j + 1] = temp;
}
}
public static void main(String args[])
{
int arr[] = { 8,4,1,5,9,2 };
sort(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
}

}


public class insertionsort {
public static void sort(int arr[])
{
for (int i = 1; i < arr.length; ++i) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j --;
}
arr[j + 1] = temp;
}
}
public static void main(String args[])
{
int arr[] = { 8,4,1,5,9,2 };
sort(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}
70 changes: 23 additions & 47 deletions binarySearch.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,34 @@
//TO Search a number from a given shorted array using the binary search
package com.company;

import java.util.Scanner;
public class BinarySearch {

public class binarySearch {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();

int ans = bin(arr, num);
if (ans == -1) {
System.out.println("Number not matched");
} else
System.out.println(num + " is at the index " + ans);
sc.close();
int[] arr = {-18, -12, -4, 0, 2, 3, 4, 15, 16, 18, 22, 45, 89};
int target = 22;
int ans = binarySearch(arr, target);
System.out.println(ans);
}

static int bin(int[] arr, int target) {
int st = 0;

// return the index
// return -1 if it does not exist
static int binarySearch(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;
// for the dessending shorted array
if (arr[st] > arr[end]) {
while (st <= end) {
int mid = st + (end - st) / 2;
// if (arr[st] > arr[end]) {
if (target < arr[mid]) {
st = mid + 1;
} else if (target > arr[mid]) {
end = mid - 1;
} else {
// number found (target==arr[mid])
return mid;
}
}
}
// for the assending shorted array
if (arr[st] < arr[end]) {
while (st <= end) {
int mid = st + (end - st) / 2;
// if (arr[st] > arr[end]) {
if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
st = mid + 1;
} else {
// number found (target==arr[mid])
return mid;
}
}

while(start <= end) {
// find the middle element
// int mid = (start + end) / 2; // might be possible that (start + end) exceeds the range of int in java
int mid = start + (end - start) / 2;

if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
// ans found
return mid;
}
}
// number not found!!
return -1;

}
}