Saturday, November 22, 2014

How to convert number to char array

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
char buf[20];

int a = 10;

sprintf(buf, "%d", a);

for(int I=0;I<log10(a)+1; I++){
cout<<buf[I]<<" ";
}
return 0;
}

Friday, November 14, 2014

Heap #4: HeapSort Descending Order


#include<cstdio>
#include<iostream>
using namespace std;

int Parent(int index)
{
return index>>1; // index/2
}

int LeftChild(int index)
{
return index<<1; // 2 * index
}

int RightChild(int index)
{
return (index<<1) + 1; // (2 * index) + 1
}

void Min_Heapify(int *x, int index, int heapsize)
{
//cout<<"Visit-->"<<index<<endl;
int left = LeftChild(index);
int right = RightChild(index);
int smallest = index;

if(left<=heapsize && x[left]<x[smallest])
{
smallest = left;
}

if(right<=heapsize && x[right]<x[smallest])
{
smallest = right;
}

if(smallest != index)
{
swap(x[smallest], x[index]);
Min_Heapify(x, smallest, heapsize);
}
}

void Build_Min_Heap(int *x, int heapsize)
{
for(int I=(heapsize/2); I>0; I--)
{
Min_Heapify(x, I, heapsize);
}
}

void HeapSort(int *x, int heapsize)
{
Build_Min_Heap(x, heapsize);

for(int I = heapsize; I>1; I-- )
{
swap(x[1], x[I]);
heapsize--;
Min_Heapify(x, 1, heapsize);
}
}


void printArray(int *x, int arraySize)
{
for(int I=0; I<arraySize; I++)
{
cout<<I<<" "<<x[I]<<endl;
}

cout<<endl;
}


int main()
{
//Heap start from index 1
int a[] = {-1,8,3,6,5,4,7,2};

int arraySize = sizeof(a)/sizeof(*a);

printArray(a, arraySize);
       
        //Descending Order
HeapSort(a, arraySize-1);

printArray(a, arraySize);


return 0;
}

Heap #3: HeapSort Ascending Order


#include<cstdio>
#include<iostream>
using namespace std;

int Parent(int index)
{
return index>>1; // index/2
}

int LeftChild(int index)
{
return index<<1; // 2 * index
}

int RightChild(int index)
{
return (index<<1) + 1; // (2 * index) + 1
}

void Max_Heapify(int *x, int index, int heapsize)
{
//cout<<"Visit-->"<<index<<endl;
int left = LeftChild(index);
int right = RightChild(index);
int largest = index;

if(left<=heapsize && x[left]>x[largest])
{
largest = left;
}

if(right<=heapsize && x[right]>x[largest])
{
largest = right;
}

if(largest != index)
{
swap(x[largest], x[index]);
Max_Heapify(x, largest, heapsize);
}
}

void Build_Max_Heap(int *x, int heapsize)
{
for(int I=(heapsize/2); I>0; I--)
{
Max_Heapify(x, I, heapsize);
}
}

void HeapSort(int *x, int heapsize)
{
Build_Max_Heap(x, heapsize);

for(int I = heapsize; I>1; I-- )
{
swap(x[1], x[I]);
heapsize--;
Max_Heapify(x, 1, heapsize);
}
}


void printArray(int *x, int arraySize)
{
for(int I=0; I<arraySize; I++)
{
cout<<I<<" "<<x[I]<<endl;
}

cout<<endl;
}


int main()
{
//Heap start from index 1
int a[] = {-1,2,3,9,5,6,7,8};

int arraySize = sizeof(a)/sizeof(*a);

printArray(a, arraySize);

//For Ascending order
HeapSort(a, arraySize-1);

printArray(a, arraySize);


return 0;
}


Heap #2: Build A MIN Heap


#include<cstdio>
#include<iostream>
using namespace std;

int Parent(int index)
{
return index>>1; // index/2
}

int LeftChild(int index)
{
return index<<1; // 2 * index
}

int RightChild(int index)
{
return (index<<1) + 1; // (2 * index) + 1
}

void Min_Heapify(int *x, int index, int heapsize)
{
//cout<<"Visit-->"<<index<<endl;
int left = LeftChild(index);
int right = RightChild(index);
int smallest = index;

if(left<=heapsize && x[left]<x[smallest])
{
smallest = left;
}

if(right<=heapsize && x[right]<x[smallest])
{
smallest = right;
}

if(smallest != index)
{
swap(x[smallest], x[index]);
Min_Heapify(x, smallest, heapsize);
}
}

void Build_Min_Heap(int *x, int heapsize)
{
for(int I=(heapsize/2); I>0; I--)
{
Min_Heapify(x, I, heapsize);
}
}

