Week 3-Multidimensional Array Concepts, Searching Sorting
Week 3-Multidimensional Array Concepts, Searching Sorting
Week 3-Multidimensional Array Concepts, Searching Sorting
CSCI431/CENG523
Prepared By
Ms.Fouzia Salma
SEARCHING & SORTING
LAB PROGRAM-WEEK-1
S.No Program
1 Write a program in Java which accept ten numbers in an array and display them from
first to last.
2 Write a program in Java which accept five numbers in an array and display them in
reverse order (last to first)
3 Write a program in java that prompts for an integer and then prints out all multiples of
7 numbers up to that number.
4 Write a program in java which store 10 numbers and find the sum of odd and even
numbers.
5 Write a program in Java which accept five numbers in an array and copy them into
another array in reverse order.
Searching Arrays
50 is found at index: 3
Binary search
• Binary search is used to search a key element from multiple
elements. Binary search is faster than linear search.
• In case of binary search, array elements must be in ascending
order. If you have unsorted array, you can sort the array
using Arrays.sort(arr) method.
class BinarySearch{ else
public static void binarySearch(int arr[], int first, int last, int key) {
{ last = mid - 1;
int mid = (first + last)/2; }
while( first <= last ) mid = (first + last)/2;
{ }
if ( arr[mid] < key ) if ( first > last )
{ {
first = mid + 1; System.out.println("Element is not found!");
} } }
else if ( arr[mid] == key ) public static void main(String args[])
{ {
System.out.println("Element is found at index: " + mid); int arr[] = {10,20,30,40,50};
break; int key = 30;
} int last=arr.length-1;
binarySearch(arr,0,last,key);
} }
Sorting Arrays
// Alternative syntax
dataType refVar[][] = new dataType[10][10];
Two-Dimensional Arrays
• A two-dimensional array is an array of arrays.
• It can be thought of as having rows and columns.
row 0
row 1
row 2
row 3
Two-Dimensional Arrays
• Declaring a two-dimensional array requires two sets of brackets and two size
declarators
• The first one is for the number of rows
• The second one is for the number of columns.
two dimensional array rows columns
double[][] scores = new double[3][4];
• The two sets of brackets in the data type indicate that the scores variable will
reference a two-dimensional array.
• Notice that each size declarator is enclosed in its own set of brackets.
Accessing Two-Dimensional Array
Elements
• When processing the data in a two-dimensional array, each element has
two subscripts:
• one for its row and
• another for its column.
Accessing Two-Dimensional Array
Elements
The scores variable
holds the address of a
2D array of doubles.
column 0 column 1 column 2 column 3
Address
row 0 scores[0][0] scores[0][1] scores[0][2] scores[0][3]
double[][] x;
Declaring, Creating, and Initializing Using
Shorthand Notations
You can also use an array initializer to declare, create
and initialize a two-dimensional array. For example,
array[4].length
ArrayIndexOutOfBoundsException
Declare Two Arrays
6000 27950 67700 141250 307050 Single filer
12000 46700 112850 171950 307050 Married jointly
6000 23350 56425 85975 153525 Married separately
10000 37450 96745 156600 307050 Head of household