Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,366 members, 7,808,277 topics. Date: Thursday, 25 April 2024 at 09:44 AM

Learning Java Algorithms:merge Equal And Unequal Arrays. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Learning Java Algorithms:merge Equal And Unequal Arrays. (1871 Views)

I Need To Learn Algorithms / Genetic Algorithms And Neural Networks / Learning Java Without Tears (2) (3) (4)

(1) (Reply) (Go Down)

Learning Java Algorithms:merge Equal And Unequal Arrays. by naijaswag1: 7:36pm On Jul 22, 2011
I have been reading algorithms lately and I have decided to solve all the programming projects in the book.This particular one was solve in about 60 hours.I had headache.I didn't know everything I did but I got it working to give me results.I added the merge method to the example array given by Robert Lafore in his algorithms book.the merge method merges equal and unequal randomly generated arrays sorted into ordered scheme before merging.I will paste the merge code.You could figure out the ordering of the arrays out by yourself,the ordering code and the empty arrays exception inherited from exception class to report malicious users.add the method in an array ordering class that has the sort method or as a static method.develop a driver class to test.I ran a test on 500 arrays.


public int[] merge(int[] a,int[] b) throws EmptyArraysException{
//initialize local variables
int[] tr = null;
int i=0,j=0,h=0;


//handle malicious users sending using empty arrays
if(a.length==0 && b.length==0){
throw new EmptyArraysException("We dont do this kind of things,Arrays are empty"wink;

//most cases-two non-empty arrays
}else if(a.length!=0 && b.length!=0){
tr = new int[a.length+b.length];
//the main algorithm implemented with a do-while loop
do{
if(i > a.length-1){
break;
}
if(j > b.length-1){
break;
}

while(a[i] < b[j]){
tr[h] = a[i];
i++;h++;
if(i > a.length-1){
break;
}
if(j>b.length-1)
break;
continue;
}
if(i > a.length-1){
break;
}
if(j>b.length-1){
break;
}
while(b[j] < a[i]){
tr[h] = b[j];
j++;h++;
if(i > a.length-1){
break;
}
if(j>b.length-1){
break;
}
continue;
}
if(i > a.length-1){
break;
}
if(j>b.length-1){
break;
}
while(a[i] == b[j]){
tr[h] = b[j];
h++;j++;
tr[h] = a[i];
i++;h++;
if(i > a.length-1){
break;
}
if(j>b.length-1){
break;
}
continue;
}
if(i > a.length-1){
break;
}
if(j>b.length-1){
break;
}

}while(i <= a.length-1);

//if first array a is empty and second b is not,copy b to tr and return
}else if(a.length==0 && b.length!=0){
tr = new int[b.length];
for(int g=0;g<b.length;g++)
tr[g] = b[g];
//if first is not empty and second b is empty,copy a to tr and return
}else if(a.length!=0 && b.length==0){
tr = new int[b.length];
for(int g=0;g<a.length;g++)
tr[g] = a[g];
}
//check if any of the arrays has members remaining,copy to to the remaining spaces in tr and return
if(i<a.length){
h = --h;
for(int p=--i;p<a.length;p++){
tr[h] = a[p];
h++;
}
}else if(j<b.length){
h = --h;
for(int p=--j;p<b.length;p++){
tr[h] = b[p];
h++;
}
}


return tr;
}

This gave me headache for two days.You can do better!
Re: Learning Java Algorithms:merge Equal And Unequal Arrays. by Nobody: 7:54pm On Jul 22, 2011
Re: Learning Java Algorithms:merge Equal And Unequal Arrays. by naijaswag1: 10:25pm On Jul 22, 2011
Number 1

1 1 3 6 7 8 9 9 9 10
1 2 3 3 3 5 5 5 5 8 9 10 10 10 10
1 1 1 2 3 3 3 3 5 5 5 5 6 7 8 8 9 9 9 9 10 10 10 10 10
Number 2

1 1 2 2 2 3 7 7 10 10
1 2 3 4 5 5 6 7 8 9 9 9 10 10 10
1 1 1 2 2 2 2 3 3 4 5 5 6 7 7 7 8 9 9 9 10 10 10 10 10
Number 3

3 4 5 5 7 7 8 8 10 10
1 1 1 2 2 3 3 3 5 5 8 8 8 8 8
1 1 1 2 2 3 3 3 3 4 5 5 5 5 7 7 8 8 8 8 8 8 8 10 10
Number 4

1 3 5 5 6 7 8 10 10 10
1 1 2 3 3 4 5 6 6 6 6 8 9 9 10
1 1 1 2 3 3 3 4 5 5 5 6 6 6 6 6 7 8 8 9 9 10 10 10 10
Number 5

1 1 4 4 5 5 6 7 7 7
1 2 3 4 5 5 6 6 6 7 8 8 9 9 10
1 1 1 2 3 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9 10

Some sample output on array of size 10 and 15 randomly generated integers.the last of each set is the merged array.what i posted did the merging.you could try it out,generate an array randomly,order it and merge it.try to shorten what i did.
Re: Learning Java Algorithms:merge Equal And Unequal Arrays. by Nobody: 10:51pm On Jul 22, 2011
Re: Learning Java Algorithms:merge Equal And Unequal Arrays. by naijaswag1: 4:02am On Jul 23, 2011
sort manually.

(1) (Reply)

Crystal Report In Vb.net / Can I Learn Python On My Own? Even With No Previous Knowledge In Programming / Creating A New Programming Language

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 22
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.