“Arrays , Arrays and Arrays…!”

Alwaz Qazi
3 min readOct 31, 2019

--

This blog is all about what we’ve learned about Arrays so far.

Arrays : “ A collection of similar data, stored in contiguous memory locations is called as Array ”.

This was the basic definition of Array according to me, but there’s much more that we learned in our last three classes of Data Structure and Algorithm.
We started with the basics of Arrays , it’s initialization , declaration and definition.

Syntax:
data_type[] arr_name=new data_type[size]; //declaration + definition
int[] arr=new int[5]; //this can store up to 5 elements.

Elements in arrays can be directly declared and initialized.

Syntax:
int[] arr={1,2,3,4,5};

this was the example of one dimensional array but there are also multidimensional arrays for example : 2D array, 3D array etc.

Syntax for multidimensional array:int[][] arr=new int[2][2]; //2D array.
int[][][] arr= new int[3][3][3]; //3D array.

then we did some example codes i.e addition of 2D array etc.

This was all about the basics of array that we did in intro class for arrays , then we proceed to learn new things which were methods of Array class.

here’s the link of methods of Arrays in java documentation

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html.

then we did some examples using these methods.

import java.util.*;
class Three
{ public static void main(String arg[])
{
//array of double values
double a[]={12.3, 3.47, 5.6, 4.8, 9.0};
//SORT METHOD : SORTS THE ARRAY IN EITHER ASCENDING OR DESCENDING ORDER
Arrays.sort(a);
//toString METHOD : CONVERTS THE ARRAY INTO STRING and that is later to be displayed
System.out.print(Arrays.toString(a));
System.out.println();
//binarySearch method : searches for the specific element from a SORTED ARRAY and returns INDEX System.out.println(Arrays.binarySearch(a,3.47));
//at 0 index after the array is sorted //for the element that doesn't exist in the array System.out.println(Arrays.binarySearch(a,2));
//at -ve index after the array is sorted because the element is not present
//EQUALS METHOD double b[]={5.5, 3.47, 3.6, 0.8, 4.0}; System.out.println(Arrays.equals(a,b)); //as a and b are unequal so FALSE WOULD BE PRINTED
//FILL METHOD double d[]=new double[5];
Arrays.fill(d,4.4);
System.out.print(Arrays.toString(d));
System.out.println();
//copyOf METHOD
double copy[]=new double[5];
copy=Arrays.copyOf(a,4);
System.out.print(Arrays.toString(copy));
System.out.println();
//opyOfRange METHOD double
copyor[]=new double[5];
copyor=Arrays.copyOfRange(a,3,5); System.out.print(Arrays.toString(copyor));
System.out.println();
}
}

after learning about methods of Array class we proceed further with
Searches”. we learned about two types of searching algorithms.
1: Linear Search O(n).
2: Binary Search -log(n).

Linear Search: A very basic and simple method for searching elements from an array. In this algorithm we search our desired element by traversing elements of array from the beginning to end. This algorithm is not considered efficient enough because it increases the “Time complexity” of a program , because if we want to search an element which is present at the end then this algorithm will check each and every element in array in order to find the desired element , and this algorithm requires elements of array to be sorted from ascending to descending order. Due to all of these reasons this algorithm for searching elements is not that efficient.

//Algorithm of linear searchingStep 1: set i to 1.
Step 2: if i is less than n then go to step 7.
Step 3: if a[i]==x then go to step 6.
Step 4: set i to i+1
Step 5: go to step 2
Step 6: print element x found at index i then go step 8
Step 7: print element not found
Step 8: EXIT.
//Pseudocode
int[] arr=new int[5];
arr={1,2,3,4,5};
S.o.p("Element to be search.");
int x=sc.nextInt();
for(int i=0;i<arr.length;i++)
{
if(arr[i]==x)
{
return i;
}
else
s.o.p("Element not found.");
}

Binary Search: This is the fastest and most efficient searching algorithm. As can be seen by the name (Bi means two) so, it works on the principle of dividing the array into two and continue dividing it till the searched element is found or if not found than continue dividing till the result becomes 0. This method is efficient because it reduces the “Time complexity” of the program.

//Algorithm for binary search
INPUT: array(sorted) and target x
OUTPUT: index;
STEP 1: let p=0 and q=n-1;
STEP 2: Repeat step 2 and 5 while p<=q;
STEP 3: let i=(p+q)/2;
STEP 4: if ai=x , return i.
STEP 5: if ai<x , let p=i+1 otherwise let q=i-1;
STEP 6: return -p-1;
//Pseudocodeclass SearchArray
{
public static int(int[] arr, int x)
{
int i;p=0;q=arr.length-1;
while(p<=q)
{ i=(p+q)/2;
if(arr[i]==x)
return i;
if(arr[i]<x)
return p=i+1;
else
return q=i-1;
return =p-1;}}}

This was my second blog on what we learned in these three labs so far.

--

--

Alwaz Qazi
Alwaz Qazi

No responses yet