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

[Added the Program ] -- Added the Program for the Merge Sort #110

Open
wants to merge 3 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
85 changes: 85 additions & 0 deletions C++/Merge_sort_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <iostream>
using namespace std;

// Function to merge two subarrays of arr[]
void merge(int arr[], int left, int mid, int right) {
// Find sizes of two subarrays to be merged
int n1 = mid - left + 1;
int n2 = right - mid;

// Create temporary arrays
int L[n1], R[n2];

// Copy data to temp arrays L[] and R[]
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

// Merge the temporary arrays back into arr[left..right]
int i = 0; // Initial index of first subarray
int j = 0; // Initial index of second subarray
int k = left; // Initial index of merged subarray

while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

// Copy the remaining elements of L[], if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy the remaining elements of R[], if any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

// Function to implement Merge Sort
void mergeSort(int arr[], int left, int right) {
if (left < right) {
// Find the middle point
int mid = left + (right - left) / 2;

// Recursively sort the two halves
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);

// Merge the sorted halves
merge(arr, left, mid, right);
}
}

// Utility function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int arr_size = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: \n";
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

cout << "Sorted array: \n";
printArray(arr, arr_size);

return 0;
}
29 changes: 29 additions & 0 deletions C++/Numbers_Of_Ways_to_Kth_stare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#This is a hard Leetcode Problem which uses a concept of memoization in recursion . Here is the question :
# https://leetcode.com/problems/find-number-of-ways-to-reach-the-k-th-stair/description

# this is the solution to the above leetcode problem


def numberOfWays(k: int) -> int:
# dp[i] will store the number of ways to reach stair i
dp = [0] * (k + 1)

# Base case: 1 way to reach the first stair
dp[1] = 1

# Iterate over each stair up to k
for i in range(1, k):
jump = 0
stair = i
# Keep jumping upwards as long as within bounds
while stair <= k:
if stair <= k:
dp[stair] += dp[i]
jump += 1
stair = i + 2**jump

return dp[k]

# Test the function
k = 10 # example value
print(numberOfWays(k))