Relative Content

Tag Archive for c++sorting

How to improve the O(n) sorting algorithm?

I have written a sorting algorithm that is based on normalising values and counting. According to tests on my computer, it is faster than Array.Sort() for sizes up to 1 million. For a wide range of values, it is faster than sorting by counting. The only problems I see are memory usage and using temporary arrays with type ‘int?’, because I know not all languages support it. What data structure could I switch to that would not lose speed and reduce memory usage without losing the idea of index normalisation?

Sorting algorithm updating a variable that it’s not supposed to

#include <cs50.h> #include <ctype.h> #include <stdio.h> #include <string.h> int get_index(char c); string selectionSort(string arr, int n); int main(int argc, string argv[]) { string key = argv[1]; int str_len = strlen(key); string sorted = selectionSort(key, str_len); string selectionSort(string arr, int n) { int i, j, min_idx; for (i = 0; i < n – 1; i++) […]

c++, sorting of custom object: requirements of the comparison function

I have a vector of pointer of a custom object std::vector<MyObject*>. The object has an index, a number and a timestamp (time of creation of the object). The timestamp is unique, the number can be either -1 (the object is not been assigned a number yet) or a positive value; if the object has a number greater than 0, the number is unique.