algorithm - Radix/LSD sort queue c++ -


i wrote radix sort using queue myself, because use theoretically during class , appeared interesting me . got familiar algorith , read this article radix sort , followed instructions ylc

i tested using pseudo random numbers , looks doing job - array sorted @ end . have doubts proper memory management. exacly removing form queue , inserting array.

there first question: are there leaks of memory ?


i tought complexity of algorithm. read this artticle efficiency.

n - size of array
w - number of positions in our max number
c1 = comparison in max function - done n times
c2 = insertion @ queue - done n*w times
c3 = remove queue - done n*w times
c4 = insertion @ array - done n*w times

t(n) = nc1 + nw*(c2+c3+c4) = o(w*n)

memory complexity t(n) = n * sizeof(node)

secound question: is reasoning logically?


radix-sort

#include <iostream> #include <cstdlib> #include <ctime> using namespace std;  struct list {     int key;     list *next; }; //pointers structure queue list* ptr [10], *head[10];  //function returns maximum of table int maxi (int *t, int n) {     int max= t[0];     for(int = 1; i<n; i++)     {         if (t[i]>max)         max = t[i];     }     return max; } //sort void radx(int *t,int  n ) {     list * tmp, *temp;  //the queue management     int exp = 10;                            int k, j = 0 , l=1;     int max = maxi(t,n);               for(int i=0; i=max*10/exp; i++)     {        //insertion queue         for(int =0 ; i<n; i++)         {             k = ((t[i]%exp)/l); //digit of our position              if(head[k] == null)             {                 head[k] = new list;                 head[k]->key = t[i];                 head[k]->next = null;             }             else             {                 tmp = head[k];                 tmp->next;                 while (tmp->next)                 {                     tmp = tmp->next;                 }                 ptr[k] = new list;                 ptr[k] -> key = t[i];                 tmp->next = ptr[k];                 ptr[k]->next = null;             }         }         //remove , writeback @ table         for(int i= 0; i<10; i++)         {             tmp = head[i];             while(tmp)             {                    t[j] = tmp->key;                 temp = tmp;                 tmp = tmp->next;                 delete temp;                 j++;             }             head[i] = null;         }              exp *= 10;  //next position of number             l   *= 10;             j    = 0;     }   } void random (int n, int a, int b, int *t) {     (int i=0; i<n; i++)     {         t[i] =  rand()% (b-a+1)+a;       } } int main() {        srand(time(0));     int n, a, b;     cout<<"how many elements: ";     cin>>n;     int *t = new int [n];     cout<<endl<<"range to: ";     cin>>a>>b;      random(n, a, b, t);      radx(t, n);      for(int i=0; i<n; i++)     cout<<t[i]<<" ";       delete []t; return 0; } 


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -