diff --git a/Graph Theory/README.md b/Graph Theory/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Graph Theory/test b/Graph Theory/test deleted file mode 100644 index 9daeafb..0000000 --- a/Graph Theory/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Miscellaneous/Largest Prime Factor/test b/Miscellaneous/Largest Prime Factor/test deleted file mode 100644 index 9daeafb..0000000 --- a/Miscellaneous/Largest Prime Factor/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Miscellaneous/Largest Product of a series/test b/Miscellaneous/Largest Product of a series/test deleted file mode 100644 index 9daeafb..0000000 --- a/Miscellaneous/Largest Product of a series/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Miscellaneous/Luhn's Algorithm/test b/Miscellaneous/Luhn's Algorithm/test deleted file mode 100644 index 8b13789..0000000 --- a/Miscellaneous/Luhn's Algorithm/test +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Miscellaneous/Palindrome Product/test b/Miscellaneous/Palindrome Product/test deleted file mode 100644 index 9daeafb..0000000 --- a/Miscellaneous/Palindrome Product/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Miscellaneous/test b/Miscellaneous/test deleted file mode 100644 index 9daeafb..0000000 --- a/Miscellaneous/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/README.md b/README.md index ee65fde..d7a1c21 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![Algorithms](./images/algorithms.jpg) -An attempt to aggregate all the various algorithms used in CS. +An attempt to aggregate all the various algorithms in CS. ## What you will gain by contributing here? diff --git a/Randomized Algorithms/Armstrong's Permutation/Permute_By_Cylic.cpp b/Randomized Algorithms/Armstrong's Permutation/Permute_By_Cylic.cpp deleted file mode 100644 index 06e49b8..0000000 --- a/Randomized Algorithms/Armstrong's Permutation/Permute_By_Cylic.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include -#include -#include - -using namespace std; -using namespace std::chrono; - -void get_input(int *arr, int n) { - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void permute_by_cyclic(int *arr, int n) { - srand(time(0)); - int *b = new int[n]; - int *c; - int offset = rand() % n; - - for (int i = 0; i < n; i++) { - int dest = i + offset; - if (dest > n) { - dest -= n; - } - b[dest] = arr[i]; - } - - c = arr; - c = NULL; - arr = b; -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } -} -int main() -{ - int n, *arr; - - cout << "Number of elements: "; - cin >> n; - - arr = new int[n]; - - get_input(arr, n); - - permute_by_cyclic(arr, n); - - cout << "The randomized elements are: " << endl; - display(arr, n); -} diff --git a/Randomized Algorithms/Armstrong's Permutation/test b/Randomized Algorithms/Armstrong's Permutation/test deleted file mode 100644 index 9daeafb..0000000 --- a/Randomized Algorithms/Armstrong's Permutation/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Randomized Algorithms/Permute In Place/Permute_In_Place.cpp b/Randomized Algorithms/Permute In Place/Permute_In_Place.cpp deleted file mode 100644 index b199069..0000000 --- a/Randomized Algorithms/Permute In Place/Permute_In_Place.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -#include -#include -#include - -using namespace std; -using namespace std::chrono; - -void swap(int *xp, int *yp) { - int temp = *xp; - *xp = *yp; - *yp = temp; -} - -void permute(int *arr, int n) { - srand(time(0)); - int j = 0; - for (int i = 0; i < n; i++) { - j = rand() % n; - swap(&arr[i], &arr[j]); - } -} - -void get_input(int *arr, int n) { - cout << "Elements: " << endl; - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } -} - -int main() -{ - int n, *arr; - cout << "Number of elements: "; - cin >> n; - - arr = new int[n]; - - get_input(arr, n); - - auto start = high_resolution_clock::now(); - permute(arr, n); - auto stop = high_resolution_clock::now(); - auto duration = duration_cast(stop - start); - cout << "The running time of algorithm is: " << duration.count() << "ns" << endl; - - cout << "The randomized list of elements is: "; - display(arr, n); - - cout << "\nPress any key to exit "; - cin.get(); - cin.ignore(); -} diff --git a/Randomized Algorithms/Permute In Place/Simple_Permute.exe b/Randomized Algorithms/Permute In Place/Simple_Permute.exe deleted file mode 100644 index 97e9333..0000000 Binary files a/Randomized Algorithms/Permute In Place/Simple_Permute.exe and /dev/null differ diff --git a/Randomized Algorithms/Permute In Place/test b/Randomized Algorithms/Permute In Place/test deleted file mode 100644 index 8b13789..0000000 --- a/Randomized Algorithms/Permute In Place/test +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.cpp b/Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.cpp deleted file mode 100644 index 9f64ad7..0000000 --- a/Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include -#include -#include - -using namespace std; -using namespace std::chrono; - -void swap(int *xp, int *yp) { - int temp = *xp; - *xp = *yp; - *yp = temp; -} - -void get_input(int *arr, int n) { - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void permute_without_identity(int *arr, int n) { - - srand(time(0)); - - for (int i = 0; i < n; i++) { - swap(&arr[i], &arr[(rand() % (n - i)) + i]); - } -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } -} - -int main() -{ - int n, *arr; - cout << "Number of elements: "; - cin >> n; - - arr = new int[n]; - - cout << "Elements: " << endl; - get_input(arr, n); - - auto start = high_resolution_clock::now(); - permute_without_identity(arr, n); - auto stop = high_resolution_clock::now(); - auto duration = duration_cast(stop - start); - cout << "Running time of algorithm is: " << duration.count() << "ns " << endl; - - cout << "The randomized array is: " << endl; - display(arr, n); -} diff --git a/Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.exe b/Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.exe deleted file mode 100644 index 560bd61..0000000 Binary files a/Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.exe and /dev/null differ diff --git a/Randomized Algorithms/Permute Without Identity/test b/Randomized Algorithms/Permute Without Identity/test deleted file mode 100644 index 9daeafb..0000000 --- a/Randomized Algorithms/Permute Without Identity/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Randomized Algorithms/test b/Randomized Algorithms/test deleted file mode 100644 index 9daeafb..0000000 --- a/Randomized Algorithms/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Searching/Binary Search/Binary_Search.cpp b/Searching/Binary Search/Binary_Search.cpp deleted file mode 100644 index 358a5d2..0000000 --- a/Searching/Binary Search/Binary_Search.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include -#include "arrayio.h" -#include - - -using namespace std; -using namespace std::chrono; - -void swap(int *xp, int *yp) { - int temp = *xp; - *xp = *yp; - *yp = temp; -} - -int partition(int *arr, int p, int r) { - int x = arr[r]; - int i = p - 1; - int j = p; - for (int j = p; j <= r - 1; j++) { - if (arr[j] <= x) { - i += 1; - swap(&arr[i], &arr[j]); - } - } - swap(&arr[i + 1], &arr[r]); - return i + 1; -} - -void quicksort(int *arr, int p, int r) { - if (p < r) { - int q = partition(arr, p, r); - quicksort(arr, p, q - 1); - quicksort(arr, q + 1, r); - } -} - -int binary_search(int *arr, int low, int high, int key) { - - if (high >= low) { - int mid = low + (high - low) / 2; - if (key == arr[mid]) { - return mid; - } - else if (arr[mid] > key) { - return binary_search(arr, low, mid - 1, key); - } - else{ - return binary_search(arr, mid + 1, high, key); - } - } - return -1; -} - - -int main() -{ - int n, *arr, key; - cout << "Number of elements: "; - cin >> n; - - arr = new int[n]; - - cout << "Enter elements: "; - get_input(arr, n); - - cout << "Enter element to be found: "; - cin >> key; - - quicksort(arr, 0, n-1); - auto start = high_resolution_clock::now(); - int index = binary_search(arr, 0, n-1, key); - auto stop = high_resolution_clock::now(); - auto duration = duration_cast(stop - start); - - if (index == -1) { - cout << "Element not found." << endl; - } - else { - cout << "Element found at position: " << index << endl; - } - cout << "Running time of binary search is: " << duration.count() << " microseconds." << endl; - - cout << "\nPress anny key to exit "; - cin.get(); - cin.ignore(); -} - -// Run program: Ctrl + F5 or Debug > Start Without Debugging menu -// Debug program: F5 or Debug > Start Debugging menu - -// Tips for Getting Started: -// 1. Use the Solution Explorer window to add/manage files -// 2. Use the Team Explorer window to connect to source control -// 3. Use the Output window to see build output and other messages -// 4. Use the Error List window to view errors -// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project -// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/Searching/Binary Search/Binary_Search.exe b/Searching/Binary Search/Binary_Search.exe deleted file mode 100644 index ab2fa4a..0000000 Binary files a/Searching/Binary Search/Binary_Search.exe and /dev/null differ diff --git a/Searching/Binary Search/arrayio.cpp b/Searching/Binary Search/arrayio.cpp deleted file mode 100644 index 61f7c4b..0000000 --- a/Searching/Binary Search/arrayio.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "arrayio.h" -#include -using namespace std; - -void get_input(int *arr, int n) { - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } - cout << endl; -} \ No newline at end of file diff --git a/Searching/Binary Search/arrayio.h b/Searching/Binary Search/arrayio.h deleted file mode 100644 index 285c744..0000000 --- a/Searching/Binary Search/arrayio.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#ifndef _AARAYIO_H -#define _ARRAYIO_H - -void get_input(int *arr, int n); - -void display(int *arr, int n); - -#endif diff --git a/Searching/Binary Search/binarySearch.c b/Searching/Binary Search/binarySearch.c new file mode 100644 index 0000000..64f4fcb --- /dev/null +++ b/Searching/Binary Search/binarySearch.c @@ -0,0 +1,34 @@ +#include + +int binarySearch(int arr[], int left, int right, int x) +{ + while (left <= right) { + int mid = left + (right - l) / 2; + + if (arr[mid] > x) + right = mid - 1; + + if (arr[mid] < x) + left = mid + 1; + + else + return mid; + + } + + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int n = sizeof(arr) / sizeof(arr[0]); + int x = 10; + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? printf("Element is not present" + " in array") + : printf("Element is present at " + "index %d", + result); + return 0; +} \ No newline at end of file diff --git a/Searching/Binary Search/test b/Searching/Binary Search/test deleted file mode 100644 index 8b13789..0000000 --- a/Searching/Binary Search/test +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Searching/Fibonacci Search/README.md b/Searching/Fibonacci Search/README.md index e69de29..8b13789 100644 --- a/Searching/Fibonacci Search/README.md +++ b/Searching/Fibonacci Search/README.md @@ -0,0 +1 @@ + diff --git a/Searching/Fibonacci Search/fibonacciSearch.c b/Searching/Fibonacci Search/fibonacciSearch.c new file mode 100644 index 0000000..7115f30 --- /dev/null +++ b/Searching/Fibonacci Search/fibonacciSearch.c @@ -0,0 +1,79 @@ +// C program for Fibonacci Search +#include + +// Utility function to find minimum of two elements +int min(int x, int y) { return (x<=y)? x : y; } + +/* Returns index of x if present, else returns -1 */ +int fibonacciSearch(int arr[], int x, int n) +{ + /* Initialize fibonacci numbers */ + int fibMMm2 = 0; // (m-2)'th Fibonacci No. + int fibMMm1 = 1; // (m-1)'th Fibonacci No. + int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci + + /* fibM is going to store the smallest Fibonacci + Number greater than or equal to n */ + while (fibM < n) + { + fibMMm2 = fibMMm1; + fibMMm1 = fibM; + fibM = fibMMm2 + fibMMm1; + } + + // Marks the eliminated range from front + int offset = -1; + + /* while there are elements to be inspected. Note that + we compare arr[fibMm2] with x. When fibM becomes 1, + fibMm2 becomes 0 */ + while (fibM > 1) + { + // Check if fibMm2 is a valid location + int i = min(offset+fibMMm2, n-1); + + /* If x is greater than the value at index fibMm2, + cut the subarray array from offset to i */ + if (arr[i] < x) + { + fibM = fibMMm1; + fibMMm1 = fibMMm2; + fibMMm2 = fibM - fibMMm1; + offset = i; + } + + /* If x is greater than the value at index fibMm2, + cut the subarray after i+1 */ + else if (arr[i] > x) + { + fibM = fibMMm2; + fibMMm1 = fibMMm1 - fibMMm2; + fibMMm2 = fibM - fibMMm1; + } + + /* element found. return index */ + else return i; + } + + /* comparing the last element with x */ + if(fibMMm1 && arr[offset+1]==x)return offset+1; + + /*element not found. return -1 */ + return -1; +} + +/* driver function */ +int main(void) +{ + int n, x, i; + scanf("%d %d",&n,&x); + + int arr[n]; + for(i=0; i - -// Utility function to find minimum of two elements -int min(int x, int y) { return (x<=y)? x : y; } - -/* Returns index of x if present, else returns -1 */ -int fibonacciSearch(int arr[], int x, int n) -{ - /* Initialize fibonacci numbers */ - int fibMMm2 = 0; // (m-2)'th Fibonacci No. - int fibMMm1 = 1; // (m-1)'th Fibonacci No. - int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci - - /* fibM is going to store the smallest Fibonacci - Number greater than or equal to n */ - while (fibM < n) - { - fibMMm2 = fibMMm1; - fibMMm1 = fibM; - fibM = fibMMm2 + fibMMm1; - } - - // Marks the eliminated range from front - int offset = -1; - - /* while there are elements to be inspected. Note that - we compare arr[fibMm2] with x. When fibM becomes 1, - fibMm2 becomes 0 */ - while (fibM > 1) - { - // Check if fibMm2 is a valid location - int i = min(offset+fibMMm2, n-1); - - /* If x is greater than the value at index fibMm2, - cut the subarray array from offset to i */ - if (arr[i] < x) - { - fibM = fibMMm1; - fibMMm1 = fibMMm2; - fibMMm2 = fibM - fibMMm1; - offset = i; - } - - /* If x is greater than the value at index fibMm2, - cut the subarray after i+1 */ - else if (arr[i] > x) - { - fibM = fibMMm2; - fibMMm1 = fibMMm1 - fibMMm2; - fibMMm2 = fibM - fibMMm1; - } - - /* element found. return index */ - else return i; - } - - /* comparing the last element with x */ - if(fibMMm1 && arr[offset+1]==x)return offset+1; - - /*element not found. return -1 */ - return -1; -} - -/* driver function */ -int main(void) -{ - int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, - 85, 90, 100}; - int n = sizeof(arr)/sizeof(arr[0]); - int x = 85; - printf("Found at index: %d", - fibonacciSearch(arr, x, n)); - return 0; -} -Upda diff --git a/Searching/Linear Search/README.md b/Searching/Linear Search/README.md index e69de29..8b13789 100644 --- a/Searching/Linear Search/README.md +++ b/Searching/Linear Search/README.md @@ -0,0 +1 @@ + diff --git a/Searching/Linear Search/linearSearch.c b/Searching/Linear Search/linearSearch.c new file mode 100644 index 0000000..bc3955f --- /dev/null +++ b/Searching/Linear Search/linearSearch.c @@ -0,0 +1,22 @@ +#include + +int search(int arr[], int n, int x) +{ + int i; + for (i = 0; i < n; i++) + if (arr[i] == x) + return i; + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int x = 10; + int n = sizeof(arr) / sizeof(arr[0]); + int result = search(arr, n, x); + (result == -1) ? printf("Element is not present in array") + : printf("Element is present at index %d", + result); + return 0; +} \ No newline at end of file diff --git a/Searching/Linear Search/linear_search_array_approach.cpp b/Searching/Linear Search/linear_search_array_approach.cpp deleted file mode 100644 index ab94bd0..0000000 --- a/Searching/Linear Search/linear_search_array_approach.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -using namespace std; - -int search(int arr[], int n, int x) -{ - int i; - for (i = 0; i < n; i++){ - if (arr[i] == x){ - return i; - } - } - return -1; -} - -int main(void) -{ - int n; //number of elements in array - cin >> n; - int arr[n]; - //storing the elements in array - for(int i = 0 ; i> arr[i]; - } - int x; //element to be searched - cin >> x; - int result = search(arr, n, x); - (result == -1)? cout << "Element is not present in array" - : cout << "Element is present at index " << result; - return 0; -} diff --git a/Searching/Linear Search/linear_search_vector_approach.cpp b/Searching/Linear Search/linear_search_vector_approach.cpp deleted file mode 100644 index 503ccb2..0000000 --- a/Searching/Linear Search/linear_search_vector_approach.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// #include -#include -#include -using namespace std; - -int search(vector arr, int n, int x) -{ - int i; - for (i = 0; i < n; i++) - if (arr[i] == x) - return i; - return -1; -} - -// Driver code -int main() -{ - vector arr; - int n; - int x; - cout << "Enter the number of elements in input : "; - cin >> n; - for(int i = 0; i < n ; i++) - { - cout << "Enter the input value of element "<> p; - arr.push_back(p); - } - cout << "Enter the value to be searched: "; - cin >> x; - int index = search(arr, n, x); - if (index == -1) - cout << "Element is not present in the array"; - else - cout << "Element found at position " << index; - - return 0; -} diff --git a/Searching/Linear Search/test b/Searching/Linear Search/test deleted file mode 100644 index 9daeafb..0000000 --- a/Searching/Linear Search/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Searching/test b/Searching/test deleted file mode 100644 index 9daeafb..0000000 --- a/Searching/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Sorting/Bubble Sort/Bubble_Sort.cpp b/Sorting/Bubble Sort/Bubble_Sort.cpp deleted file mode 100644 index 262e125..0000000 --- a/Sorting/Bubble Sort/Bubble_Sort.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -using namespace std; -using namespace std::chrono; - -/* -Bubblesort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements -that are out of order. - -BUBBLESORT(A) -1 for i ← 1 to length[A] -2 do for j ← length[A] downto i + 1 -3 do if A[j] < A[j - 1] -4 then exchange A[j] ↔ A[j - 1] - -Worst case time complexity: O(n^2) when the list is not pre-sorted -Average case time complexity: O(n^2) -Best case time complexity: O(n) list is pre sorted - -Space complexity: O(1) - -*/ - - -void bubble_sort(int *arr, int n) { - int i, j; - for (i = 0; i < n - 1; i++) { - for (j = 0; j < n - i - 1 ; j++) { - if (arr[j] > arr[j + 1]) { - int temp = arr[j]; - arr[j] = arr[j+1]; - arr[j+1] = temp; - } - } - } -} - -//If the array gets sorted in any iteration before the n^2 th iteration, the sorting process is halted -void optimized_bubble_sort(int *arr, int n) { - int i, j; - for (i = 0; i < n; i++) { - //initialize flag to monitor swapping process - bool flag = 0; - for (j = 0; j > n - i - 1; j++) { - if (arr[j] > arr[j + 1]) { - // set flag to 1 if the swapping condition is true anywhere in the inner for loop - flag = 1; - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - // if the flag is still not set that means the array is sorted and thus there is no need to continue the sorting process - if (!flag) { - break; - } - } -} - - -void get_input(int *arr, int n) { - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } -} - -int main() -{ - int *arr, n; - cout << "Number of elements: "; - cin >> n; - arr = new int[n]; - - //get the elements of the array - cout << "Elements of array: " << endl; - get_input(arr, n); - - //perform bubble sort - auto start = high_resolution_clock::now(); - bubble_sort(arr, n); - auto stop = high_resolution_clock::now(); - auto duration = duration_cast(stop - start); - cout << "Running time: " << duration.count() << " microseconds." <(stop2 - start2); - cout << "\nRunning time for already sorted list and normal bubble sort: " << duration2.count() << " microseconds" << endl; - - auto start3 = high_resolution_clock::now(); - optimized_bubble_sort(arr, n); - auto stop3 = high_resolution_clock::now(); - auto duration3 = duration_cast(stop3 - start3); - cout << "\nRunning time for already sorted list and optimized bubble sort: " << duration3.count() << " microseconds" << endl; - - cout << "Enter values again, first number of elements: " << endl; - cin >> n; - cout << "Enter values: " << endl; - get_input(arr, n); - - auto start4 = high_resolution_clock::now(); - optimized_bubble_sort(arr, n); - auto stop4 = high_resolution_clock::now(); - auto duration4 = duration_cast(stop4 - start4); - cout << "\nRunning time for optimized bubble sort: " << duration4.count() << " microseconds" << endl; - - cout << "Press any key to exit. "; - cin.get(); - cin.ignore(); - -} - diff --git a/Sorting/Bubble Sort/Bubble_Sort.exe b/Sorting/Bubble Sort/Bubble_Sort.exe deleted file mode 100644 index 718f89d..0000000 Binary files a/Sorting/Bubble Sort/Bubble_Sort.exe and /dev/null differ diff --git a/Sorting/Bubble Sort/test b/Sorting/Bubble Sort/test deleted file mode 100644 index 9daeafb..0000000 --- a/Sorting/Bubble Sort/test +++ /dev/null @@ -1 +0,0 @@ -test diff --git a/Sorting/Bucket Sort/Bucket_Sort.cpp b/Sorting/Bucket Sort/Bucket_Sort.cpp deleted file mode 100644 index b232a3a..0000000 --- a/Sorting/Bucket Sort/Bucket_Sort.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include -using namespace std; -void display(float *array, int size) { - for(int i = 0; i bucket[size]; - for(int i = 0; i> n; - float arr[n]; //create an array with given number of elements - cout << "Enter elements:" << endl; - for(int i = 0; i> arr[i]; - } - cout << "Array before Sorting: "; - display(arr, n); - bucketSort(arr, n); - cout << "Array after Sorting: "; - display(arr, n); -} \ No newline at end of file diff --git a/Sorting/Insertion Sort/Insertion_Sort.cpp b/Sorting/Insertion Sort/Insertion_Sort.cpp deleted file mode 100644 index 2e3c5ad..0000000 --- a/Sorting/Insertion Sort/Insertion_Sort.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include -using namespace std; -using namespace std::chrono; - -/* - -input: a sequence of n numbers - a_0, a_1, ... a_(n-1) -output: a reordering of numbers where they are arranged in ascending order - -Pseudo code: -1 for j ← 2 to length[A] -2 do key ← A[j] -3 Insert A[j] into the sorted sequence A[1  j - 1]. -4 i ← j - 1 -5 while i > 0 and A[i] > key -6 do A[i + 1] ← A[i] -7 i ← i - 1 -8 A[i + 1] ← key - -*/ - -/* - 1> Worst case: O(n^2) time complexity and swaps - 2> Best case: O(n) time complexity and O(1) swaps - 3> Average case: O(n^2) time complexity and swaps - - Space complexity: Worst case - O(n) -*/ - -void insertion_sort(int *arr, int n) { - int iter = 2, key = 0, i; - for (iter = 1; iter < n; iter++) { - key = arr[iter]; - i = iter - 1; - while (i > -1 && arr[i] > key) { - arr[i + 1] = arr[i]; - i = i - 1; - } - arr[i + 1] = key; - } -} - -void input(int *arr, int n) { - cout << "Elements: "; - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } -} - -int main() -{ - int n, *arr; - cout << "Number of elements: "; - cin >> n; - arr = new int[n]; - - input(arr, n); - - auto start = high_resolution_clock::now(); - insertion_sort(arr, n); - auto stop = high_resolution_clock::now(); - auto duration1 = duration_cast(stop - start); - cout << "Running time: " << duration1.count() << " microseconds" << endl; - - - display(arr, n); - - start = high_resolution_clock::now(); - insertion_sort(arr, n); - stop = high_resolution_clock::now(); - auto duration2 = duration_cast(stop - start); - cout << "\n\nRunning time: " << duration2.count() << " microseconds" << endl; - cout << "Press any key to exit. "; - cin.get(); - cin.ignore(); -} diff --git a/Sorting/Insertion Sort/Insertion_Sort.exe b/Sorting/Insertion Sort/Insertion_Sort.exe deleted file mode 100644 index 564b25c..0000000 Binary files a/Sorting/Insertion Sort/Insertion_Sort.exe and /dev/null differ diff --git a/Sorting/Insertion Sort/insertion.c b/Sorting/Insertion Sort/insertion.c new file mode 100644 index 0000000..57fe66d --- /dev/null +++ b/Sorting/Insertion Sort/insertion.c @@ -0,0 +1,40 @@ +#include + +void insertionSort(int arr[], int n) +{ + int i, k, j; + for (i = 1; i < n; i++) { + k = arr[i]; + j = i - 1; + while (j >= 0 && arr[j] > k) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = k; + } +} + +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +void main(){ + + int i, j, c, t, arr[30]; + + printf("Enter the total number of elements:"); + scanf("%d",&c); + + printf("Enter %d elements: ", c); + + for(i=0;i -#include - -using namespace std; -using namespace std::chrono; -# define MAX 100000 -void get_array(int *arr, int n) { - cout << "Elements: " << endl; - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void merge_array(int *arr, int p, int q, int r) { - int n1 = q - p + 1; - int n2 = r - q; - int *L = new int[n1], *R = new int[n2]; - int i = 0, j = 0; - while (i < n1) { - L[i] = arr[p + i]; - i++; - } - while (j < n2) { - R[j] = arr[q + j + 1]; - j++; - } - //L[n1] = MAX; - //R[n2] = MAX; - i = 0; j = 0; - int k = p; - while (i < n1 && j < n2) { - if (L[i] <= R[j]) { - arr[k] = L[i]; - i++; - } - else { - arr[k] = R[j]; - j++; - } - k++; - } - - while (i < n1) { - arr[k] = L[i]; - i++; k++; - } - while (j < n2) { - arr[k] = R[j]; - j++; k++; - } - delete[]L; - delete[]R; -} - - - -void merge_sort(int *arr, int l, int r) { - if (l < r) { - int m = (l+r)/ 2; - - merge_sort(arr, l, m); - - merge_sort(arr, m + 1, r); - - merge_array(arr, l, m, r); - } -} - - -void display_array(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } -} - -int main() -{ - int n, *arr; - cout << "Number of elements: "; - cin >> n; - arr = new int[n]; - - get_array(arr, n); - - auto start = high_resolution_clock::now(); - merge_sort(arr, 0, n - 1); - auto stop = high_resolution_clock::now(); - auto duration = duration_cast(stop - start); - cout << "\nRunning time: " << duration.count() << " microseconds" << endl; - - display_array(arr, n); - - delete[]arr; - - cout << "Press any key to exit. "; - cin.get(); - cin.ignore(); -} - diff --git a/Sorting/Merge Sort/Merge_Sort.exe b/Sorting/Merge Sort/Merge_Sort.exe deleted file mode 100644 index bd04837..0000000 Binary files a/Sorting/Merge Sort/Merge_Sort.exe and /dev/null differ diff --git a/Sorting/Merge Sort/README.md b/Sorting/Merge Sort/README.md index 0e5fe54..7174a51 100644 --- a/Sorting/Merge Sort/README.md +++ b/Sorting/Merge Sort/README.md @@ -1,3 +1,3 @@ -# Merge Sort +# Merge Sort - Python -- Aditya Mandke + \ No newline at end of file diff --git a/Sorting/Merge Sort/mergeSort.c b/Sorting/Merge Sort/mergeSort.c new file mode 100644 index 0000000..dac0382 --- /dev/null +++ b/Sorting/Merge Sort/mergeSort.c @@ -0,0 +1,104 @@ +/* C program for Merge Sort */ +#include +#include + +// 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 i, j, k; + 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 (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1 + j]; + + /* Merge the temp arrays back into arr[l..r]*/ + i = 0; // Initial index of first subarray + j = 0; // Initial index of second subarray + k = l; // 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 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) { + // Same as (l+r)/2, but avoids overflow for + // large l and h + int m = l + (r - l) / 2; + + // Sort first and second halves + 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) +{ + int i; + for (i = 0; i < size; i++) + printf("%d ", A[i]); + printf("\n"); +} + +/* Driver program to test above functions */ +int main() +{ + + int arr_size, i; + scanf("%d ",&arr_size); + + int arr[arr_size]; + + for(i=0; i -#include "arrayio.h" -#include -using namespace std; -using namespace std::chrono; -void swap(int *xp, int *yp) { - int temp = *xp; - *xp = *yp; - *yp = temp; -} - -int partition(int *arr, int p, int r) { - int x = arr[r]; - int i = p - 1; - int j = p; - for (int j = p; j <= r - 1; j++) { - if (arr[j] <= x) { - i += 1; - swap(&arr[i], &arr[j]); - } - } - swap(&arr[i + 1], &arr[r]); - return i + 1; -} - -void quicksort(int *arr, int p, int r) { - if (p < r) { - int q = partition(arr, p, r); - quicksort(arr, p, q - 1); - quicksort(arr, q + 1, r); - } -} - -int main() -{ - int *arr, n; - cout << "Elements: "; - cin >> n; - arr = new int[n]; - cout << "Enter elements: "; - get_input(arr, n); - - auto start = high_resolution_clock::now(); - quicksort(arr, 0, n - 1); - auto stop = high_resolution_clock::now(); - auto duration = duration_cast(stop - start); - cout << "Running time of algorithm is: " << duration.count() << " microseconds. " << endl; - - cout << "Sorted array is: " << endl; - display(arr, n); - delete[]arr; - - cout << "Press any key to exit "; - cin.get(); - cin.ignore(); -} diff --git a/Sorting/Quicksort/README.md b/Sorting/Quicksort/README.md index 226c568..bdb3802 100644 --- a/Sorting/Quicksort/README.md +++ b/Sorting/Quicksort/README.md @@ -1 +1 @@ -# Quicksort +# Quicksort - Python diff --git a/Sorting/Quicksort/arrayio.cpp b/Sorting/Quicksort/arrayio.cpp deleted file mode 100644 index 61f7c4b..0000000 --- a/Sorting/Quicksort/arrayio.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "arrayio.h" -#include -using namespace std; - -void get_input(int *arr, int n) { - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } - cout << endl; -} \ No newline at end of file diff --git a/Sorting/Quicksort/arrayio.h b/Sorting/Quicksort/arrayio.h deleted file mode 100644 index 45f6508..0000000 --- a/Sorting/Quicksort/arrayio.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#ifndef _ARRAYIO_H -#define _ARRAYIO_H - -void get_input(int *arr, int n); - -void display(int *arr, int n); - -#endif diff --git a/Sorting/Quicksort/quicksort.c b/Sorting/Quicksort/quicksort.c new file mode 100644 index 0000000..7f69168 --- /dev/null +++ b/Sorting/Quicksort/quicksort.c @@ -0,0 +1,63 @@ +#include +void quicksort(int number[25],int first,int last){ + + int i, j, pivot, temp; + + if(first number[pivot] ) + j--; + if( i < j ){ + + temp=number[i]; + + number[i]=number[j]; + + number[j]=temp; + } + } + + temp = number[pivot]; + + number[pivot] = number[j]; + + number[j] = temp; + + quicksort(number, first, j-1); + + quicksort(number, j+1, last); + + } +} + +int main(){ + int i, count, number[25]; + + printf("Enter the number of elements u want to enter"); + + scanf("%d",&count); + + printf("Enter %d elements: ", count); + + for( i=0; i -#include - -using namespace std; -using namespace std::chrono; - -/* -The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) -from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. - -1) The subarray which is already sorted. -2) Remaining subarray which is unsorted. - -In every iteration of selection sort, the minimum element (considering ascending order) -from the unsorted subarray is picked and moved to the sorted subarray. -*/ - -/* - Begin to iterate through the array from index 0 to n - 1, iterator = i - find the smallest element in this range, let the index of element be min_ind - swap elements at indices i, and min_ind - continue -*/ - -void selection_sort(int *arr, int n) { - int i, j, min_ind = 0, ind = 0; - for (i = 0; i < n; i++) { - min_ind = arr[i]; - ind = i; - for (j = i + 1; j < n; j++) { - if (min_ind > arr[j]) { - min_ind = arr[j]; - ind = j; - } - } - int temp = arr[i]; - arr[i] = min_ind; - arr[ind] = temp; - } -} - -void input(int *arr, int n) { - cout << "Elements: "; - for (int i = 0; i < n; i++) { - cin >> arr[i]; - } -} - -void display(int *arr, int n) { - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } -} - -int main() -{ - int *arr, n; - cout << "Number of elements: " << endl; - cin >> n; - - arr = new int[n]; - - input(arr, n); - - auto start = high_resolution_clock::now(); - selection_sort(arr, n); - auto stop = high_resolution_clock::now(); - auto duration = duration_cast(stop - start); - cout <<"\nRunning time of selection sort is: " << duration.count() <<" microseconds" << endl; - - cout << "\nSorted array is: " << endl; - display(arr, n); - - cout << "Press any key to exit. "; - cin.get(); - cin.ignore(); -} \ No newline at end of file diff --git a/Sorting/Selection Sort/Selection_Sort.exe b/Sorting/Selection Sort/Selection_Sort.exe deleted file mode 100644 index ff42993..0000000 Binary files a/Sorting/Selection Sort/Selection_Sort.exe and /dev/null differ diff --git a/Sorting/Selection Sort/selectionSort.c b/Sorting/Selection Sort/selectionSort.c new file mode 100644 index 0000000..2e4dec5 --- /dev/null +++ b/Sorting/Selection Sort/selectionSort.c @@ -0,0 +1,49 @@ +#include +#include + +void swap(int *xp, int *yp){ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void selectionSort(int *arr, int n){ + int i, j, min_idx; + for (i = 0; i < n-1; i++) { + min_idx = i; + for (j = i+1; j < n; j++){ + if (arr[j] < arr[min_idx]) + min_idx = j; + } + swap(&arr[min_idx], &arr[i]); + } +} + +void printArray(int *arr, int size){ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +void input(int *arr, int n){ + printf("Elements: "); + for (int i=0; i