C :: Space Complexity Of Radix Sort?
Sep 2, 2013
I was just going through Radix Sort algorithm. Though I got the logic and the concept used in it, I am still pretty much confused about the space complexity being O(k+n) for this algorithm. (where, k is the max no of digits in a number, and n is the no. of inputs to be sorted).
View 1 Replies
ADVERTISEMENT
Aug 19, 2013
Example radix sort function to sort an array of 64 bit unsigned integers. To allow for variable bin sizes, the array is scanned one time to create a matrix of 8 histograms of 256 counts each, corresponding to the number of instances of each possible 8 bit value in the 8 bytes of each integer, and the histograms are then converted into indices by summing the histograms counts. Then a radix sort is performed using the matrix of indices, post incrementing each index as it is used.
Code:
typedef unsigned long long UI64;
typedef unsigned long long *PUI64;
PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count) {
size_t mIndex[8][256] = {0};
/* index matrix */
PUI64 pDst, pSrc, pTmp;
size_t i,j,m,n;
UI64 u;
[Code]....
View 9 Replies
View Related
Feb 1, 2013
What would the worst, average and best case space complexity be for a data structure of type map<string, vector<int> > in big O notation? I'm parsing through a document and storing each word as a key and im attaching an associated int (in a vector) to it as the value.
View 4 Replies
View Related
Jan 23, 2013
this function has to check if an array is not descending it has to be recursive and it's space complexity has to be O(log(n))
Code:
int is_sorted(int a[ ], int n) {
int check=n-1;
if(n==1 || n==0) return 1;
if(a[check]<a[check-1])return 0;
else return(is_sorted(a,n-1));
}
this is what i came up with. i know it's space complexity is O(n) how do i make it O(log(n))?
View 2 Replies
View Related
Jan 25, 2015
define the time and space complexity of an algorithm and about what can i do when the time and space complexity are given and i should write the code corresponding to the restrictions , which i find very difficult.
View 1 Replies
View Related
Apr 10, 2013
why we use radix sort.
View 2 Replies
View Related
Jun 13, 2014
I'm trying ultimately to do a radix sort. I'm going with the implementation using two tables. One will initially hold the unsorted values then hold the partially sorted values thereafter. The other will be an array of linked lists which will be where the radix sort is done. Here is my code thus far:
Main
#include <iostream>
#include <fstream>
#include "radix.h"
#include "radix.cpp"
using namespace std ;
int main ( int argc , char * argv [ ] ) {
ifstream input ( argv [ 1 ] ) ;
[Code] ....
It appears to be crashing during void insert_into_sorted ( int to_insert , int place ) ; and I'm not sure why.
View 2 Replies
View Related
May 11, 2014
I'm having trouble implementing my radix sort program on the main .cpp file. Here is what the radix header file looks like::
#include <vector>
#include <queue>
using namespace std;
void distribute(const vector<int> &v, queue<int> digitQueue[], int pwr) {
int i;
for(int i=0; i < v.size(); i++)
digitQueue[(v[i]/pwr) % 10].push(v[i]);
[Code] .....
Here is what my main looks like:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
#include "radix.h"
[Code] ....
My output:
sorted array is:
0 0 0 0 0 0 0 0 0 0
View 1 Replies
View Related
Apr 12, 2014
I'm attempting to build a column based database, and I'm new to C++ (just wanted to play around with building a column base database "for the fun of it"). I want to construct a very fast radix sort, that would allow me to quickly sort groups of columns based on integer values. My general preference is to take up more RAM to get more performance.
I'd like to build the radix sort by allowing 256 "buckets" to drop values in as I'm sorting. This allows me to sort through a group of 4 byte integers in only 4 passes. But assuming I'm trying to sort a large group of values (say 50+ million), I'm not sure what type of container to use for these. Also note I'm pretty unfamiliar with the "standard library", so below are my thoughts:
Vectors:
-Pros: Easy to use, and very fast for sequential and random access inserts / reads
-Cons: If they have to dynamically resize because a given vector wasn't large enough, this can apparently really slow performance. Unless I make another pass over the numbers before I start sorting, I wouldn't know how big to make individual the individual vectors. This means I either have to make them "too big" and waste space, or pay a performance price for either resizing, or scanning data first.
Lists:
-Pros: Seems like I wouldn't have to specify size ahead of time, so I could just easily insert values to a given list. Also, since I don't need random access reads (I'll ready the "0" list sequentially, then the "1" list, etc. they should work fine.
-Cons: I don't really know much about lists, but I'm not sure how easy it is to append a new value to the end of a list. I've read that standard library lists include both "forward" and "backward" pointers, which I don't need. Also, I find it hard to believe that there isn't some time taken up with memory allocation. If I build a list and append x million records in it, is it calling memory allocation routines x million times?
Or maybe there's another container type I should learn?
Again, my goal is to make this "fast", not "memory efficient". But having said that, the fastest way I could think of (use 256 vectors, each sized equal to the total number of members to be sorted) is just too much memory to burn - 256 times a vector big enough to hold millions of elements is too much.
View 4 Replies
View Related
May 5, 2012
For the following code :
s = 0 ;
for(i=m ; i<=(2*n-1) ; i+=m) {
if(i<=n+1)
{ s+=(i-1)/2 ; }
else
{ s+=(2*n-i+1)/2 ; }
}
I want to change the complexity of the code from O(n) to O(1) . So I wanted to eliminate the for loop . But as the sum "s" stores values like (i-1)/2 or (2*n-i+1)/2 so eliminating the loop involves tedious calculation of floor value of each (i-1)/2 or (2*n-i+1)/2 . It became very difficult for me to do so as I might have derived the wrong formula in sums of floors . Need Changing complexity from O(n) to O(1). Is there any other way to reduce the complexity ? If yes ... then how ?
View 3 Replies
View Related
Mar 24, 2013
Assuming that we have :
Code:
int arr2d[rows][columns] ; // Not valid syntax of course ... let be arr2d rows * columns size
for(int i=0; i<rows; i++)
for(int j=0; j<columns; j++)
arr2d[rows][columns] = some_value;
What is the complexity? I believe O(n) and not O(n^2) on this case because if you have 3*3 size you would put 9 elements (from 9 elements input of course)... for each input you have one insertion and that is the meaning. Same as 4*4 size 16 input times 16 insertions .. or 5*5 and so forth...
View 8 Replies
View Related
Jul 21, 2014
I have a very simple program the time complexity of the function that I used in this program is O(mn)because it has a nested loop, I really need to reduce the time complexity to O(n)
[code=c++]
#include <iostream.h>
#include<stdlib.h>
int *char_count( const char* DNA, const int *starts, const int *ends, char letter);
int main()
[Code].....
View 5 Replies
View Related
Feb 9, 2015
Giving a dynamic array, we want to pass the array elements from a file. The first number in the file N gives us the array length. They follow N numbers, the actual elements of the array.
Three brothers are going to work in the shop of their father for a time. The shop is doing well and every day generates profit which the three brothers may provide. The brothers agreed that they would divide the total time in three successive parts, not necessarily equal duration, and that each one will be working in the shop during one of these parts and collects the corresponding profit on his behalf. But they want to make a deal fairly, so that one received from three more profit someone else. Specifically, they want to minimize the profit the most favored of the three will receive.
I first created a program that WORKS! with complexity O(n3).
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int sum_array(int* array, int cnt){
int res = 0;
int i;
for ( i = 0; i < cnt ; ++i)
[Code] .....
Let's assume that we have the following input:
10
1 1 8 1 1 3 4 9 5 2
The ouptut should be 15 -> A = 1 + 1 + 8 + 1 + 1 + 3 = 15 , B = 4 + 9 = 13 , C = 5 + 2 = 7.
But the ouptut is 16!
How can I reduce the complexity of my first working! C code?
View 9 Replies
View Related
May 22, 2013
would like to know that my algorithm has asymptotic complexity recursive?
int solve(pair<int,int> actual)
{
if(actual.first == PuntoInicio.first && actual.second == PuntoInicio.second)
return 1;
[Code]....
View 1 Replies
View Related
Feb 14, 2014
Suppose you have a switch statement defined like:
switch (parameter) {
case ONE:
case TWO:
// ... other n-2 cases
case N:
doSomething();
break;
default:
somethingElse();
break;
}
What is the complexity of the doSomething()? Is it still equal to the complexity of doSomething() , or the complexity of doSomething() plus the number of case statements? In other words, does the number of case statements a conditional piece of code has count towards the complexity?
View 10 Replies
View Related
Oct 29, 2014
We were discussing how to find average time complexity of different algorithms. Suppose I've a the following nested loop
for(int i=0;i<n;i++)
{
min = i;
[Code].....
Now the outer loop will iterate N times. the inner loop will always iterate 3 times no matter what the value of N is. so the worst case time complexity should be O(n^2) but what about the average case and the best case? I mean in terms of searching we can figure out and distinguish between the worst,best and average as it depends upon the number of comparisons. But here how can the average and best case be different then the worst case.
View 13 Replies
View Related
Mar 22, 2013
how to sort a 1D array using function sort().but if i have a 2D array how do i sort only 1 row of it (or column)?
View 2 Replies
View Related
Feb 19, 2014
You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.
You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.
I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861) Also, I feel like I'm missing something in int main() to get it to sort properly.
# include <iostream>
using namespace std;
int main()
{
[Code].....
View 14 Replies
View Related
Sep 20, 2014
how to input with space what i mean is like this, for example:
Product Code: 0000001
Product Name: Mobile Phone
if the users input is like that in the (Product Name)it will skip the next input that is the
Price: Quantity: 100
Manufacturer: Alcatel
something like that...
Heres the code
Code:
if(b<=10)
{/*Start for IF Statement*/
for(a=1;a<=b;a++)
{/*Start of FOR loop*/
printf("
");
printf("Product Code : ");
scanf("%s", &prod_code);
}
[code]....
View 14 Replies
View Related
Dec 2, 2014
I used pointer(or is it not?) to make it one part only alphabets and the other one digits. The coding, calculate_charges.c and the open file, customer.txt are attached at the end of the post.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 3
void trimback(char input[], int strnameindex);
void trimfrnt(char input[], int strnameindex);
}
[code]....
View 3 Replies
View Related
Mar 16, 2013
Can we use using declaration within name space scope? is it safe to use it?
I think we can use using declaration for class scope and function scope only.
View 9 Replies
View Related
Sep 24, 2014
I want the user to be able to enter a command then a character, like for example: push r....I want the command to be stored in the array command, and the character to be stored in the variable c.
Now I wonder what the best way to get rid of the space is, using scanf or getchar (see below for code, only thing that I changed between the 2 versions is the statement before the comment "get rid of space")? Or maybe it doesnt matter?
Code:
include <stdio.h>
#define MAX 200
void push(char c); // Puts a new element last in queue
char pop(void); // Gets the first element in queue
static char s[MAX];
}
[code]....
View 6 Replies
View Related
Jan 15, 2013
I am allocating space only for two characters but it fits all of them, if you run this it will print the whole string literal "hello my friend". How is that possible?
I am using gcc 4.6.3., I know about strncpy().
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char* str = new char[2];
strcpy(str, "hello my friend");
cout << str << endl;
return 0;
}
View 4 Replies
View Related
Sep 26, 2013
how can I read some strings that contains spaces and put them in a vector of strings, using the push_back() function?
I have a collection of functions, for example: [multiply_by_forty two, add_by_five]. All I want to do is to store the strings like: multiply_by, add_by in a vector of strings, and the arguments:forty two, five etc in another vector of strings, but with spaces. The function convert() converts written numbers to numbers (for ex the output of covert("forty two")is 42;)
Here is the code:
public:void split() {
vector < string > s1;
vector < string > s2;
[Code].....
View 1 Replies
View Related
Oct 12, 2013
In my code I have a template class.
In the contructor, I create a new array arrray.
A pointer to this array is saved as an attribute.
The purpose of this attribute is that I should be able to call the array from within another function be typing *p or *(p+1) etc.
The only thing I forgot is that the array doesn't exist anymore when the constructor is done, right?
This means the space is no longer reserved for this array and the space is used for other things.
Is there a way to keep the array space reserved (even when the array does not exist anymore at the end of the function)?
View 1 Replies
View Related
Jun 8, 2013
I was coding a program that can count space, new_line, and other characters in a paragraph, but it doesn't work.
#include <stdio.h>
int main(void)
{
[Code].....
View 1 Replies
View Related