Here's a sample implementation of Radix sort, which internally implements count sort to store intermediate results. It sorts the data in O(n), yeah a linear sorting method. Amortized values will be O(kn) where k is the average length of typename. Sorting uses LSD -> MSB movement;
Also sample implementation is in C++ and works only for unsigned int. It can be typcased to work for int, long, float and even string.
http://code.google.com/p/samcoder/source/browse/trunk/codebase/src/RadixSort.h
http://code.google.com/p/samcoder/source/browse/trunk/codebase/test/RadixSortLSD.cpp
Radix Sort wiki -> http://en.wikipedia.org/wiki/Radix_sort
Happy Coding!
Showing posts with label CODE. Show all posts
Showing posts with label CODE. Show all posts
Thursday, July 22, 2010
Radix Sort
Labels:
Algorithm,
c++,
CODE,
cormen,
cormencat,
liner sorting,
O(n),
radix sort,
sorting
Tuesday, July 20, 2010
Listed Algorithms
Well in continuation of my effort to understand Algorithm. Here's a simple approach.
I gonna dump all my new codes in this opensource project and anyone is free to browse/use the source code. Well mostly they are copy cat from Introduction to Algorithm. But many a times it will vary. Most of the code is tested but i cant gaurentee accuracy. :)
http://code.google.com/p/samcoder/
BTW: QuickSort is checked.
http://code.google.com/p/samcoder/source/browse/trunk/codebase/src/QuickSort.h
EnjoY!
I gonna dump all my new codes in this opensource project and anyone is free to browse/use the source code. Well mostly they are copy cat from Introduction to Algorithm. But many a times it will vary. Most of the code is tested but i cant gaurentee accuracy. :)
http://code.google.com/p/samcoder/
BTW: QuickSort is checked.
http://code.google.com/p/samcoder/source/browse/trunk/codebase/src/QuickSort.h
EnjoY!
Labels:
Algorithm,
c++,
CODE,
cormen,
cormencat,
Data Structure,
google project,
Programming,
quicksort,
samcode
Thursday, July 15, 2010
Linked List (C++)
Here's a trivial linked list that i developed few hours back, use it for pedantic purpose. Obvious to say i don't bother what you do with the code.
#include <iostream>
namespace stdds
{
template <typename T>
class Node {
public:
T mData;
Node<T> *mNext;
Node(T pData):mData(pData),mNext(NULL){}
Node(T pData,Node<T>* pNextNode):mData(pData),mNext(pNextNode){}
};
template <typename T>
class LinkedList{
public:
Node<T> *mHead;
LinkedList(){mHead=NULL;}
bool InsertAt(T pData,int pSequence) {
}
bool PushBack(T pData) {
Node<T> *tTempNode;
if(mHead==NULL) {
tTempNode = new Node<T>(pData);
mHead = tTempNode;
return true;
} else {
tTempNode = mHead;
while(tTempNode->mNext!=NULL)
tTempNode = tTempNode->mNext;
tTempNode->mNext = new Node<T>(pData);
return true;
}
}
bool PopFront() {
if(mHead!=NULL) {
Node<T>* tTemp = mHead->mNext;
delete(mHead);
mHead=tTemp;
}
}
bool Insert(T pData,int pPosition) {
Node<T> *tPrevious = NULL;
Node<T> *tCurrent = mHead;
if(pPosition) {
int iCount=0;
while(tCurrent!=NULL) {
if(pPosition==iCount) {
tPrevious->mNext=new Node<T>(pData,tCurrent);
break;
}
tPrevious=tCurrent;
tCurrent=tCurrent->mNext;
iCount++;
}
} else {
mHead = new Node<T>(pData);
mHead->mNext=tCurrent;
}
}
int Find(T pData) {
int iCount=0;
Node<T>* tTemp=mHead;
while(tTemp) {
if(tTemp->mData==pData)
return iCount;
tTemp=tTemp->mNext;
iCount++;
}
return -1;
}
void RemoveAt(int pPos) {
int iCount=0;
Node<T>* tCurr=mHead;
Node<T>* tPrev=NULL;
if(pPos) {
while(tCurr!=NULL) {
if(iCount==pPos) {
tPrev->mNext=tCurr->mNext;
delete(tCurr);
break;
}
tPrev=tCurr;
tCurr=tCurr->mNext;
iCount++;
}
} else {
mHead=tCurr->mNext;
delete(tCurr);
}
}
void Remove(T pData) {
RemoveAt(Find(pData));
}
bool Print() {
if(mHead!=NULL) {
Node<T> *tTempNode = mHead;
while(tTempNode!=NULL) {
std::cout<<tTempNode<<" "<<tTempNode->mNext<<" ["<<tTempNode->mData<<"]"<<std::endl;
tTempNode=tTempNode->mNext;
}
}
}
};
}
#include <iostream>
namespace stdds
{
template <typename T>
class Node {
public:
T mData;
Node<T> *mNext;
Node(T pData):mData(pData),mNext(NULL){}
Node(T pData,Node<T>* pNextNode):mData(pData),mNext(pNextNode){}
};
template <typename T>
class LinkedList{
public:
Node<T> *mHead;
LinkedList(){mHead=NULL;}
bool InsertAt(T pData,int pSequence) {
}
bool PushBack(T pData) {
Node<T> *tTempNode;
if(mHead==NULL) {
tTempNode = new Node<T>(pData);
mHead = tTempNode;
return true;
} else {
tTempNode = mHead;
while(tTempNode->mNext!=NULL)
tTempNode = tTempNode->mNext;
tTempNode->mNext = new Node<T>(pData);
return true;
}
}
bool PopFront() {
if(mHead!=NULL) {
Node<T>* tTemp = mHead->mNext;
delete(mHead);
mHead=tTemp;
}
}
bool Insert(T pData,int pPosition) {
Node<T> *tPrevious = NULL;
Node<T> *tCurrent = mHead;
if(pPosition) {
int iCount=0;
while(tCurrent!=NULL) {
if(pPosition==iCount) {
tPrevious->mNext=new Node<T>(pData,tCurrent);
break;
}
tPrevious=tCurrent;
tCurrent=tCurrent->mNext;
iCount++;
}
} else {
mHead = new Node<T>(pData);
mHead->mNext=tCurrent;
}
}
int Find(T pData) {
int iCount=0;
Node<T>* tTemp=mHead;
while(tTemp) {
if(tTemp->mData==pData)
return iCount;
tTemp=tTemp->mNext;
iCount++;
}
return -1;
}
void RemoveAt(int pPos) {
int iCount=0;
Node<T>* tCurr=mHead;
Node<T>* tPrev=NULL;
if(pPos) {
while(tCurr!=NULL) {
if(iCount==pPos) {
tPrev->mNext=tCurr->mNext;
delete(tCurr);
break;
}
tPrev=tCurr;
tCurr=tCurr->mNext;
iCount++;
}
} else {
mHead=tCurr->mNext;
delete(tCurr);
}
}
void Remove(T pData) {
RemoveAt(Find(pData));
}
bool Print() {
if(mHead!=NULL) {
Node<T> *tTempNode = mHead;
while(tTempNode!=NULL) {
std::cout<<tTempNode<<" "<<tTempNode->mNext<<" ["<<tTempNode->mData<<"]"<<std::endl;
tTempNode=tTempNode->mNext;
}
}
}
};
}
Labels:
Algorithm,
C,
c++,
CODE,
Data Structure,
Dynamic List,
Linked List,
List,
LL,
Programming,
STL
Friday, April 18, 2008
getCodeReviewSuccessRate()
Well one of the most frustrating fact for a Software Developer is that in backdrop lies a cruel tester. No offences attached, even when it's been told ..told and again told that testing is an required phase...and bugs detected are nothing personal...i still dont feel COMFORTABLE with this concept of testing. Well howz its not personal man ?? I wrote the whole code, so more bugs means lesser reliability index for me. So at core i would hate a tester. Esp if the testing is done in semi-understood state, where all well thoought concept are questioned/categorized as bugs. Well i guess, this a non-evasive situation where all you can do is pray (or bribe).
From the eyes of an tester (beholder), its his duty to cleanse the code. Form my p.o.v he is the "devil crawling outside my door".
Well it's me...i was born and brought up as Software Developer...but what about the person who is typecasted to an developer ?? Well i guess he will be elated everytime he finds a bug in his code. Awwh man awesome, my code has a bug. :D...
Comon man u no tester now ... dude u need to fix it well now, so dont be happy.
Below is one of the screen/code walkthrough session stuff ( ie: WTF/min )

As truly said "Coding is routine, buggs are the real twisters".
From the eyes of an tester (beholder), its his duty to cleanse the code. Form my p.o.v he is the "devil crawling outside my door".
NativeTester *objOldTester = new NativeTester("AIRHEAD");
NewAgeDev *objOldTesterToNewDev = reinterpret_cast(objOldTester);
if(!objOldTesterToNewDev->canICode())
{
std::cout<<"REMEMBER I WAS A AIRHAED ?? HOW CAN U EXPECT ME TO CODE";
}
return somebug;
Well it's me...i was born and brought up as Software Developer...but what about the person who is typecasted to an developer ?? Well i guess he will be elated everytime he finds a bug in his code. Awwh man awesome, my code has a bug. :D...
Comon man u no tester now ... dude u need to fix it well now, so dont be happy.
Below is one of the screen/code walkthrough session stuff ( ie: WTF/min )

As truly said "Coding is routine, buggs are the real twisters".
Subscribe to:
Posts (Atom)