Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

January 28, 2020

Sorting two arrays into one array in efficient way

Sorting two sorted square number array to single array

/**
 * inputs :
 * int[] a = { 100, 64, 36, 2 };
int[] b = { 1, 9, 36, 49, 81 };

output :

[1, 2, 9, 36, 36, 49, 64, 81, 100]

 */
package com.codecreeks.solutions;

import java.util.Arrays;

/**
 * @author Athul
 *
 */
public class SortingAlgorithm {

/**
* @param javacode
*/
public static void main(String[] javacode) {

int[] a = { 100, 64, 36, 2 };
int[] b = { 1, 9, 36, 49, 81 };

SortingAlgorithm algo = new SortingAlgorithm();
int[] output = algo.sort(a, b);
System.out.println(Arrays.toString(output));

}

private int[] sort(int[] a, int[] b) {
int bLen = b.length;
int aLen = a.length;
int len = aLen + bLen;
int aPtr = aLen - 1;
int bPtr = 0;

int[] output = new int[len];

for (int i = 0; i < len; i++) {

if (bPtr > (bLen - 1)) {
while (aPtr >= 0) {
output[i] = a[aPtr];
i++;
aPtr--;
}
break;
}

if (aPtr < 0) {
while (bPtr < bLen) {
output[i] = b[bPtr];
i++;
bPtr++;
}
break;
}

if (b[bPtr] < a[aPtr]) {
output[i] = b[bPtr];
bPtr++;
} else {
output[i] = a[aPtr];
aPtr--;
}

}

return output;
}

}



One of the biggest mistakes we all make is thinking we need more time than we actually do.

- Admond Lee Kin Lim


February 23, 2019

How to copy array in Java best example source code

Copy an array into another in java

You can either use any of this following methods only for one dimensional array


  1. Object.clone()
  2. System.arraycopy()
  3. Arrays.copyOf()
  4. Arrays.copyOfRange()
  5. ArrayUtils.clone()  from  apache commons

Java code

/**
 *  Copying an array in Java
 *
 */
package com.Konzerntech.tutorial;

import java.util.Arrays;

/**
 * @author Konzerntechnologies
 *
 */
public class CopingArray {

/**
* @param args
*/
public static void main(String[] args) {

// Source array
int a[] = {8,2,8,1,8,0,8,0,2,9};


/**
* Since array is also an Object
* you can use clone method
* you can copy the whole object
* But partial copying is not possible
*/
int d[] = a.clone();
System.out.println(" using clone method ");
for(int i =0; i< d.length; i++) {
System.out.print(d[i]+"\t");
}


/**
* The best way to copy array
* you can do partial copying
* From a source to destination to a different position
*
* The below code will copy 4 elements from
* starting from first arrays second position to second arrays 3rd position
*/
System.out.println("\n using System.arrayCopy() method ");
int b[] = new int[a.length];
System.arraycopy(a, 2, b, 3, 4);
for(int i =0; i< b.length; i++) {
System.out.print(b[i]+"\t");
}

/**
* If you want to copy array partially or fully
* use array copy method
*/
System.out.println("\n using Arrays.copyOf() method ");
int c[] = Arrays.copyOf(a, a.length);
for(int i =0; i< c.length; i++) {
System.out.print(c[i]+"\t");
}

/**
* If you want to copy array partially or fully
* use array copy method
*/
System.out.println("\n rusing Arrays.copyOfRange() method ");
int e[] = Arrays.copyOfRange(a, 2 ,a.length);
for(int i =0; i< e.length; i++) {
System.out.print(e[i]+"\t");
}

}

}


Output


 using clone method 
8 2 8 1 8 0 8 0 2 9
 using System.arrayCopy() method 
0 0 0 8 1 8 0 0 0 0
 using Arrays.copyOf() method 
8 2 8 1 8 0 8 0 2 9
 rusing Arrays.copyOfRange() method 
8 1 8 0 8 0 2 9


Description

If you are going behind performance dont use clone method, as the clone method works on Object class, there will be internal type casting to datatypes

You can use  ArrayUtils.clone from  apache commons

int arr[] = ArrayUtils.clone(source_array);

All the methods discussed above performs shallow copy so they are suitable for immutable objects and primitive data types.

If you want to copy a mutable object go for deep copy ( Create a new memory for object while copying )


email us at itatcalicut(at)gmail(dot)com

Follow this link to join my WhatsApp group: https://chat.whatsapp.com/HR0H7MG7AeA2PMApmfEeZn

Facebook comments