Archive for the ‘Puisi’ Category

Quick Sort

Posted: 9 June 2008 in Puisi
Tags: ,

#include <iostream.h>
#include <conio.h>

void main()
{
void srt(int[],int,int);
int a[10],count=0,n;
clrscr();
cout<<“Ener 10 values in unsorted order : \n”;
for (n=0;n<10;n++)
{
cout<<“value no.: “<<(n+1)<>a[n];
count++;
}
n=0;
clrscr();
srt(a,n,count-1);
clrscr();
cout<<“\t\tThe Sorted order is : \n”;
for (n=0;n<10;n++)
{
cout<<“\t\tposition : “<<(n+1)<<“\t”<<a[n]<<“\n”;
}
getch();
}
void srt(int k[20],int lb,int ub)
{
int i,j,key,flag=0,temp;
clrscr();
if (lb<ub)
{
i=lb;
j=ub+1;
key=k[i];
while(flag!=1)
{
i++;
while(k[i]key)
{
j–;
}
if (i<j)
{
temp=k[i];
k[i]=k[j];
k[j]=temp;
}
else
{
flag=1;
temp=k[lb];
k[lb]=k[j];
k[j]=temp;
}
}
srt(k,lb,j-1);
srt(k,j+1,ub);
}
}

Bubble Sort

Posted: 9 June 2008 in Puisi
Tags: ,

#include <iostream.h>
#include <conio.h>
#define MAX 10

class bubsort{
int arr[MAX],n;
public:
void getdata();
void showdata();
void sortLogic();
};

void bubsort :: getdata(){
cout<>n;
for(int i=0;i>arr[i];
}

void bubsort :: showdata(){
cout<<“\n–Display–\n”;
for(int i=0;i<n;i++)
cout<<arr[i]<<” “;
}

void bubsort :: sortLogic(){
int temp;
for(int i=0;i<n;i++){
for(int j=0,exchange=0;j arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
exchange++;
cout<<“\n arr[j] = “<<arr[j]<<” arr[j+1] = “<<arr[j+1];
}
}
cout<<endl;
if(exchange==0)
break;
}
}

void main(){
clrscr();
cout<<“\n*****Bubble Sort*****\n”;
bubsort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
getch();
}

Merge Sort

Posted: 9 June 2008 in Puisi
Tags: ,

#include <iostream.h>
#include <conio.h>
#define MAX 10

static int merge_arr[MAX+MAX],sort_arr[MAX+MAX];

class mergesort{
int arr1[MAX],arr2[MAX],n1,n2,n3;
public:
void getdata();
void showdata(int);
void mergeLogic();
void sortLogic();
};

void mergesort :: getdata(){
int i;
cout<<“\n–Data Must be Entered in Sorted Order for each Array–\n\n”;
cout< Array: “;
cin>>n1;
for(i=0;i>arr1[i];
cout< Array: “;
cin>>n2;
for(i=0;i>arr2[i];
n3=n1+n2;
}

void mergesort :: showdata(int select){
int i;
if(select==1){
cout<<“\n\n–Array 1–\n”;
for(i=0;i<n1;i++)
cout<<arr1[i]<<” “;
}
else if(select==2){
cout<<“\n\n–Array 2–\n”;
for(i=0;i<n2;i++)
cout<<arr2[i]<<” “;
}
else if(select==3){
cout<<“\n\n–Sorted Array–\n”;
for(i=0;i<n3;i++)
cout<<sort_arr[i]<<” “;
}
}

void mergesort :: mergeLogic(){
int i,j,c;
for(i=0;i<n1;i++)
merge_arr[i] = arr1[i];
for(j=i,c=0;j<n3;j++,c++)
merge_arr[j] = arr2[c];
}

void mergesort :: sortLogic(){
//before sort call mergeLogic
mergeLogic();

int i,j,first=0,second=n1,third=n3;
i=first;
j=second;
static int c=0;

while(i<second && j<third){
if(merge_arr[i] < merge_arr[j]){
sort_arr[c]=merge_arr[i];
i++;
}
else{
sort_arr[c]=merge_arr[j];
j++;
}
c++;
}

if(i<second){
while(i<second){
sort_arr[c]=merge_arr[i];
i++;
c++;
}
}

if(j<third){
while(j<third){
sort_arr[c]=merge_arr[j];
j++;
c++;
}
}
}
/*

void mergesort :: sortLogic(){
static int i,j,c=0;

while(i<n1 && j<n2){
if(arr1[i] < arr2[j]){
sort_arr[c]=arr1[i];
i++;
}
else{
sort_arr[c]=arr2[j];
j++;
}
c++;
}

if(i<n1){
while(i<n1){
sort_arr[c]=arr1[i];
i++;
c++;
}
}

if(j<n2){
while(j<n2){
sort_arr[c]=arr2[j];
j++;
c++;
}
}

}
*/

void main(){
clrscr();
cout<<“\n*****Merge Sort*****\n”;
mergesort obj;
obj.getdata();
obj.sortLogic();
obj.showdata(1);
obj.showdata(2);
obj.showdata(3);
getch();
}