Skip to content
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions CPP/Data Structures/Stack Using Arrays/stackClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class stack {
this -> size = size;
p = new t[size];
}

~stack(){
delete [] p;
}

int getSize(){
return size;
Expand Down
33 changes: 33 additions & 0 deletions CPP/Data Structures/queue/queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include "queueClass.cpp"

using namespace std;

// dynamic queue, changes its size according to input

int main() {
Queue<int> q1(5);
for (int i = 0; i < 5; i++) {
q1.enqueue(i + 1);
}
cout << "Dequeued: " << q1.dequeue() << endl;
cout << "Dequeued: " << q1.dequeue() << endl;
q1.enqueue(6);
q1.enqueue(7);
while (!q1.empty()) {
cout << "Dequeued: " << q1.dequeue() << endl;
}
cout << "Current size of queue: " << q1.size() << endl;

Queue<char> q2(5);
q2.enqueue('h');
q2.enqueue('e');
q2.enqueue('l');
q2.enqueue('l');
q2.enqueue('o');
while (!q2.empty()) {
cout << "Dequeued: " << q2.dequeue() << endl;
}

return 0;
}
82 changes: 82 additions & 0 deletions CPP/Data Structures/queue/queueClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <climits>

using namespace std;

template <typename T>
class Queue {
T *arr;
int head, tail;
int sz;
int capacity;

public:
Queue(int capacity = 10) {
this->capacity = capacity;
sz = 0;
head = 0;
tail = 0;
arr = new T[capacity];
}

int size() {
return sz;
}

bool empty() {
return sz == 0;
}

bool full() {
return sz == capacity;
}

void resize(int cap) {
if(cap < 1) cap = 1;
T* narr = new T[cap];

for(int i=0; i<sz; i++)
narr[i] = arr[(head + i) % capacity];
delete []arr;
arr = narr;
capacity = cap;
}

void enqueue(T value) {
if (full()) {
resize(2 * capacity);
}
arr[tail] = value;
tail = (tail + 1) % capacity;
sz++;
}

T dequeue() {
if(empty()) {
throw std::underflow_error("Queue is empty");
}
T val = arr[head];
head = (head + 1) % capacity;
sz--;
return val;
}

T front() {
if(empty()) {
throw std::underflow_error("Queue is empty");
}
return arr[head];
}

void display() {
for(int i=0; i<sz; i++) {
cout << arr[(head + i) % capacity] << " ";
}
cout << endl;
}

~Queue() {
delete []arr;
}

};
Binary file removed CPP/SortingTechniques/AllSortingTechniques
Binary file not shown.
Binary file removed CPP/SortingTechniques/AllSortingTechniquesBasicLevel
Binary file not shown.
Binary file removed CPP/SortingTechniques/BubbleSort
Binary file not shown.
Binary file removed CPP/SortingTechniques/BucketSort
Binary file not shown.
144 changes: 144 additions & 0 deletions CPP/SortingTechniques/BucketSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Bucket Sort
// Bucket sort is a comparison sort algorithm that operates on elements by dividing them into different buckets
// and then sorting these buckets individually. Each bucket is sorted using a separate sorting algorithm or by
// recursively applying the bucket sort algorithm.
// It is a distribution sort.

//Time Complexity = O(n+k) where k = no. of buckets and n = no. of elements; O(n^2) in worst case
//Space complexity = O(n+k)
#include <iostream>

using namespace std;

class Node{
public:
int data;
Node *next;
};

class LinkedList{
private:
Node *first, *last;

public:
LinkedList(){first = last = NULL;}
~LinkedList();
void Insert(int x);
int Delete();
bool isEmpty();
void Sort();
};

LinkedList :: ~LinkedList()
{
Node *p;
while(first){
first = first->next;
delete p;
}
first = last = NULL;
}

void LinkedList :: Insert(int x)
{
Node *t = new Node;
t->data = x;
t->next = NULL;
if(first == NULL)
first = last = t;
else{
last->next = t;
last = last->next;
}
}

int LinkedList :: Delete()
{
if(first == NULL) return -1;
Node * tr = first;
first = first->next;
int x = tr->data;
delete tr;
return x;
}

bool LinkedList :: isEmpty()
{
return first == NULL;
}

void LinkedList :: Sort()
{
if(!first || !first->next) return; // Array is empty or has one element
Node* sorted = nullptr;
Node* current = first;
while (current != nullptr) {
Node* next = current->next;
if(sorted == nullptr || sorted->data >= current->data) {
current->next = sorted;
sorted = current;
} else {
Node* temp = sorted;
while(temp->next != nullptr && temp->next->data < current->data) {
temp = temp->next;
}
current->next = temp->next;
temp->next = current;
}
current = next;
}
first = sorted;

// reupdate last pointer after sorting
last = first;
while(last && last->next) {
last = last->next;
}
}

int Max(int A[], int n)
{
int mx = INT32_MIN;
for(int i=0; i < n; i++)
if(mx < A[i]) mx = A[i];
return mx;
}

void BucketSort(int A[], int n)
{
int max = Max(A, n);
// Create n buckets (linked lists)
LinkedList *bin = new LinkedList[n];

// Put array elements in different buckets
for(int i=0; i < n; i++)
{
int bin_idx = (long long)n * A[i] / (max + 1);
bin[bin_idx].Insert(A[i]);
}

// Sort individual buckets and concatenate
int i=0;
for(int j=0; j < n; j++)
{
bin[j].Sort(); // Sort each bucket
while(!bin[j].isEmpty())
A[i++] = bin[j].Delete();
}
delete []bin;
}

int main()
{
int A[] = {237, 140, 259, 348, 152, 163, 1244, 48, 36, 62};
int n = sizeof(A)/sizeof(A[0]);

BucketSort(A, n);
//Display
cout << "Sorted List : ";
for(int i=0; i < n; i++)
cout << A[i] << " ";
cout << endl;

return 0;
}