diff --git a/Bitwise operations.cpp b/Bitwise operations.cpp index 4f0b9ed..44a425e 100644 --- a/Bitwise operations.cpp +++ b/Bitwise operations.cpp @@ -24,6 +24,10 @@ int main() { // The result is 00000100 cout<<"b >> 1 "<<"= " << (b >> 1 )< char_stack; + string output; + + for (int i = 0; i < l; i++) { + + // If the scanned character is an + // operand, add it to output. + if (isalpha(infix[i]) || isdigit(infix[i])) + output += infix[i]; + + // If the scanned character is an + // ‘(‘, push it to the stack. + else if (infix[i] == '(') + char_stack.push('('); + + // If the scanned character is an + // ‘)’, pop and output from the stack + // until an ‘(‘ is encountered. + else if (infix[i] == ')') { + while (char_stack.top() != '(') { + output += char_stack.top(); + char_stack.pop(); + } + + // Remove '(' from the stack + char_stack.pop(); + } + + // Operator found + else + { + if (isOperator(char_stack.top())) + { + if(infix[i] == '^') + { + while (getPriority(infix[i]) <= getPriority(char_stack.top())) + { + output += char_stack.top(); + char_stack.pop(); + } + + } + else + { + while (getPriority(infix[i]) < getPriority(char_stack.top())) + { + output += char_stack.top(); + char_stack.pop(); + } + + } + + // Push current Operator on stack + char_stack.push(infix[i]); + } + } + } + return output; +} + +string infixToPrefix(string infix) +{ + /* Reverse String + * Replace ( with ) and vice versa + * Get Postfix + * Reverse Postfix * */ + int l = infix.size(); + + // Reverse infix + reverse(infix.begin(), infix.end()); + + // Replace ( with ) and vice versa + for (int i = 0; i < l; i++) { + + if (infix[i] == '(') { + infix[i] = ')'; + i++; + } + else if (infix[i] == ')') { + infix[i] = '('; + i++; + } + } + + string prefix = infixToPostfix(infix); + + // Reverse postfix + reverse(prefix.begin(), prefix.end()); + + return prefix; +} + +// Driver code +int main() +{ + string s = ("x+y*z/w+u"); + cout << infixToPrefix(s) << std::endl; + return 0; } \ No newline at end of file diff --git a/Infix-prefix.exe b/Infix-prefix.exe new file mode 100644 index 0000000..e48217d Binary files /dev/null and b/Infix-prefix.exe differ diff --git a/MergeSort.cpp b/MergeSort.cpp index b4960d8..5fc200e 100644 --- a/MergeSort.cpp +++ b/MergeSort.cpp @@ -1,57 +1,90 @@ -#include +#include using namespace std; - -void merge(int arr[],int mid,int left,int right){ - int n1 = mid - left + 1; - int n2 = right - mid; - int leftArr[n1],rightArr[n2]; - - for(int i=0;i=right) - return; - - int mid = left + (right-left)/2; - - mergeSort(arr,left,mid); - mergeSort(arr,mid+1,right); - merge(arr,mid,left,right); -} - -int main(){ - int n; - cin>>n; - int arr[n]; - for(int i=0;i>arr[i]; + // Copy the remaining elements of + // left[], if there are any + while (indexOfSubArrayOne < subArrayOne) { + array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + indexOfMergedArray++; } - mergeSort(arr,0,n-1); - for(int i=0;i= end) + return; // Returns recursively + + auto int mid = begin + (end - begin) / 2; + mergeSort(array, begin, mid); + mergeSort(array, mid + 1, end); + merge(array, begin, mid, end); +} + +// UTILITY FUNCTIONS +// Function to print an array +void printArray(int A[], int size) +{ + for (auto int i = 0; i < size; i++) + cout << A[i] << " "; +} + +// Driver code +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + auto 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; -} \ No newline at end of file +} diff --git a/MergeSort.exe b/MergeSort.exe new file mode 100644 index 0000000..fab8557 Binary files /dev/null and b/MergeSort.exe differ diff --git a/PalindromeNumber.cpp b/PalindromeNumber.cpp index e3527db..63a9544 100644 --- a/PalindromeNumber.cpp +++ b/PalindromeNumber.cpp @@ -1,28 +1,98 @@ +// A recursive C++ program to check +// whether a given number +// is palindrome or not #include using namespace std; - + +// A function that returns true only +// if num contains one +// digit +int oneDigit(int num) +{ + + // Comparison operation is faster + // than division + // operation. So using following + // instead of "return num + // / 10 == 0;" + return (num >= 0 && num < 10); +} + +// A recursive function to find +// out whether num is +// palindrome or not. Initially, dupNum +// contains address of +// a copy of num. +bool isPalUtil(int num, int* dupNum) +{ + + // Base case (needed for recursion + // termination): This + // statement mainly compares the + // first digit with the + // last digit + if (oneDigit(num)) + return (num == (*dupNum) % 10); + + // This is the key line in this + // method. Note that all + // recursive calls have a separate + // copy of num, but they + // all share same copy of *dupNum. + // We divide num while + // moving up the recursion tree + if (!isPalUtil(num / 10, dupNum)) + return false; + + // The following statements are + // executed when we move up + // the recursion call tree + *dupNum /= 10; + + // At this point, if num%10 contains + // i'th digit from + // beginning, then (*dupNum)%10 + // contains i'th digit + // from end + return (num % 10 == (*dupNum) % 10); +} + +// The main function that uses +// recursive function +// isPalUtil() to find out whether +// num is palindrome or not +int isPal(int num) +{ + + // Check if num is negative, + // make it positive + if (num < 0) + num = -num; + + // Create a separate copy of num, + // so that modifications + // made to address dupNum don't + // change the input number. + // *dupNum = num + int* dupNum = new int(num); + + return isPalUtil(num, dupNum); +} + +// Driver program to test +// above functions int main() { - int n, num, digit, reverse = 0; - - cout << "Enter a positive number: "; - cin >> num; - - n = num; - - do - { - digit = num % 10; - reverse = (reverse * 10) + digit; - num = num / 10; - } while (num != 0); - - cout << " The reverse of the number is: " << reverse << endl; - - if (n == reverse) - cout << " The number is a palindrome."; - else - cout << " The number is not a palindrome."; - - return 0; -} \ No newline at end of file + int n = 12321; + isPal(n) ? cout <<"Yes\n": cout <<"No" << endl; + + n = 12; + isPal(n) ? cout <<"Yes\n": cout <<"No" << endl; + + n = 88; + isPal(n) ? cout <<"Yes\n": cout <<"No" << endl; + + n = 8999; + isPal(n) ? cout <<"Yes\n": cout <<"No"; + return 0; +} diff --git a/PalindromeNumber.exe b/PalindromeNumber.exe new file mode 100644 index 0000000..6ffc12f Binary files /dev/null and b/PalindromeNumber.exe differ diff --git a/PalindromeString.cpp b/PalindromeString.cpp index b3c99ba..d7859e2 100644 --- a/PalindromeString.cpp +++ b/PalindromeString.cpp @@ -1,33 +1,29 @@ #include using namespace std; - + +// Function to check whether string +// is palindrome string isPalindrome(string S) { - // Stores the reverse of the - // string S - string P = S; - - // Reverse the string P - reverse(P.begin(), P.end()); - - // If S is equal to P - if (S == P) { - // Return "Yes" - return "Yes"; - } - // Otherwise - else { - // return "No" - return "No"; - } + // Iterate over the range [0, N/2] + for (int i = 0; i < S.length() / 2; i++) { + + // If S[i] is not equal to + // the S[N-i-1] + if (S[i] != S[S.length() - i - 1]) { + // Return No + return "No"; + } + } + // Return "Yes" + return "Yes"; } - + // Driver Code int main() { - string S ; - cin >> S; - cout << isPalindrome(S); - - return 0; + string S = "ABCDCBA"; + cout << isPalindrome(S); + + return 0; } \ No newline at end of file diff --git a/PalindromeString.exe b/PalindromeString.exe new file mode 100644 index 0000000..3d7e8f8 Binary files /dev/null and b/PalindromeString.exe differ diff --git a/Quicksort.cpp b/Quicksort.cpp index 2f11315..d9cb614 100644 --- a/Quicksort.cpp +++ b/Quicksort.cpp @@ -1,63 +1,72 @@ -#include -using namespace std; -// Swap two elements - Utility function -void swap(int *a, int *b) -{ - int t = *a; - *a = *b; - *b = t; -} - -// partition the array using last element as pivot -int partition(int arr[], int low, int high) -{ - int pivot = arr[high]; // pivot - int i = (low - 1); - - for (int j = low; j <= high - 1; j++) - { - //if current element is smaller than pivot, increment the low element - //swap elements at i and j - if (arr[j] <= pivot) - { - i++; // increment index of smaller element - swap(&arr[i], &arr[j]); - } - } - swap(&arr[i + 1], &arr[high]); - return (i + 1); -} - -//quicksort algorithm -void quickSort(int arr[], int low, int high) -{ - if (low < high) - { - //partition the array - int pivot = partition(arr, low, high); - - //sort the sub arrays independently - quickSort(arr, low, pivot - 1); - quickSort(arr, pivot + 1, high); - } -} - -void displayArray(int arr[], int size) -{ - int i; - for (i = 0; i < size; i++) - cout << arr[i] << "\t"; -} - -int main() -{ - int arr[] = {12, 23, 3, 43, 51, 35, 19, 45}; - int n = sizeof(arr) / sizeof(arr[0]); - cout << "Input array" << endl; - displayArray(arr, n); - cout << endl; - quickSort(arr, 0, n - 1); - cout << "Array sorted with quick sort" << endl; - displayArray(arr, n); - return 0; -} \ No newline at end of file +#include +using namespace std; + +// A utility function to swap two elements +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +/* This function takes last element as pivot, places +the pivot element at its correct position in sorted +array, and places all smaller (smaller than pivot) +to left of pivot and all greater elements to right +of pivot */ +int partition (int arr[], int low, int high) +{ + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far + + for (int j = low; j <= high - 1; j++) + { + // If current element is smaller than the pivot + if (arr[j] < pivot) + { + i++; // increment index of smaller element + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} + +/* The main function that implements QuickSort +arr[] --> Array to be sorted, +low --> Starting index, +high --> Ending index */ +void quickSort(int arr[], int low, int high) +{ + if (low < high) + { + /* pi is partitioning index, arr[p] is now + at right place */ + int pi = partition(arr, low, high); + + // Separately sort elements before + // partition and after partition + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +// Driver Code +int main() +{ + int arr[] = {10, 7, 8, 9, 1, 5}; + int n = sizeof(arr) / sizeof(arr[0]); + quickSort(arr, 0, n - 1); + cout << "Sorted array: \n"; + printArray(arr, n); + return 0; +} diff --git a/Quicksort.exe b/Quicksort.exe new file mode 100644 index 0000000..2857fc5 Binary files /dev/null and b/Quicksort.exe differ diff --git a/RemoveDuplicates.cpp b/RemoveDuplicates.cpp index 42d2dd0..a04d823 100644 --- a/RemoveDuplicates.cpp +++ b/RemoveDuplicates.cpp @@ -1,31 +1,51 @@ -// C++ program to remove duplicates in-place -#include +// C++ program to remove the +// duplicate elements from the array +// using STL in C++ + +#include using namespace std; -// Function to remove duplicate elements This function returns new size of modified array. -int removeDuplicates(int arr[], int n) + +// Function to remove duplicate elements +void removeDuplicates(int arr[], int n) { - if (n == 0 || n == 1) - return n; - // To store index of next unique element - int j = 0; - // Doing same as done in Method 1 - // Just maintaining another updated index i.e. j - for (int i = 0; i < n - 1; i++) - if (arr[i] != arr[i + 1]) - arr[j++] = arr[i]; - - arr[j++] = arr[n - 1]; - return j; + + int i; + + // Initialise a set + // to store the array values + set s; + + // Insert the array elements + // into the set + for (i = 0; i < n; i++) { + + // insert into set + s.insert(arr[i]); + } + + set::iterator it; + + // Print the array with duplicates removed + cout << "\nAfter removing duplicates:\n"; + for (it = s.begin(); it != s.end(); ++it) + cout << *it << ", "; + cout << '\n'; } -// Main Function code + +// Driver code int main() { - int arr[] = {1, 3, 3, 4, 4, 5,6 , 6, 6}; + int arr[] = { 4, 2, 3, 3, 2, 4 }; + int n = sizeof(arr) / sizeof(arr[0]); - // removeDuplicates() returns new size of array. - n = removeDuplicates(arr, n); - // Print updated array + + // Print array + cout << "\nBefore removing duplicates:\n"; for (int i = 0; i < n; i++) cout << arr[i] << " "; + + // call removeDuplicates() + removeDuplicates(arr, n); + return 0; } diff --git a/RemoveDuplicates.exe b/RemoveDuplicates.exe new file mode 100644 index 0000000..ca90135 Binary files /dev/null and b/RemoveDuplicates.exe differ diff --git a/binarySearch.cpp b/binarySearch.cpp index b44db31..fb3025e 100644 --- a/binarySearch.cpp +++ b/binarySearch.cpp @@ -1,48 +1,74 @@ -#include +#include using namespace std; - -int binarySearch(int *arr, int n, int k) { - int start = 0, end = n - 1; - - while(start <= end) { - int mid = (start + end) / 2; - - if(arr[mid] == k) { - return mid; - } - else if(arr[mid] < k) { - start = mid + 1; + +//define array globally +const int N = 1e6 +4; + +int a[N]; +int n;//array size + +//elememt to be searched in array + int k; + +bool check(int dig) +{ + //element at dig position in array + int ele=a[dig]; + + //if k is less than + //element at dig position + //then we need to bring our higher ending to dig + //and then continue further + if(k<=ele) + { + return 1; + } + else + { + return 0; + } +} +void binsrch(int lo,int hi) +{ + while(lo> n; - - int *arr = new int[n]; - - for(int i = 0; i < n; i++) { - cin >> arr[i]; - } - - int k; - cin >> k; - - int i = binarySearch(arr, n, k); - - if(i != -1) { - cout << "Element found at index " << i << endl; - } - - else { - cout << "Element not found" << endl; - } - - delete [] arr; + + +int main() +{ + cin>>n; + for(int i=0; i>a[i]; + } + cin>>k; + + //it is being given array is sorted + //if not then we have to sort it + + //minimum possible point where our k can be is starting index + //so lo=0 + //also k cannot be outside of array so end point + //hi=n + + binsrch(0,n); + + return 0; } \ No newline at end of file diff --git a/binarySearch.exe b/binarySearch.exe new file mode 100644 index 0000000..c8c4ef1 Binary files /dev/null and b/binarySearch.exe differ diff --git a/bubble-sort.cpp b/bubble-sort.cpp index dac0ec0..d59801f 100644 --- a/bubble-sort.cpp +++ b/bubble-sort.cpp @@ -1,25 +1,33 @@ -//Ishita Karandikar -// C++ program for implementation of Bubble sort #include using namespace std; - -void swap(int *x, int *y) +void swap(int *xp, int *yp) { - int temp = *x; - *x = *y; - *y = temp; + int temp = *xp; + *xp = *yp; + *yp = temp; } -// A function to implement bubble sort +// An optimized version of Bubble Sort void bubbleSort(int arr[], int n) { - int i, j; - for (i = 0; i < n-1; i++) - - // Last i elements are already in place - for (j = 0; j < n-i-1; j++) + int i, j; + bool swapped; + for (i = 0; i < n-1; i++) + { + swapped = false; + for (j = 0; j < n-i-1; j++) + { if (arr[j] > arr[j+1]) - swap(&arr[j], &arr[j+1]); + { + swap(&arr[j], &arr[j+1]); + swapped = true; + } + } + + // IF no two elements were swapped by inner loop, then break + if (swapped == false) + break; + } } /* Function to print an array */ @@ -27,17 +35,17 @@ void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) - cout << arr[i] << " "; - cout << endl; + cout <<" "<< arr[i]; + cout <<" n"; } -// Driver code +// Driver program to test above functions int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); - cout<<"Sorted array: \n"; + cout <<"Sorted array: \n"; printArray(arr, n); return 0; -} +} \ No newline at end of file diff --git a/bubble-sort.exe b/bubble-sort.exe new file mode 100644 index 0000000..d6ccf65 Binary files /dev/null and b/bubble-sort.exe differ diff --git a/bucketsort.cpp b/bucketsort.cpp index 7a07b26..2a10735 100644 --- a/bucketsort.cpp +++ b/bucketsort.cpp @@ -1,38 +1,44 @@ -#include #include -#include //used for the sake of simplicity +#include +#include using namespace std; +// Function to sort arr[] of +// size n using bucket sort void bucketSort(float arr[], int n) { + + // 1) Create n empty buckets vector b[n]; - - for (int i=0; i>n; - float arr[n]; - - for (int i=0; i> arr[i]; -} + float arr[] + = { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 }; + int n = sizeof(arr) / sizeof(arr[0]); bucketSort(arr, n); - - cout << "\nAfter Sorting \n"; - for (int i=0; i 1: + + # Finding the mid of the array + mid = len(arr)//2 + + # Dividing the array elements + L = arr[:mid] + + # into 2 halves + R = arr[mid:] + + # Sorting the first half + mergeSort(L) + + # Sorting the second half + mergeSort(R) + + i = j = k = 0 + + # Copy data to temp arrays L[] and R[] + while i < len(L) and j < len(R): + if L[i] < R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 k += 1 + + # Checking if any element was left + while i < len(L): + arr[k] = L[i] i += 1 - else: - # Inversion will occur. - temp_arr[k] = arr[j] - inv_count += (mid - i + 1) k += 1 + + while j < len(R): + arr[k] = R[j] j += 1 - - # Copy the remaining elements of left subarray into temporary array - while i <= mid: - temp_arr[k] = arr[i] - k += 1 - i += 1 - - # Copy the remaining elements of right subarray into temporary array - while j <= right: - temp_arr[k] = arr[j] - k += 1 - j += 1 - - # Copy the sorted subarray into Original array - for loop_var in range(left, right + 1): - arr[loop_var] = temp_arr[loop_var] - - return inv_count - - + k += 1 + +# Code to print the list + + +def printList(arr): + for i in range(len(arr)): + print(arr[i], end=" ") + print() + + # Driver Code -# Given array is -arr = [1, 20, 6, 4, 5] -n = len(arr) -result = mergeSort(arr, n) -print("Number of inversions are", result) +if __name__ == '__main__': + arr = [12, 11, 13, 5, 6, 7] + print("Given array is", end="\n") + printList(arr) + mergeSort(arr) + print("Sorted array is: ", end="\n") + printList(arr) \ No newline at end of file diff --git a/selection_sort.py b/selection_sort.py index 7fbf672..66b0a7f 100644 --- a/selection_sort.py +++ b/selection_sort.py @@ -1,23 +1,21 @@ -''' -Sorting: Selection sort using recursion -''' - - -def selection(l, n): - if n == len(l): - return l - else: - s = max(l) - f = len(l)-1 - for i in range(n, len(l)): - if l[i] < s: - s = l[i] - f = i - l[n], l[f] = l[f], l[n] - return selection(l, n+1) - - -f = int(input()) -l = [int(input()) for i in range(f)] -n = 0 -print(selection(l, n)) +import sys +A = [64, 25, 12, 22, 11] + +# Traverse through all array elements +for i in range(len(A)): + + # Find the minimum element in remaining + # unsorted array + min_idx = i + for j in range(i+1, len(A)): + if A[min_idx] > A[j]: + min_idx = j + + # Swap the found minimum element with + # the first element + A[i], A[min_idx] = A[min_idx], A[i] + +# Driver code to test above +print ("Sorted array") +for i in range(len(A)): + print("%d" %A[i]) \ No newline at end of file diff --git a/style.css b/style.css index d73dfce..1d255f9 100644 --- a/style.css +++ b/style.css @@ -1,7 +1,11 @@ * { padding: 0; margin: 0; - background-color: powderblue; + /* background-color: powderblue;*/ + +} +body{ + background-image: linear-gradient(180deg, powderblue, white); } .head, @@ -10,6 +14,7 @@ display: flex; justify-content: center; align-items: center; + font-weight: 900; } .link { @@ -20,13 +25,14 @@ } .head h1 { - margin: 50px; + margin: 20px; } h1, h2, h3 { font-family: "Poppins", sans-serif; + } .main { @@ -35,3 +41,27 @@ h3 { .one { margin-bottom: 20px; } + +.one p{ + text-align: center; +} + +.bord +{ + border-left: 5px solid #bdc3c7; + padding-left:10px; +} + +.btn-lg, .btn-group-lg > .btn { + padding: 10px 16px; + font-size: 18px; + line-height: 1.3333333; + border-radius: 6px; +} + +.btn-info { + color: #fff; + background-color: #5bc0de; + border-color: #46b8da; +} + diff --git a/transpose_of_sparse_matrix.cpp b/transpose_of_sparse_matrix.cpp index a839494..f798529 100644 --- a/transpose_of_sparse_matrix.cpp +++ b/transpose_of_sparse_matrix.cpp @@ -1,59 +1,93 @@ -// Transpose of a sparse matrix - #include -#include using namespace std; +class transposeM{ + int m1[20][20],m2[20][20],i,j,row,column,t; +public: + void read(){ + t=0; + cout<<"Enter the number of row: \n"; + cin>>row; + cout<<"enter the number of column: \n"; + cin>>column; -int main() { - int row,column,size; - - // input for number of rows and columns and number of non zero elements - cout<<"Enter the number of rows in the matrix: "; - cin>>row; - cout<<"Enter the number of columns in the matrix: "; - cin>>column; - cout<<"Enter the number of non zero elements in the matrix: "; - cin>>size; - - // inputing the matrix in sparse form (triplet form) - if(size <= (row*column)){ - int sparseMatrix[size][3]; - for(int i=0 ; i>sparseMatrix[i][0]; - cin>>sparseMatrix[i][1]; - cin>>sparseMatrix[i][2]; - } + for(i=0;i>m1[i][j]; + + if(m1[i][j]){ + t++; +// cout<<"first t is:"<
-

Welcome to Hacktober-fest 2021

+

Welcome to Hacktober-fest 2021

@@ -46,10 +46,10 @@

Event Details


-

Hacktober Rules:

+

Hacktober Rules:

-

+

To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone). Pull requests can be made in any participating GitHub or GitLab @@ -70,7 +70,10 @@

Hacktober Rules:

+ + + +