Professional Documents
Culture Documents
Topic 1 - Roslan Sadjirin
Topic 1 - Roslan Sadjirin
-Roslan Sadjirin
At the end of the lesson, student should be able to:
Understand and explain the concept of Abstract
Data Type.
Understand and explain the concept of Data
Structures.
List and explain the application of data structure
1 2 4 5 8 9
list[index]
list.length = 6 0 1 2 3 4 5
Action
index < list.length- list[index] > temp = list[index] = list[index+1] = 2 9 5 4 8 1
i i < list.length-1 index (i+1) list[index+1] list[index] list[index+1] temp index++ i++
0 TRUE 0 0 < 5; TRUE FALSE 1 2 9 5 4 8 1 remain
1 1 < 5; TRUE TRUE 9 5 9 2 2 5 9 4 8 1 swap
2 2 < 5; TRUE TRUE 9 4 9 3 2 5 4 9 8 1 swap
3 3 < 5; TRUE TRUE 9 8 9 4 2 5 4 8 9 1 swap
4 4 < 5; TRUE TRUE 9 1 9 5 2 5 4 8 1 9 swap
5 5 < 5; FALSE 1 2 5 4 8 1 9 unsorted
1 TRUE 0 0 < 4; TRUE FALSE 1 2 5 4 8 1 9 remain
1 1 < 4; TRUE TRUE 5 4 5 2 2 4 5 8 1 9 swap
2 2 < 4; TRUE FALSE 3 2 4 5 8 1 9 remain
3 3 < 4; TRUE TRUE 8 1 8 4 2 4 5 1 8 9 swap
4 4 < 4; FALSE 2 2 5 4 1 8 9 unsorted
2 TRUE 0 0 < 3; TRUE FALSE 1 2 4 5 1 8 9 remain
1 1 < 3; TRUE FALSE 2 2 4 5 1 8 9 remain
2 2 < 3; TRUE TRUE 5 1 5 3 2 4 1 5 8 9 swap
3 3 < 3; FALSE 3 2 4 1 5 8 9 unsorted
3 TRUE 0 0 < 2; TRUE FALSE 1 2 4 1 5 8 9 remain
1 1 < 2; TRUE TRUE 4 1 4 2 2 1 4 5 8 9 swap
2 2 < 2; FALSE 4 2 1 4 5 8 9 unsorted
4 TRUE 0 0 < 1; TRUE TRUE 2 1 2 1 1 2 4 5 8 9 swap
1 1 < 1; FALSE 5 1 2 4 5 8 9 sorted!
5 FALSE 1 2 4 5 8 9 sorted!
Characteristics:
About twice as fast as the bubble sort.
Not too complex, although it’s slightly more
involved than the bubble sort.
Sort a list of values by repetitively inserting a
particular value into a subset of the list that
has already been sorted.
One at a time, each unsorted element is
inserted at the appropriate position in that
sorted subset until the entire list is in order.
Strategy:
Sort the first two values in the list relative to each
other by exchanging them if necessary.
Insert the third list value into the position relative to
the first two (sorted) values.
Then insert the fourth value into its proper position
relative to the first three values in the list.
◦ Each time an insertion is made, the number of values in the
sorted subset is increases by one.
Continue this process until all values in the list are
completely sorted.
The insertion process requires that the other values
in the list shift to make room for the inserted
element.
Step 1, list = {2}:
◦ Initially, the sorted sublist contains the first element
in the list. Insert 9 into the sublist.
1 2 4 5 8 9
Searching:
Searching is the process of finding a designated
target element within a group of items, or that
the target does not exist within the group.
◦ Searching is the task of determining whether a
collection/list contains some particular element.
The group of items to be searched is sometimes
called the search pool.
E.g. Binary Search, Linear Search, Binary Search
Tree, Hashing, Radix Search, External Search, etc.
Binary search algorithm is one of the common
search approaches for a list of values.
A binary search algorithm eliminates large
parts of the search pool with each
comparison by capitalizing on the fact that
the search pool is in sorted.
◦ For a binary search to work, the elements in the list
must already be ordered (sorted).
Sort the list (assume list is sorted in ascending order)
Searching for the key begins in the middle of the
element/list.
◦ If the key is less than the middle element, search continue
in the first half of the list.
Eliminate the second half of the list.
The search continues in the same manner as in the beginning of
the search (examining the middle).
◦ If the key is equal to the middle element, the search ends
with a match.
◦ If the key is greater than the middle element, search
continue in the second half of the list.
Eliminate the first half of the list.
The search continues in the same manner as in the beginning of
the search (examining the middle).
Each comparison reduces the viable
candidates by half until eventually the target
element is found or there are no more viable
candidates, which means the target element
is not in the search pool.
✓
1st Round
1st round
◦ Key to be searched: 2
◦ list.length = 6
◦ low = 0
◦ high = 5
◦ mid = (low + high) / 2 → (0 + 5) / 2 = 2
2
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
2nd Round
2nd round
◦ Key to be searched: 2
◦ list.length = 6
◦ low = 0
◦ high = 1
◦ mid = (low + high) / 2 → (0 + 1) / 2 = 0
2
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
3rd round
◦ Key to be searched: 2
◦ list.length = 6
◦ low = 1
◦ high = 1
◦ mid = (low + high) / 2 → (1 + 1) / 2 = 1
2
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
mid
Found!
low high
✓
5
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
2nd round
◦ Key to be searched: 5
◦ list.length = 6
◦ low = 3
◦ high = 5
◦ mid = (low + high) / 2 → (3 + 5) / 2 = 4
5
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
3rd round
◦ Key to be searched: 5
◦ list.length = 6
◦ low = 3
◦ high = 3
◦ mid = (low + high) / 2 → (3 + 3) / 2 = 3
5
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
mid
Found!
low high
✓
7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
2nd round
◦ Key to be searched: 7
◦ list.length = 6
◦ low = 3
◦ high = 5
◦ mid = (low + high) / 2 → (3 + 5) / 2 = 4
7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
mid
low high
3rd round
◦ Key to be searched: 7 α
◦ list.length = 6
◦ low = 3
◦ high = 3
◦ mid = 3
Not
Found!
4th round
◦ Key to be searched: 7
◦ list.length = 6
◦ low = 4
◦ high = 3
◦ mid = 3
7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9
list.add("Red");
list.add("White");
list.add("White");
String s = list.get(0);
System.out.println(s);
Output: ?
ArrayList<Integer> list = new
ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
int i = list.get(0);
System.out.println(i);
Output: ?
ArrayList<Double> list = new
ArrayList<Double>();
list.add(1.0);
list.add(2.0);
list.add(3.0);
double i = list.get(0);
System.out.println(i);
Output: ?
Student student1 = new Student("Ali", "CS110");
Student student2 = new Student("Ahmad", "BM111");
ArrayList<Student> listStudent = new
ArrayList<Student>();
listStudent.add(student1);
listStudent.add(student2);
System.out.println(listStudent.get(1));
System.out.println(listStudent.get(0));
Object ob = listStudent.get(1);
System.out.println(ob);
Output: ?
public class Chp1GenericMethod {
public static void main(String[] args)
{
Integer[] integers = {1,2,3,4,5}; Output:
String[] strings = {"London", "Paris", 1
"New York",
"Kuala Lumpur"};
2
3
Chp1GenericMethod.<Integer>print(integers); 4
Chp1GenericMethod.<String>print(strings); 5
--------
}
public static <E> void print(E[] list){ London
for (int i=0; i<list.length; i++) Paris
System.out.println(list[i] + " "); New York
System.out.println("--------"); Kuala
} Lumpur
}
Topic 1
End of Topic 1
Question?