From 4f16b17aa786e51f4b0bcc652ab30c7df38a0909 Mon Sep 17 00:00:00 2001 From: Ishita Date: Wed, 26 May 2021 13:44:59 +0530 Subject: [PATCH 1/5] Added Insertion Sorting #17 --- InsertionSorting.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 InsertionSorting.cpp diff --git a/InsertionSorting.cpp b/InsertionSorting.cpp new file mode 100644 index 0000000..f327e1c --- /dev/null +++ b/InsertionSorting.cpp @@ -0,0 +1,47 @@ +// C++ program for insertion sort +#include +using namespace std; + +/* Function to sort an array using insertion sort*/ +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i - 1; + + /* Move elements of arr[0..i-1], that are + greater than key, to one position ahead + of their current position */ + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +// A utility function to print an array of size n +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + cout << arr[i] << " "; + cout << endl; +} + +/* Driver code */ +int main() +{ + int arr[] = { 12, 11, 13, 5, 6 }; + int n = sizeof(arr) / sizeof(arr[0]); + + insertionSort(arr, n); + printArray(arr, n); + + return 0; +} + + From 518b6e5351beb4b360ab0b9d2a85d11250844724 Mon Sep 17 00:00:00 2001 From: Ishita Date: Wed, 26 May 2021 22:41:11 +0530 Subject: [PATCH 2/5] Added Merge Sort #77 --- MergeSort.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 MergeSort.cpp diff --git a/MergeSort.cpp b/MergeSort.cpp new file mode 100644 index 0000000..2935b48 --- /dev/null +++ b/MergeSort.cpp @@ -0,0 +1,98 @@ +// C++ program for Merge Sort +#include +using namespace std; + +// Merges two subarrays of arr[]. +// First subarray is arr[l..m] +// Second subarray is arr[m+1..r] +void merge(int arr[], int l, int m, int r) +{ + int n1 = m - l + 1; + int n2 = r - m; + + // Create temp arrays + int L[n1], R[n2]; + + // Copy data to temp arrays L[] and R[] + for (int i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (int j = 0; j < n2; j++) + R[j] = arr[m + 1 + j]; + + // Merge the temp arrays back into arr[l..r] + + // Initial index of first subarray + int i = 0; + + // Initial index of second subarray + int j = 0; + + // Initial index of merged subarray + int k = l; + + 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 there are any + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + // Copy the remaining elements of + // R[], if there are any + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +} + +// l is for left index and r is +// right index of the sub-array +// of arr to be sorted */ +void mergeSort(int arr[],int l,int r){ + if(l>=r){ + return;//returns recursively + } + int m =l+ (r-l)/2; + mergeSort(arr,l,m); + mergeSort(arr,m+1,r); + merge(arr,l,m,r); +} + +// UTILITY FUNCTIONS +// Function to print an array +void printArray(int A[], int size) +{ + for (int i = 0; i < size; i++) + cout << A[i] << " "; +} + +// Driver code +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + int arr_size = sizeof(arr) / sizeof(arr[0]); + + cout << "Given array is \n"; + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + cout << "\nSorted array is \n"; + printArray(arr, arr_size); + return 0; +} + From 76a0d194c26685a1a8c319332d7f18e23ecd7e0f Mon Sep 17 00:00:00 2001 From: Ishita Date: Wed, 26 May 2021 22:50:04 +0530 Subject: [PATCH 3/5] Matrix Multiplication #84 --- MatrixMultiply.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 MatrixMultiply.cpp diff --git a/MatrixMultiply.cpp b/MatrixMultiply.cpp new file mode 100644 index 0000000..9debf00 --- /dev/null +++ b/MatrixMultiply.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +int main() +{ +int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; +cout<<"enter the number of row="; +cin>>r; +cout<<"enter the number of column="; +cin>>c; +cout<<"enter the first matrix element=\n"; +for(i=0;i>a[i][j]; +} +} +cout<<"enter the second matrix element=\n"; +for(i=0;i>b[i][j]; +} +} +cout<<"multiply of the matrix=\n"; +for(i=0;i Date: Wed, 26 May 2021 22:55:40 +0530 Subject: [PATCH 4/5] Number Triangle #88 --- NumberTriangle.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 NumberTriangle.cpp diff --git a/NumberTriangle.cpp b/NumberTriangle.cpp new file mode 100644 index 0000000..e838f51 --- /dev/null +++ b/NumberTriangle.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +int main() +{ +int i,j,k,l,n; +cout<<"Enter the Range="; +cin>>n; +for(i=1;i<=n;i++) +{ +for(j=1;j<=n-i;j++) +{ +cout<<" "; +} +for(k=1;k<=i;k++) +{ +cout<=1;l--) +{ +cout< Date: Wed, 26 May 2021 23:11:06 +0530 Subject: [PATCH 5/5] Subtracting Matrices #91 --- SubtractionMatrix.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 SubtractionMatrix.cpp diff --git a/SubtractionMatrix.cpp b/SubtractionMatrix.cpp new file mode 100644 index 0000000..7041cc9 --- /dev/null +++ b/SubtractionMatrix.cpp @@ -0,0 +1,43 @@ +// C++ program for subtraction of matrices +#include +using namespace std; +#define N 4 + +// This function subtracts B[][] from A[][], and stores +// the result in C[][] +void subtract(int A[][N], int B[][N], int C[][N]) +{ + int i, j; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + C[i][j] = A[i][j] - B[i][j]; +} + +// Driver code +int main() +{ + int A[N][N] = { {1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + int B[N][N] = { {1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + int C[N][N]; // To store result + int i, j; + subtract(A, B, C); + + cout << "Result matrix is " << endl; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + cout << C[i][j] << " "; + cout << endl; + } + + return 0; +} +