diff --git a/CPP/Bucket_sort.cpp b/CPP/Bucket_sort.cpp new file mode 100644 index 0000000..8e5958d --- /dev/null +++ b/CPP/Bucket_sort.cpp @@ -0,0 +1,55 @@ +#include +#define ll long long +using namespace std; +/* +Bucket sort is a sorting algorithm which sorts data in a specific range and is uniformly distributed in linear time. + +If we take the number of buckets as n or n/2 or n/3 then the time requirred for insertion,sorting,joining of buckets take linear time. + +This algorithm will work very badly if the data is not uniformly dist this algorithm works very badly. + +Assuming n approx = k / when we have perfectly distributed data : + +Time complexity : O(n) (when we have constant elements in each bucket sorting the elements takes const time and concat is also O(n) operation) + +Worst case : when the elements are not uniformly dist and all elements end up in one bucket. So if we use insertion sort we get O(n^2). +But if we use some other algo to sort the buckets we get O(nlogn) + +*/ +void bucket_sort(int arr[],int n,int k){ + int range=*max_element(arr,arr+n); + range++; //this is done so that in case range%k!=0 we do not cross k in the formula to find the index of the bucket. + vector bkt[k]; + for(int i=0;i +using namespace std; +/* +Counting sort is linear time algo used when our input elements are in a small range. + +Not a comparision based algo only counts the occurences of elements. + +Time complexity : O(n+k) (n-number of elements, k-range of elements) + +Auxillary space : O(n+k) + +Stable + +Used as a subroutine in radix sort. + +Used only when k (range) is small i.e n not n^2 or n^3. Otherwise we generally use radix sort algorithm. +*/ +void counting_sort(int arr[],int n) +{ + int mini=*min_element(arr,arr+n); + int maxi=*max_element(arr,arr+n); + int k=maxi-mini+1; + int freq[k]; + for(int i=0;i=0;i--) + { + temp[freq[arr[i]-mini]-1]=arr[i]; + freq[arr[i]-mini]--; + } + //We traverse fromm the right so that the sort is stable. + for(int i=0;i +#include + +using namespace std; + +//Utility functions +int parent(int i) +{ + return floor(i); +} +int left(int i) +{ + return 2 * i; +} +int right(int i) +{ + return (2 * i) + 1; +} + +//Sorts the heap in descending order +void maxHeapify(int *array, int i, int size) +{ + int l = left(i); + int r = right(i); + int largest; + for (int i = 0; i < size; i++) + { + cout << *(array + i) << " "; + } + cout << endl; + if (l *(array + i)) + { + largest = l; + } + else + { + largest = i; + } + if (r *(array + largest)) + { + largest = r; + } + if (largest != i) + { + int temp; + temp = *(array + i); + *(array + i) = *(array + largest); + *(array + largest) = temp; + maxHeapify(array, largest, size); + } +} + +//Builds heap from array +//Time complexity(worst) : O(n) +int *buildHeapFromArray(int *array, int size) +{ + for (int i = floor(size / 2); i >= 0; i--) + { + maxHeapify(array, i, size); + } + cout << "Built heap from array" << endl; + return array; +} + +//Heap Sort function +//Arguments: pointer to array(*array) and size of array(n) +//Time Complexity(worst) : O(nlogn) +//Sorts in descending order +//Returns the pointer to the first element in sorted heap +int *heapSort(int *array, size_t n) +{ + int *a = buildHeapFromArray(array, n); + for (int i = 0; i < n; i++) + { + cout << *(a + i) << " "; + } + cout << endl + << "Done" << endl; + for (int i = n - 1; i > 0; i--) + { + int temp; + temp = *(a + i); + *(a + i) = *(a + 1); + *(a + 1) = temp; + maxHeapify(a, 0, n); + } + return array; +} + +int main() +{ + int array[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; + + int n = sizeof(array) / sizeof(array[0]); + + int *array1 = heapSort(array, sizeof(array) / sizeof(array[0])); + + for (int i = 0; i < n; i++) + { + std::cout << *(array1 + i) << " "; + } + std::cout << std::endl; + + return 0; +} \ No newline at end of file diff --git a/CPP/InsertionSort.cpp b/CPP/InsertionSort.cpp new file mode 100644 index 0000000..365429e --- /dev/null +++ b/CPP/InsertionSort.cpp @@ -0,0 +1,57 @@ +#include + +//Insertion sort function +//Arguments: pointer to array(*array) and size of array(n) +//Time Complexity(worst) : O(n^2) +//Returns the pointer to the first element in array +int *insertionSort(int *array, size_t n) +{ + //Print the inital array + std::cout << "Initial Array: " << std::endl; + for (int i = 0; i < n; i++) + { + std::cout << *(array + i) << " "; + } + std::cout << std::endl; + + //Insertion sort Algo + //For loop from 1 to length of array + for (int j = 1; j < n; j++) + { + int key = *(array + j); + std::cout << "key " << key << std::endl; + int i = j - 1; + //While loop + //Conditions : i shouldn't be out of bounds of array and array[i] greater than key + //Keep on shifting elements right until end of array is reached(left end) and element is greater than key + while (i >= 0 && *(array + i) > key) + { + *(array + i + 1) = *(array + i); + i -= 1; + } + *(array + i + 1) = key; + + //Print the array after each pass + std::cout << "Pass no. " << j - 1 << std::endl; + for (int i = 0; i < n; i++) + { + std::cout << *(array + i) << " "; + } + std::cout << std::endl; + } + return array; +} + +int main() +{ + //initialize array + int a[] = {1231, 123123, 12, 3, 123, 21, 3, 12, 3, 24, 234, 4542, 5, 43, 55, 3, 542, 52, 53, 24, 32, 4, 123, 41, 24, 3, 52, 5, 2, 4, 24, 35412534, 5, 43, 435, 62, 45437, 787, 568, 4, 8766, 85, 6, 55, 76, 48, 6547, 46, 73, 5426, 5, 2, 53, 45, 4324, 5, 34, 264, 25, 4, 5, 2, 413, 5, 43, 156654326, 45, 76, 473, 567, 5, 8, 678, 467, 8, 247, 6, 254, 4325, 4325, 1, 3245, 134, 6, 436, 2354, 465, 36, 245, 63, 456, 54, 7, 345, 765, 426, 4, 56, 254, 654, 6, 4}; + + //Calculate the length of array + size_t n = sizeof(a) / sizeof(a[0]); + + //call insertion sort function + int *array = insertionSort(a, n); + + return 0; +} \ No newline at end of file diff --git a/CPP/MergeSort.cpp b/CPP/MergeSort.cpp index b298c44..7c67bff 100644 --- a/CPP/MergeSort.cpp +++ b/CPP/MergeSort.cpp @@ -1,73 +1,83 @@ -#include -#include -#include - -void merge(int A[], int p, int q, int r) -{ - - int n1 = q - p + 1 ; - int n2 = r - q ; - - std::vectorL; - std::vectorR; - - for (int i = 0; i <= n1; i++) - { - L.push_back(0); - } - for (int j = 0; j <= n2; j++) - { - R.push_back(0); - } - - - for (int i = 0; i < n1; i++) - { - L[i] = A[p + i]; - } - for (int j = 0; j < n2; j++) - { - R[j] = A[q+j+1]; - } - - int i = 0; - int j = 0; - - L[n1] = INT_MAX; - R[n2] = INT_MAX; - - for(int k = p ; k <= r; k++) - { - if (L[i] <= R[j]) - { - A[k] = L[i]; - i = i + 1; - } - else - { - A[k] = R[j]; - j = j + 1; - } - } -} -void MergeSort(int A[], int p, int r) -{ - if (p < r) - { - int q =(p + r) / 2; - MergeSort(A, p, q); - MergeSort(A, q + 1, r); - merge(A, p, q, r); - } -} - -int main() -{ - int arr[] = {1, 2, 9, 2, 4, 5}; - int arrsize = sizeof(arr) / sizeof(arr[0]); - MergeSort(arr,0,(arrsize-1)); - for (int i = 0; i < arrsize; i++) - { - std::cout << arr[i] << " "; - } -} +#include + +void merge(int *array, int left, int middle, int right) +{ + int i, j, k; + int n1 = middle - left + 1; + int n2 = right - middle; + + int *L = new int[n1], *R = new int[n2]; + + for (i = 0; i < n1; i++) + L[i] = array[left + i]; + for (j = 0; j < n2; j++) + R[j] = array[middle + 1 + j]; + + i = 0; + j = 0; + k = left; + while (i < n1 || j < n2) + { + if (j >= n2 || (i < n1 && L[i] <= R[j])) + { + array[k] = L[i]; + i++; + } + else + { + array[k] = R[j]; + j++; + } + k++; + } + std::cout << "n1 : " << n1 << " n2 : " << n2 << std::endl; + std::cout << "Left : "; + for (int i = 0; i < n1; i++) + { + std::cout << L[i] << " "; + } + std::cout << std::endl; + std::cout << "Right : "; + for (int i = 0; i < n2; i++) + { + std::cout << R[i] << " "; + } + std::cout << std::endl; + std::cout << "Array : " << std::endl; + for (int i = 0; i < 10; i++) + { + std::cout << array[i] << " "; + } + std::cout << std::endl + << std::endl; + + delete[] L; + delete[] R; +} + +void mergeSort(int *array, int left, int right) +{ + if (left < right) + { + int middle = left + (right - left) / 2; + mergeSort(array, left, middle); + mergeSort(array, middle + 1, right); + merge(array, left, middle, right); + } +} + +int main() +{ + int array[] = {234, 234, 2134, 23, 5, 245, 34, 346, 567, 76}; + + size_t n = sizeof(array) / sizeof(array[0]); + mergeSort(array, 0, n - 1); + + for (int i = 0; i < n; i++) + { + std::cout << *(array + i) << " "; + } + std::cout << std::endl; + + return 0; +} \ No newline at end of file diff --git a/CPP/QuickSort.cpp b/CPP/QuickSort.cpp new file mode 100644 index 0000000..898c281 --- /dev/null +++ b/CPP/QuickSort.cpp @@ -0,0 +1,48 @@ +#include + +namespace sorting +{ + int partition(int arr[], int low, int high) + { + int pivot = arr[high]; + int i = (low - 1); + + for (int j = low; j < high; j++) + { + if (arr[j] <= pivot) + { + i++; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + return (i + 1); + } + void quickSort(int arr[], int low, int high) + { + if (low < high) + { + int p = partition(arr, low, high); + quickSort(arr, low, p - 1); + quickSort(arr, p + 1, high); + } + } + +} + +int main() +{ + int arr[] = {324, 45, 34, 54645675, 8678675, 9, 869, 879, 78, 978, 59, 56, 73, 54, 25, 43, 432151, 64575}; + int size = sizeof(arr) / sizeof(arr[0]); + sorting::quickSort(arr, 0, size); + for (int i = 0; i < size; i++) + { + std::cout << arr[i] << " "; + } + std::cout << std::endl; + return 0; +} \ No newline at end of file diff --git a/CPP/Radix_sort.cpp b/CPP/Radix_sort.cpp new file mode 100644 index 0000000..57f0963 --- /dev/null +++ b/CPP/Radix_sort.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; +/* +Generally we use radix sort algorithm instead of using counting sort. + +Linear time algo if the data is within a range. + +Radix sort works in linear time even if the data is in range of n^2 or n^3. + +*/ +void counting_sort(int arr[],int n,int exp) +{ + int k=10; + int freq[k]; + for(int i=0;i=0;i--) + { + temp[freq[(arr[i]/exp)%10]-1]=arr[i]; + freq[(arr[i]/exp)%10]--; + } + //We traverse fromm the right so that the sort is stable. + for(int i=0;i + +//Function for selection sort +//Arguments: pointer to array(*array) and size of array(n) +//Time Complexity : O(n^2) +//Returns a pointer to the sorted array +int *selectionSort(int *array, size_t n) +{ + //Print the inital array + std::cout << "Initial Array: " << std::endl; + for (int a = 0; a < n; a++) + { + std::cout << array[a] << " "; + } + std::cout << std::endl; + + //Selection Sort Algo + //For loop from 0 to length of array + for (int i = 0; i < n; i++) + { + int min = i; + + //For loop to find the element smaller than element at ith position + for (int j = i + 1; j < n; j++) + { + if (array[j] < array[min]) + { + min = j; + } + } + + //Swap the elements in the minimum position and i + int temp = array[i]; + array[i] = array[min]; + array[min] = temp; + + //Print the array after every pass + std::cout << "Pass no : " << i << std::endl; + for (int a = 0; a < n; a++) + { + std::cout << array[a] << " "; + } + std::cout << std::endl; + } + return array; +} + +int main() +{ + //Initialize the array + int array[] = {345, 5435, 34, 5, 435, 43, 5, 1, 335, 4, 3263, 1, 6254, 6, 457, 65, 37, 68, 7456, 7, 354, 66, 572, 54, 26, 254, 325, 432}; + + //Calculate the length of array + size_t n = sizeof(array) / sizeof(array[0]); + + //Call the selection sort function + int *array2 = selectionSort(array, n); + + return 0; +} \ No newline at end of file