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 Sorting algorithms #315

Open
wants to merge 1 commit 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
55 changes: 55 additions & 0 deletions CPP/Bucket_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<bits/stdc++.h>
#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 <ll> bkt[k];
for(int i=0;i<n;i++)
{
int bi=(k*arr[i])/range; //formula to decide the bucket to put the element in.
bkt[bi].push_back(arr[i]);
}
for(int i=0;i<k;i++)
{
sort(bkt[i].begin(),bkt[i].end());
}
int index=0;
for(int i=0;i<k;i++)
{
for(int j=0;j<bkt[i].size();j++)
{
arr[index]=bkt[i][j];
index++;
}
}
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main(){
int arr[]={20,25,10,5,30,36,41,45,50,59,60};
int size=sizeof(arr)/sizeof(int);
print(arr,size);
bucket_sort(arr,size,4);
print(arr,size);
return 0;
}
61 changes: 61 additions & 0 deletions CPP/Counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<bits/stdc++.h>
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<k;i++)
freq[i]=0;

for(int i=0;i<n;i++)
{
freq[arr[i]-mini]++;
}
for(int i=1;i<k;i++)
{
freq[i]+=freq[i-1];
}
//Now freq[i] represent the count of all the elements smaller than or equal to i.
int temp[n];
for(int i=n-1;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<n;i++)
{
arr[i]=temp[i];
}
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main(){
int arr[]={1,4,4,1,0,1};
int size=sizeof(arr)/sizeof(int);
print(arr,size);
counting_sort(arr,size);
print(arr,size);
return 0;
}
105 changes: 105 additions & 0 deletions CPP/HeapSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <iostream>
#include <cmath>

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<size && *(array + l) > *(array + i))
{
largest = l;
}
else
{
largest = i;
}
if (r<size && *(array + 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;
}
57 changes: 57 additions & 0 deletions CPP/InsertionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>

//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;
}
Loading