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

Counting sort implementation in C++ #239

Open
wants to merge 2 commits into
base: master
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
124 changes: 124 additions & 0 deletions Singly Linked Lists/cycle_detection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <bits/stdc++.h>

using namespace std;

class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;

SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};

class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;

SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}

void insert_node(int node_data) {
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);

if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}

this->tail = node;
}
};

void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
while (node) {
fout << node->data;

node = node->next;

if (node) {
fout << sep;
}
}
}

void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;

free(temp);
}
}

bool has_cycle(SinglyLinkedListNode* head) {
set<SinglyLinkedListNode *> s;
SinglyLinkedListNode *curr = head;
while (curr != nullptr) {
auto it = s.find(curr);
if (it != s.end())
return true;
s.insert(curr);
curr = curr->next;
}
return false;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int tests;
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
int index;
cin >> index;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

SinglyLinkedList* llist = new SinglyLinkedList();

int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

llist->insert_node(llist_item);
}

SinglyLinkedListNode* extra = new SinglyLinkedListNode(-1);
SinglyLinkedListNode* temp = llist->head;

for (int i = 0; i < llist_count; i++) {
if (i == index) {
extra = temp;
}

if (i != llist_count-1) {
temp = temp->next;
}
}

temp->next = extra;

bool result = has_cycle(llist->head);

fout << result << "\n";
}

fout.close();

return 0;
}

91 changes: 91 additions & 0 deletions Sorting/counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <bits/stdc++.h>

using namespace std;

vector<string> split_string(string);

vector<int> countingSort(vector<int> arr) {
int n = arr.size();
vector<int> ans(n);
vector<int> temp(100);
int i, j, k = 0;
for(i = 0; i < 100; i++)
temp[i] = 0;
for(i = 0; i < n; i++)
temp[arr[i]]++;
for(i = 0; i < 100; i++) {
if(temp[i] == 0)
continue;
for(j = temp[i]; j > 0; j--)
ans[k++] = i;
}
return ans;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

string arr_temp_temp;
getline(cin, arr_temp_temp);

vector<string> arr_temp = split_string(arr_temp_temp);

vector<int> arr(n);

for (int i = 0; i < n; i++) {
int arr_item = stoi(arr_temp[i]);

arr[i] = arr_item;
}

vector<int> result = countingSort(arr);

for (int i = 0; i < result.size(); i++) {
fout << result[i];

if (i != result.size() - 1) {
fout << " ";
}
}

fout << "\n";

fout.close();

return 0;
}

vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});

input_string.erase(new_end, input_string.end());

while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}

vector<string> splits;
char delimiter = ' ';

size_t i = 0;
size_t pos = input_string.find(delimiter);

while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));

i = pos + 1;
pos = input_string.find(delimiter, i);
}

splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

return splits;
}