Saturday, August 8, 2009
Euphoria
close, turn-off, shut-down the box
wanna come-out ,speak ,shout like a fox
Gripped by loneliness, hebetude, claustrophobia?
No, dont hide, come in the light, Euphoria!!!
wanna come-out ,speak ,shout like a fox
Gripped by loneliness, hebetude, claustrophobia?
No, dont hide, come in the light, Euphoria!!!
Friday, August 7, 2009
source code of quick-sort
Two days after starting to write the code for quick sort, from scratch, I was finally able to make it error free. The algorithm is elegant but error prone and to make matters worse (from the point of view of program readability) recursion is an integral part of the code.
I searched heavily for the code but couldn't find any of them written in C++, and clean enough.
So I decided to blog it down, for my own future references and above all, for the whole coding community.
#include "iostream"
#define max 9
using namespace std;
void display(int* a)
{
for(int i=0; i< max; i++)
printf("%d",a[i]);
printf("\n");
}
void swap(int& x,int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int partition(int* a,int beg,int end)
{
int left = beg, right=end, piv = *(a+beg);
while(left {
while((a[left]<=piv)&&(left left++;
while((a[right]>=piv)&&(right>beg))
right--;
if(left swap(a[left],a[right]);
}
a[beg] = a[right];
a[right] = piv;
return(right);
}
void quick(int* a,int beg,int end)
{
if (beg>=end) return;
int pos = partition(a,beg,end);
cout< quick(a,beg,pos-1);
quick(a,pos+1,end);
}
int main()
{
int arr[] = {1,5,2,7,3,4,6,0,9};
display(arr);
quick(arr,0,max-1);
printf("After Quick Sorting:");
display(arr);
system ("pause");
return 0;
}
The program was written in Dev C++ version 4.9.9.2
Compiler used- g++
I searched heavily for the code but couldn't find any of them written in C++, and clean enough.
So I decided to blog it down, for my own future references and above all, for the whole coding community.
#include "iostream"
#define max 9
using namespace std;
void display(int* a)
{
for(int i=0; i< max; i++)
printf("%d",a[i]);
printf("\n");
}
void swap(int& x,int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int partition(int* a,int beg,int end)
{
int left = beg, right=end, piv = *(a+beg);
while(left
while((a[left]<=piv)&&(left
while((a[right]>=piv)&&(right>beg))
right--;
if(left
}
a[beg] = a[right];
a[right] = piv;
return(right);
}
void quick(int* a,int beg,int end)
{
if (beg>=end) return;
int pos = partition(a,beg,end);
cout<
quick(a,pos+1,end);
}
int main()
{
int arr[] = {1,5,2,7,3,4,6,0,9};
display(arr);
quick(arr,0,max-1);
printf("After Quick Sorting:");
display(arr);
system ("pause");
return 0;
}
The program was written in Dev C++ version 4.9.9.2
Compiler used- g++
Subscribe to:
Comments (Atom)