void printArray(int *x, int arraySize)
{
for(int I=0; I<arraySize; I++)
{
cout<<I<<" "<<x[I]<<endl;
}

cout<<endl;
}


int main()
{
//Heap start from index 1
int a[] = {-1,8,3,6,5,4,7,2};

int arraySize = sizeof(a)/sizeof(*a);

printArray(a, arraySize);

Build_Min_Heap(a, arraySize-1);

printArray(a, arraySize);


return 0;
}

Heap #1: Build A MAX Heap

If you want to understand the code fully please read Introduction to algorithm(CLRS) chapter-6 first.


#include<cstdio>
#include<iostream>
using namespace std;

int Parent(int index)
{
return index>>1; // index/2
}

int LeftChild(int index)
{
return index<<1; // 2 * index
}

int RightChild(int index)
{
return (index<<1) + 1; // (2 * index) + 1
}

void Max_Heapify(int *x, int index, int heapsize)
{
//cout<<"Visit-->"<<index<<endl;
int left = LeftChild(index);
int right = RightChild(index);
int largest = index;

if(left<=heapsize && x[left]>x[largest])
{
largest = left;
}

if(right<=heapsize && x[right]>x[largest])
{
largest = right;
}

if(largest != index)
{
swap(x[largest], x[index]);
Max_Heapify(x, largest, heapsize);
}
}

void Build_Max_Heap(int *x, int heapsize)
{
for(int I=(heapsize/2); I>0; I--)
{
Max_Heapify(x, I, heapsize);
}
}

void printArray(int *x, int arraySize)
{
for(int I=0; I<arraySize; I++)
{
cout<<I<<" "<<x[I]<<endl;
}

cout<<endl;
}


int main()
{
//Heap start from index 1
int a[] = {-1,2,3,4,5,6,7,8};

int arraySize = sizeof(a)/sizeof(*a);

printArray(a, arraySize);

Build_Max_Heap(a, arraySize-1);

//cout<<"--"<<heapsize<<endl;
printArray(a, arraySize);


return 0;
}

Saturday, November 1, 2014

Bangla Blog list (Collected from FB)


ইকরাম মাহমুদ ফাহিমের ব্লগ- https://sites.google.com/site/smilitude/
জোবায়ের হাসানের ব্লগ- http://zobayer.blogspot.com/
  http://zobayer2009.wordpress.com/
শাফায়েতের ব্লগ- http://www.shafaetsplanet.com/planetcoding/
আহমেদ শামসুল আরেফিনের ব্লগ- http://www.acmsolver.org/bangla/
প্রোগক্রিয়া- http://www.progkriya.org/
আরিফুজ্জামান আরিফের ব্লগ- http://isolvedaproblem.blogspot.com/
জানে আলম জান -http://lightoj.com/article_showcategory.php
সাব্বির ইউসুফ সানির ব্লগ- http://one-problem-a-day.blogspot.com/
 http://recurringblunders.blogspot.com/
প্রোগ্রামিং কনসেপ্ট- https://sites.google.com/site/programinggconcept/
আলাভোলার ব্লগ- http://alavolacoder.blogspot.com/
অনিন্দ্য সুন্দর পালের ব্লগ- http://binaryrongo.wordpress.com/
আহমাদ ফাইয়াজের ব্লগ- http://itsfaiyaz.wordpress.com/
বিধানের ব্লগ - http://bidhanr.wordpress.com/
এরর এর ব্লগ - https://sites.google.com/site/erorown/
আই ইউ টি ব্লগ- http://doinik-iut.com/archives/23106
সুবিন ভাইয়ের সাইট- http://cpbook.subeen.com/
ফাইয়াজ বিন হোসেনের ব্লগ- http://faiazerblog.blogspot.com/
ফারসানের ব্লগ- http://potasiyam.com/farsan/
মশিউর এর ব্লগ -http://problem-solving-notes.blogspot.com/
আনা ফারিহার ব্লগ - http://chorui12.blogspot.com/
তাওসিকের ব্লগ- http://tausiq.wordpress.com/
শরীফের স্বপ্নরাজ্য - http://www.techsharif.com/
শিখরের ব্লগ - http://shikhorroy.wordpress.com/
সায়েফের ব্লগ - http://sketchingdream.wordpress.com/
সায়মুমের ব্লগ - http://saimoomsafayet.wordpress.com/
শাকিলের ব্লগ - https://www.facebook.com/shakil.ahmed.980/notes
মাহফুজের ব্লগ - http://www.mahfuzsust.info/
আসিফের হ-য-ব-র-ল - http://www.abuasifkhan.me/

Saturday, October 4, 2014

Hello Testing 1..2..3

Hello everyone,
I am a very happy person. I just want to spread my happiness with you all. That's why i created this blog and testing how to post...:)