Saturday, August 7, 2021

How to print array in java

To print any array in java program we need to use java.util.Arrays.toString(obj) method, This is availabe since java 5.

example :

public class PrintArray {

public static void main(String args[]) {

int[] arr = {23,4,5,6,71,5,4};

System.out.println(Arrays.toString(arr));

}

}

In case if  an array contains another array then in this case we need String representation of deep content like this:

example : Arrays.deepToString(object).

public class PrintArray {

public static void main(String args[]) {

String[] arr1= {"one", "two", "three"};

String[] arr2= {"four", "five", "six"};

String [][] arr = {arr1 , arr2};

System.out.println(Arrays.deepToString(arr));

}

}

Labels: , , ,

Friday, August 6, 2021

Java selection sort

Sorting an array using selection sort.
We first need to find minimum element in a array and swap with the first element.
Next step to pick the minimum element in the sub-array as first element will be the minimum hence the subarray will not include the first element.
Repeat the same process with this subarray as we did with the main array.

Suppose we have following array : { 23, 4, 1, 6, 7, 3, 4, 5 }

1.  Find minimum : 1 , swap with first element. array will be : { 1, 4, 23, 6, 7, 3, 4, 5 }
2.  First element is sorted hence take subarray {  4, 23, 6, 7, 3, 4, 5 } from main array { 1, 4, 23, 6, 7, 3,        4,  5 } without first element 
     repeat the above process : result will be { 1, 3, 23, 6, 7, 4, 4, 5}.
In this manner you will repeat the above process until all element sorted.
Worst case time complexity O(n^2)

Java code for the same is as follows :

public class SelectSort {
public static void main(String args[]) {
int[] arr = { 1, 2, 3, -4, 5, 6, -7, 8 ,0};
int min = 0;
int index = 0;
int temp = 0;
for (int j = 0; j <= arr.length - 1; j++) {
min = arr[j];
boolean swap = false;
for (int i = j+1; i <= arr.length - 1; i++) {
if (min > arr[i]) {
min = arr[i];
index = i;
swap = true;
}
}
if(swap) {
temp = arr[j];
arr[j] = min;
arr[index] = temp;
}
}
for (int k : arr) {
System.out.println(k);
}
}
}

Labels: , , , ,

Sunday, August 1, 2021

Java Bubble sort

In bubble sort we pick first element and compare with each elements. If right element is bigger with first element, we will swap the elements. Similarly we will pick next element and compare with each elements and swap if found and bigger value or right hand side.

So in the first iteration first element will be in sorted order. As you can see in below example.

unsorted array : {23, 3, 4, 1, 6, 34}

1- iteration  {1, 23, 4, 3, 6, 34}

2- iteration  {1, 3, 23, 4, 6, 34}

3 -iteration  {1, 3, 4, 23, 6, 34}

4- iteration  {1, 3, 4, 6, 23, 34}

5- iteration  {1, 3, 4, 6, 23, 34}

Worst case time complexity is O(n^2), it is simple but inefficient way of sorting.


public class BubbleSort {

public static void main(String[] args) {

int[] data = {23,3,4,1,6,34};

int length  = data.length;

int temp = 0;

for(int i=0;i<length-1;i++) {

for(int j=i+1;j<length-1;j++) {

if(data[i]>data[j]) {

temp = data[j];

data[j]=data[i];

data[i]=temp;

}

}

}

for (int k : data) {

System.out.println(k);

}

}

}

Labels: , ,