Data Structure and Algorithms Lecture Notes PDF
Document Details
Uploaded by ModestSerenity
2024
Dr. Yusra Ahmed Salih
Tags
Summary
Arrays, linear search, and binary search are the main topics of the document, with illustrative examples. The 2024-2025 lecture notes provide programming concepts relevant to data structures and algorithms, a crucial topic in computer science.
Full Transcript
Data Structure and Algorithms Lec 2 :Arrays Dr. Yusra Ahmed Salih 2024-2025 Example 1: Some Array Definitions 1 public class ArrayDefs { 2 public static void main(String[] args) { 3 float x[]; 4 x = new float; 5 String[] s= new String;...
Data Structure and Algorithms Lec 2 :Arrays Dr. Yusra Ahmed Salih 2024-2025 Example 1: Some Array Definitions 1 public class ArrayDefs { 2 public static void main(String[] args) { 3 float x[]; 4 x = new float; 5 String[] s= new String; 6 boolean[] isPrime = new boolean; 7 int fib[] = {0, 1, 1, 2, 3, 5, 8, 13};}} Explanation of Example 1 Line 3 declares x[] to be an array of floats but does not allocate any storage for the array. Line 4 defines x[] to have 100 float components. Line 5 declares s[] to be an array of 10 String objects. Note the two different (equivalent) ways to declare an array: The brackets may be a suffix on the type identifier or on the array identifier. Line 5 defines s[] to have 10 String components. Line 6 defines isPrime[] to be an array of 1000 boolean variables. Line 7 defines fib[] to be an array of 8 ints, initializing them to the 8 values listed. So for example, fib has the value 3, and fib has the value 13. Duplicating an Array ✓ An array can be duplicated by invoking the Object.clone() method, as shown in Example 2. The java.util.Arrays Class ✓ Java includes a special “utility” class for processing arrays. The name of this class is Arrays, and it is defined in the java.util package. Example 2: Duplicating an Array public class DuplicatingArrays { public static void main(String[] args) { int[] a = {22, 44, 66, 88}; print(a); int[] b = (int[])a.clone(); // duplicate a[] in b[] print(b); String[] c = {"AB", "CD", "EF"}; print(c); String[] d = (String[])c.clone(); // duplicate c[] in d[] print(d); c = "XYZ"; // change c[], but not d[] print(c); print(d);} public static void print(int[] a) public static void print(String[] a) { { System.out.printf("{%s", a); System.out.printf("{%d", a); for (int i = 1; i < a.length; i++) for (int i = 1; i < a.length; i++) { { System.out.printf(", %d", a[i]); System.out.printf(", %s", a[i]); } } System.out.println("}"); System.out.println("}"); } } }//end of class Example : The Sequential Search public class TestSequentialSearch { public static void main(String[] args){ int[] a = {22, 33, 44, 55, 66, 77, 88, 99}; DuplicatingArrays obj= new DuplicatingArrays();// example 2. obj.print(a); System.out.println("search(a, 44): " +search(a,44)); System.out.println("search(a, 50): " +search(a,50)); System.out.println("search(a, 77): " +search(a,77)); System.out.println("search(a, 100): " + search(a, 100)); } public static int search(int[] a, int x) { The output for (int i=0; i