Skip to content
This repository has been archived by the owner on Oct 8, 2022. It is now read-only.

created sorting folder and added sorting algorithms #253

Open
wants to merge 14 commits 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
51 changes: 51 additions & 0 deletions C++/DivisibleBy3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Given a very large number num (1 <= num <= 10^1000), print the number of digits that needs to be removed to make the number exactly divisible by 3. If it is not possible then print -1.

// Input: num = "1234"
// Output: 1
// Explanation: we need to remove one
// digit that is 1 or 4, to make the
// number divisible by 3.on

// Input: num = "11"
// Output: -1
// Explanation: It is not possible to
// remove any digits and make it divisible
// by 3.

#include <iostream>
using namespace std;

int Sum(string n) {
int len = n.length(), sum = 0;
for (int i = 0; i < len; i++) {
sum += (int)n[i];
}
return sum;
}
int Count(string num) {
int n = num.length();
int sum = Sum(num);
if (sum % 3 == 0) {
return 0;
}
if (n == 1) {
return -1;
}
for (int i = 0; i < n; i++) {
int currDigit = num[i] - '0';
if (sum % 3 == currDigit % 3) {
return 1;
}
}
if (n == 2) {
return -1;
}
return 2;
}
int main() {
string num;
cout << "Enter the number: ";
cin >> num;
cout << Count(num) << endl;
return 0;
}
35 changes: 35 additions & 0 deletions Java-DSA/Sorting/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class BubbleSort{
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
}

}
public static void main(String[] args) {
int arr[] ={1, 10, 50, 5, 2, 29, 70, 69, 9, 6};

System.out.println("Array Before Bubble Sort ");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();

bubbleSort(arr);//sorting array elements using bubble sort

System.out.println("Array After Bubble Sort ");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}

}
}
38 changes: 38 additions & 0 deletions Java-DSA/Sorting/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class InsertionSort
{
void insert(int a[]) /* function to sort an array with insertion sort */
{
int i, j, temp;
int n = a.length;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;

while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one position ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void printArr(int a[]) /* function to print the array */
{
int i;
int n = a.length;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}

public static void main(String[] args) {
int a[] = { 92, 50, 5, 20, 11, 22 };
InsertionSort i1 = new InsertionSort();
System.out.println("\nBefore sorting array elements are - ");
i1.printArr(a);
System.out.println();
i1.insert(a);
System.out.println("\n\nAfter sorting array elements are - ");
i1.printArr(a);
System.out.println();
}
}
74 changes: 74 additions & 0 deletions Java-DSA/Sorting/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class MergeSort {

/* Function to merge the subarrays of a[] */
void merge(int a[], int beg, int mid, int end) {
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;

/* temporary Arrays */
int LeftArray[] = new int[n1];
int RightArray[] = new int[n2];

/* copy data to temp arrays */
for (i = 0; i < n1; i++)
LeftArray[i] = a[beg + i];
for (j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];

i = 0; /* initial index of first sub-array */
j = 0; /* initial index of second sub-array */
k = beg; /* initial index of merged sub-array */

while (i < n1 && j < n2) {
if (LeftArray[i] <= RightArray[j]) {
a[k] = LeftArray[i];
i++;
} else {
a[k] = RightArray[j];
j++;
}
k++;
}
while (i < n1) {
a[k] = LeftArray[i];
i++;
k++;
}

while (j < n2) {
a[k] = RightArray[j];
j++;
k++;
}
}

void mergeSort(int a[], int beg, int end) {
if (beg < end) {
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}

/* Function to print the array */
void printArray(int a[], int n) {
int i;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}

public static void main(String args[]) {
int a[] = { 11, 30, 24, 7, 31, 16, 39, 41 };
int n = a.length;
MergeSort m1 = new MergeSort();
System.out.println("\nBefore sorting array elements are - ");
m1.printArray(a, n);
m1.mergeSort(a, 0, n - 1);
System.out.println("\nAfter sorting array elements are - ");
m1.printArray(a, n);
System.out.println("");
}

}
53 changes: 53 additions & 0 deletions Java-DSA/Sorting/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.*;
class QuickSort {
//selects last element as pivot, p using which array is partitioned.
int partition(int intArray[], int low, int high) {
int p = intArray[high];
int i = (low-1); // smaller element index
for (int j=low; j<high; j++) {
// check if current element is less than or equal to p
if (intArray[j] <= p) {
i++;
// swap intArray[i] and intArray[j]
int temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
}
}

// swap intArray[i+1] and intArray[high] (or p)
int temp = intArray[i+1];
intArray[i+1] = intArray[high];
intArray[high] = temp;

return i+1;
}


//routine to sort the array partitions recursively
void quick_sort(int intArray[], int low, int high) {
if (low < high) {
//partition the array around pi=>partitioning index and return pi
int p = partition(intArray, low, high);

// sort each partition recursively
quick_sort(intArray, low, p-1);
quick_sort(intArray, p+1, high);
}
}
}

class Main{
public static void main(String args[]) {
//initialize a numeric array, intArray
int intArray[] = {4,-1,6,8,0,5,-3};
int n = intArray.length;
//print the original array
System.out.println("Original Array: " + Arrays.toString(intArray));
//call quick_sort routine using QuickSort object
QuickSort obj = new QuickSort();
obj.quick_sort(intArray, 0, n-1);
//print the sorted array
System.out.println("\nSorted Array: " + Arrays.toString(intArray));
}
}
40 changes: 40 additions & 0 deletions Java-DSA/Sorting/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.Scanner;

public class SelectionSort
{
public static void main(String args[])
{
int size, i, j, temp;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Array Size : ");
size = scan.nextInt();

System.out.print("Enter Array Elements : ");
for(i=0; i<size; i++)
{
arr[i] = scan.nextInt();
}

System.out.print("Sorting Array using Selection Sort Technique..\n");
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

System.out.print("Now the Array after Sorting is :\n");
for(i=0; i<size; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Binary file modified Weather/Background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 48 additions & 31 deletions Weather/app.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
let weather = {
"apiKey": "2edb891504a4aba85d525f7a119893c4",
fetchWeather: function (city) {
fetch("https://api.openweathermap.org/data/2.5/weather?q="
+ city
+ "&units=metric&appid="
+ this.apiKey
)
.then((response) => response.json())
.then((data) => this.displayWeather(data))
},
displayWeather: function(data) {
const { name } = data;
const { lon, lat} = data.coord;
const { icon, description } = data.weather[0];
const { temp, feels_like, temp_min, temp_max, pressure, humidity} = data.main;
const { speed } = data.wind;
console.log(name, icon, description, temp, humidity, speed);
document.querySelector(".city").innerHTML = "Weather in " + name;
document.querySelector(".long").innerHTML = "( " + lon + " , " + lat + " )";
document.querySelector(".icon").src = "http://openweathermap.org/img/wn/" + icon +"@2x.png";
document.querySelector(".description").innerText = description;
document.querySelector(".temp").innerText = temp + "°C";
document.querySelector(".feelslike").innerText = "Feels like: " + feels_like + "°C || Max: " + temp_max + "°C || Min: " + temp_min + "°C";
document.querySelector(".pressure").innerText = "Pressure: " + pressure + "pa || Humidity: " + humidity + "% || Wind: " + speed + "km/h";
},
search: function() {
this.fetchWeather(document.querySelector(".search-bar").value);
}
apiKey: "2edb891504a4aba85d525f7a119893c4",
fetchWeather: function (city) {
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&units=metric&appid=" +
this.apiKey
)
.then((response) => response.json())
.then((data) => this.displayWeather(data));
},
displayWeather: function (data) {
const { name } = data;
const { lon, lat } = data.coord;
const { icon, description } = data.weather[0];
const { temp, feels_like, temp_min, temp_max, pressure, humidity } =
data.main;
const { speed } = data.wind;
console.log(name, icon, description, temp, humidity, speed);
document.querySelector(".city").innerHTML = "Weather in " + name;
document.querySelector(".long").innerHTML = "( " + lon + " , " + lat + " )";
document.querySelector(".icon").src =
"http://openweathermap.org/img/wn/" + icon + "@2x.png";
document.querySelector(".description").innerText = description;
document.querySelector(".temp").innerText = temp + "°C";
document.querySelector(".feelslike").innerText =
"Feels like: " +
feels_like +
"°C || Max: " +
temp_max +
"°C || Min: " +
temp_min +
"°C";
document.querySelector(".pressure").innerText =
"Pressure: " +
pressure +
"pa || Humidity: " +
humidity +
"% || Wind: " +
speed +
"km/h";
},
search: function () {
this.fetchWeather(document.querySelector(".search-bar").value);
},
};

document.querySelector(".search button").addEventListener("click", function() {
weather.search();
})
document.querySelector(".search button").addEventListener("click", function () {
weather.search();
});
Binary file added Weather/cloudy.ico
Binary file not shown.
Loading