SlideShare a Scribd company logo
1A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Frank NIELSEN
nielsen@lix.polytechnique.fr
A Concise and
Practical
Introduction to
Programming
Algorithms in Java
Chapter 6: Searching and sorting
2A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Agenda
● Answering a few questions
● Searching (sequential, bisection)
● Sorting for faster searching
● Recursive sort
● Hashing
3A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Swapping
Java is pass-by-value only.
Arrays and objects: pass-by-reference (=memory `value')
0 y
Memory for
main
class FunctionCallSample
{
public static void f(double x)
{
x=1;
System.out.println(x);//1
return;
}
public static void main(String[] args)
{
int y=0;
f(y);
System.out.println(y);//y is still 0
}}
Memory for
f
x1
4A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Swapping & Strings
public static void Swap(String p, String q)
{ String tmp;
tmp=p;
p=q;
q=tmp;}
public static void Swap(String [] t, int i, int j)
{ String tmp;
tmp=t[i];
t[i]=t[j];
t[j]=tmp;}
String s1="toto";
String s2="titi";
String [] s=new String[2];
s[0]=new String("toto");
s[1]=new String("titi");
System.out.println(s1+" "+s2);
Swap(s1,s2);
System.out.println(s1+" "+s2);
Swap(s,0,1);
System.out.println(s[0]+" "+s[1]);
Result
5A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Swapping using wrapped integers
// Wrapping integers into an object
class IntObj {
private int x;
public IntObj(int x)
{ this.x = x; }
public int getValue()
{ return this.x; }
public void insertValue(int newx)
{ this.x = newx;}
}
Let us wrap integers into a tailored object named IntObj
6A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
public class SwapInt {
// Pass-by-value (= pass-by-reference for objects)
static void swap(IntObj p, IntObj q)
{
// interchange values inside objects
int t = p.getValue();
p.insertValue(q.getValue());
q.insertValue(t);
}
public static void main(String[] args) {
int a = 23, b = 12;
System.out.println("Before| a:" + a + ", b: " + b);
IntObj aObj = new IntObj(a);
IntObj bObj = new IntObj(b);
swap(aObj, bObj);
a = aObj.getValue();
b = bObj.getValue();
System.out.println("After| a:" + a + ", b: " + b);
}
} Java wrapper class for int is Integer BUT
it doesn't allow you to alter the data field inside.
7A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
class SwapInteger
{
public static void main(String args[])
{
Integer a, b;
a = new Integer(10);
b = new Integer(50);
System.out.println("before swap...");
System.out.println("a is " + a);
System.out.println("b is " + b);
swap(a, b); // References did not change (PASS-BY-VALUE)
System.out.println("after swap...");
System.out.println("a is " + a);
System.out.println("b is " + b);
}
public static void swap(Integer a, Integer b)
{
Integer temp = a;
a = b;
b = temp;
}
}
8A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Searching: Problem statement
● Objects are accessed via a corresponding key
● Each object stores its key and additional fields
● One seeks for information stored in an object from its key
(key= a handle)
● All objects are in the main memory (no external I/O)
More challenging problem:
Adding/removing or changing object attributes dynamically
9A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Searching: Basic examplesSearching: Basic examplesSearching: Basic examples
Dictionary
Key: word
Object:
(word, definition)
class DictionaryEntry
{
String word;
String definition;
}
10A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Dictionary
class DictionaryEntry
{
String word;
String definition;
DictionaryEntry(String w, String def)
{
this.word=new String(w); // Clone the strings
this.definition=new String(def);
}
}
class TestMyDictionary
{public static void main(String[] args){
DictionaryEntry [] Dico =new DictionaryEntry[10];
Dico[0]=new DictionaryEntry("INF311","Cours d'informatique");
Dico[1]=new DictionaryEntry("ECO311","Cours d'economie");
}
}
11A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Searching: Basic examples
● Objects denote people: Each object represents an individual
● The key (handle) is the lastname, firstname, ...
● Additional information fields are:
address, phone number, etc.
class Individual {
String Firstname;
String Lastname;
String Address;
String Phonenumber;
int Age;
boolean Sex;
}
Object
Firstname
Lastname
Phonenumber
int Age
12A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Searching: Basic examples
● Objects denote 2D point sets: Each object is a 2D point
● The key (handle) is the name, or x- or y- ordinate, etc.
● Additional information fields are:
color, name, etc.
class Color {int Red, Green, Blue;}
class Point2D
{
double x;
double y;
String name;
Color color;
}
x
y
name
color
13A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Sequential (linear) search
● Objects are stored in an array (of objects):
Any order is fine (= not necessarily sorted)
● To seek for an object, we browse sequentially the array until
we find the object, or that we report that it is not inside
● Compare the keys of the query with object keys
14A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
class Person{
String firstname, lastname; // key for searching
String phone; // additional fields go here
// Constructor
Person(String l, String f, String p)
{firstname=f; lastname=l; phone=p;}
};
static Person[] array=new Person[5];
static String LinearSearch(String lastname, String firstname)
{
for(int i=0;i<array.length;i++)
{
if ((lastname.equals(array[i].lastname) &&
(firstname.equals(array[i].firstname))))
{return array[i].phone;}
}
return "Not found in my dictionary";
}
15A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
class LinearSearch{
static Person[] array=new Person[5];
static String LinearSearch(String lastname, String firstname)
{...}
public static void main (String [] args)
{
array[0]=new Person("Nielsen","Frank","0169364089");
array[1]=new Person("Nelson","Franck","04227745221");
array[2]=new Person("Durand","Paul","0381846245");
array[3]=new Person("Dupond","Jean","0256234512");
array[4]=new Person("Tanaka","Ken","+81 3 1234 4567");
System.out.println(LinearSearch("Durand","Paul"));
System.out.println(LinearSearch("Tanaka","Ken"));
System.out.println(LinearSearch("Durand","Jean"));
}
}
Array is class variable, here
16A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
● In the worst case, we need to scan the full array
● On average, we scan half the array size (if object is inside)
Donald Knuth. The Art of Computer Programming, Volume 3:
Sorting and Searching, Third Edition. Addison-Wesley, 1997.
ISBN 0-201-89685-0. Section 6.1: Sequential Searching, pp.396–408.
Complexity of sequential search
17A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Adding an element (object)
● To add dynamically an element, we first build a bigger array
● Then we use an index for storing the position of the
last stored element
● When added an element, we increment the position of the
'end of array'
● Of course, we should check whether the array there is
overflow or not
● Notice that it is difficult to erase elements...
18A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Adding an element to the corpus
public static final int MAX_ELEMENTS=100;
static int nbelements=0;
static Person[] array=new Person[MAX_ELEMENTS];
static String LinearSearch(String lastname, String firstname)
{
for(int i=0;i<nbelements;i++)
{
if ((lastname.equals(array[i].lastname) &&
(firstname.equals(array[i].firstname))))
return array[i].phone;
}
return "Not found in my dictionary";
}
static void AddElement(Person obj)
{if (nbelements<MAX_ELEMENTS)
array[nbelements++]=obj; // At most MAX_ELEMENTS-1 here
// nbelements is at most equal to MAX_ELEMENTS now
}
19A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Adding elements to the corpus
public static void main (String [] args)
{
AddElement(new Person("Nielsen","Frank","0169334089"));
AddElement(new Person("Nelson","Franck","04227745221"));
AddElement(new Person("Durand","Paul","0381846245"));
AddElement(new Person("Dupond","Jean","0256234512"));
AddElement(new Person("Tanaka","Ken","+81 3 1234 4567"));
System.out.println(LinearSearch("Durand","Paul"));
System.out.println(LinearSearch("Tanaka","Ken"));
System.out.println(LinearSearch("Durand","Jean"));
AddElement(new Person("Durand", "Jean","0199989796"));
System.out.println(LinearSearch("Durand","Jean"));
}
Check if a person already belongs to the array before adding it
Under or oversaturated memory
(not convenient for non-predictable array sizes)
20A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Dichotomic search (in sorted arrays)
● Assume all elements (keys) are totally sorted in the array
● For example, in increasing order
array[0] < array[1] < ... <
(any lexicographic order would do)
● To find information, use a dichotomic search on the key
● Compare the key of the middle element with the query key:
● If identical, we are done since we found it
● If the key is greater, we search in the rightmost array part
● If the key is less, we search in the leftmost array part
● If the range [left,right] is <=1 and element not inside then...
.....conclude it is not inside the array
21A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Dichotomic search/Bisection search
Think recursion!
static int Dichotomy(int [] array, int left, int right, int key)
{
if (left>right) return -1;
int m=(left+right)/2; // !!! Euclidean division !!!
if (array[m]==key) return m;
else
{
if (array[m]<key) return Dichotomy(array,m+1, right, key);
else return Dichotomy(array,left,m-1, key);
}
}
static int DichotomicSearch(int [] array, int key)
{
return Dichotomy(array,0,array.length-1, key);
}
Result is the index of the element position
Does it always terminate? Why?
22A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Dichotomic search/Bisection search
public static void main (String[] args)
{
int [] v={1, 6, 9, 12 , 45, 67, 76, 80, 95};
System.out.println("Seeking for element 6: Position "+DichotomicSearch(v,6));
System.out.println("Seeking for element 80: Position "+DichotomicSearch(v,80));
System.out.println("Seeking for element 33: Position "+DichotomicSearch(v,33));
}
1, 6, 9, 12 , 45, 67, 76, 80, 95
9 elements [0,8] m=4 6<45
1, 6, 9, 12
4 elements [0,3] m=1 6=6 Found it return 1
23A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
We halve by 2 the array range at every recursion call
n => n/2 => (n/2)/2 => ((n/2)/2)/2 => ...etc... => 1 or 0
How many recursion calls?
2**k=n => k=log(n)
(Execution stack (memory) is also of order log n)
Big O(.)-notation of ALGORITHMSALGORITHMS & complexity
Requires a logarithmic number of operations: O(log n)
t(1024)=10,
t(2048)=11,
t(65556)=16
=> (exponentially more) Efficient compare to sequential search
!!! SORTING HELPS A LOT !!!
Complexity of bisection search
24A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
static int Dichotomy(int [] array, int left, int right, int key)
{
if (left>right)
{throw new RuntimeException("Terminal state reached");
//return -1;
}
int m=(left+right)/2;
if (array[m]==key)
{throw new RuntimeException("Terminal state reached");
//return m;
}
else
{
if (array[m]<key) return Dichotomy(array,m+1, right, key);
else return Dichotomy(array,left,m-1, key);
}
}
25A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Sorting: A fundamental procedure
● Given an (unsorted) array of elements
● We are asked to get a sorted array in, say increasing order
● The only primitive operations (basic operations) are
● comparisons and
● element swappings
static boolean GreaterThan(int a, int b)
{return (a>b);}
static void swap (int [] array, int i, int j)
{
int tmp=array[i];
array[i]=array[j];
array[j]=tmp;
}
26A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Sorting: Sorting by selectionselection
Many different sorting techniques (endless world)
● First, seek for the smallest element of the array
(=SELECTION)
● Put it at the front (using a swapping operation)
● Iterate for the remaining part:
array[1], ..., array[n-1]
=> we seek for the smallest elements for all (sub)arrays
array[j], array[j+1], ..., array[n-1]
27A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A[i] and A[min]
Sorting by selectionselection
Pseudo-code: 2 NESTED LOOPS
28A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A[i] and A[min]
The inner loop (i=0)
for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A[i] and A[min]
The inner loop (i=0)
22 is my first min
29A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A[i] and A[min]
The inner loop (i=1)
35 is my first min
30A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A[i] and A[min]
The outer loop:
min of respective subarrays
31A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Sorting by selection
Two nested loops:
● Select and put the smallest element in front of the array (inner loop)
● Repeat for all subarrays (at the right)
static boolean GreaterThan(int a, int b)
{return (a>b);}
static void swap (int [] array, int i, int j)
{int tmp=array[i];array[i]=array[j];array[j]=tmp;}
static void SelectionSort(int [] array)
{
int n=array.length;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if (GreaterThan(array[i],array[j]))
swap(array,i,j);}
}
}
32A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
public static void main(String[] args)
{
int [] array={22,35,19,26,20,13,42,37,11,24};
SelectionSort(array);
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
System.out.println("");
}
Sorting by selection
33A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
From sorting integers to objects
● Array array contains objects of the same type
● Define a predicate that returns whether an object
is greater than another one or not
● Adjust the swap procedure
In Java 1.5, there are generics, beyond the scope of this course
Generic algorithms for sorting any kind (=type) of elementsGeneric algorithms for sorting any kind (=type) of elements
34A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
For example, sorting events
class EventObject
{
int year, month, day;
EventObject(int y, int m, int d)
{year=y; month=m; day=d;}
static void Display(EventObject obj)
{System.out.println(obj.year+"/"+obj.month+"/"+obj.day);}
}
static boolean GreaterThan(EventObject a, EventObject b)
{return (( a.year>b.year) ||
( (a.year==b.year) && (a.month>b.month)) ||
((a.year==b.year) && (a.month==b.month) && (a.day>b.day)) ) ;}
static void swap (EventObject [] array, int i, int j)
{
EventObject tmp=array[i];
array[i]=array[j];
array[j]=tmp;
}
35A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
sorting events
static void SelectionSort(EventObject [] array)
{
int n=array.length;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if (GreaterThan(array[i],array[j]))
swap(array,i,j);
}
public static void main(String[] args)
{
EventObject [] array=new EventObject[5];
array[0]=new EventObject(2008,06,01);
array[1]=new EventObject(2005,04,03);
array[2]=new EventObject(2005,05,27);
array[3]=new EventObject(2005,04,01);
array[4]=new EventObject(2005,04,15);
SelectionSort(array);
for(int i=0;i<array.length;i++)
EventObject.Display(array[i]);
System.out.println("");
}
2005/4/1
2005/4/3
2005/4/15
2005/5/27
2008/6/1
36A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Worst-case complexity of selection sort
Quadratic time: O(n^2)
= number of elementary operations
= number of comparisons+swap operations
...Try sorting a reversed sorted array...
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
1, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2
1, 2, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3
...
nb=2*array.length*(array.length-1)/2
( 2 times n choose 2)
37A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
class SelectionSort
{
static int nboperations;
static boolean GreaterThan(int a, int b)
{nboperations++; return (a>b);}
static void swap (int [] array, int i, int j)
{
nboperations++;
int tmp=array[i];array[i]=array[j];array[j]=tmp;}
public static void main(String[] args)
{
int [] array={16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
nboperations=0;
SelectionSort(array);
System.out.println("Number of operations:"+nboperations);
int nb=2*array.length*(array.length-1)/2;
System.out.println("Number of operations:"+nb);
}
38A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
● Classify the elements according to array[0] (the pivot):
- those smaller than the pivot: arrayleft
- those greater than the pivot: arrayright
- those equal to the pivot (in case of ties): arraypivot
● Solve recursively the sorting in arrayleft / arrayright,
and recompose as
arrayleft arraypivot arrayright
● Note that a single element is sorted (=terminal case)
39A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Pivot
A recursive approach: QuicksortQuicksort
Partition the array into 3 pieces
40A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
41A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
static int partition(int [] array, int left, int right)
{
int m=left;// pivot
for(int i=left+1; i<=right;i++)
{
if (GreaterThan(array[left],array[i]))
{
swap(array,i,m+1);
m++;// we extend left side array
}
}
// place pivot now
swap(array,left,m);
return m;
}
Sorting is in-place
(require no extre memory storage)
Pivot
42A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
static void quicksort(int [] array, int left, int right)
{
if (right>left)
{
int pivot=partition(array,left,right);
quicksort(array,left,pivot-1);
quicksort(array,pivot+1,right);
}
}
static void QuickSort(int [] array)
{
quicksort(array,0, array.length-1);
}
43A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Quicksort: Analyzing the running time
● Worst-case running time is quadratic O(n^2)
● Expected running time is O(n log n)
● Best case is linear O(n) (all elements are identical)
● Lower bound is n log n: Any algorithm sorting
n numbers require n log n comparison operations
Las Vegas complexity analysis, tail bounds (how much deviation), etc...
http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html
44A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Hashing: Principle and techniques
● Hashing: fundamental technique in computer science
(see INF 421.a')
● Store object x in position h(x) (int) in an array
● Problem occurs if two objects x and y
are stored on the same cell: Collision.
● Finding good hashing functions that minimize collisions, and
adopting a good search policy in case of collisions are
key problems of hashing.
int i;
array[i]
Object Obj=new Object();
int i;
i=h(Obj);// hashing function
array[i]
45A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Hashing functions
● Given a universe X of keys and for any x in X,
we need to find an integer h(x) between 0 and m
● Usually easy to transform the object into an integer:
For example, for strings just add the ASCII codes of characters
● The problem is then to transform
a set of n (sparse) integers
into a compact array of size m<<N.
(<< means much less than)
46A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
http://en.wikipedia.org/wiki/Hash_function
Hashing strings
Constant-size
representation
(compact)
String lengths
may vary much
47A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Hashing functions
Key idea is to take the modulo operation
h(k) = k mod m where m is a prime number.
static int m=23;
static int String2Integer(String s)
{
int result=0;
for(int j=0;j<s.length();j++)
result+=(int)s.charAt(j);
return result;
}
// Note that m is a static variable
static int HashFunction(int l)
{return l%m;}
48A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
public static void main (String[] args)
{
String [] animals={"cat","dog","parrot","horse","fish",
"shark","pelican","tortoise", "whale", "lion",
"flamingo", "cow", "snake", "spider", "bee", "peacock",
"elephant", "butterfly"};
int i;
String [] HashTable=new String[m];
for(i=0;i<m;i++)
HashTable[i]=new String("-->");
for(i=0;i<animals.length;i++)
{int pos=HashFunction(String2Integer(animals[i]));
HashTable[pos]+=(" "+animals[i]);
}
for(i=0;i<m;i++)
System.out.println("Position "+i+"t"+HashTable[i]);
}
49A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
Position 0 --> whale
Position 1 --> snake
Position 2 -->
Position 3 -->
Position 4 -->
Position 5 -->
Position 6 -->
Position 7 --> cow
Position 8 --> shark
Position 9 -->
Position 10 -->
Position 11 -->
Position 12 --> fish
Position 13 --> cat
Position 14 -->
Position 15 --> dog tortoise
Position 16 --> horse
Position 17 --> flamingo
Position 18 -->
Position 19 --> pelican
Position 20 --> parrot lion
Position 21 -->
Position 22 -->
50A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen

More Related Content

What's hot (20)

Extracting Executable Transformations from Distilled Code Changes
Extracting Executable Transformations from Distilled Code ChangesExtracting Executable Transformations from Distilled Code Changes
Extracting Executable Transformations from Distilled Code Changes
stevensreinout
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
Christian Nagel
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
Nishan Barot
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
SFilipp
 
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheet
anand_study
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
Intro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
Intro C# Book
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Ganesh Samarthyam
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
Hussain Behestee
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
Knoldus Inc.
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
Paulo Morgado
 
The Ring programming language version 1.10 book - Part 41 of 212
The Ring programming language version 1.10 book - Part 41 of 212The Ring programming language version 1.10 book - Part 41 of 212
The Ring programming language version 1.10 book - Part 41 of 212
Mahmoud Samir Fayed
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
sidra tauseef
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
Knoldus Inc.
 
Java generics
Java genericsJava generics
Java generics
Hosein Zare
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
Gouda Mando
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
Intro C# Book
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
Payel Guria
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
Extracting Executable Transformations from Distilled Code Changes
Extracting Executable Transformations from Distilled Code ChangesExtracting Executable Transformations from Distilled Code Changes
Extracting Executable Transformations from Distilled Code Changes
stevensreinout
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
Nishan Barot
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
SFilipp
 
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheet
anand_study
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
Intro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
Intro C# Book
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Ganesh Samarthyam
 
The Ring programming language version 1.10 book - Part 41 of 212
The Ring programming language version 1.10 book - Part 41 of 212The Ring programming language version 1.10 book - Part 41 of 212
The Ring programming language version 1.10 book - Part 41 of 212
Mahmoud Samir Fayed
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
Knoldus Inc.
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
Gouda Mando
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
Intro C# Book
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
Payel Guria
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 

Viewers also liked (20)

Point elementary team- olivia, gaby and kody-basket of hope-2788
Point elementary   team- olivia, gaby and kody-basket of hope-2788Point elementary   team- olivia, gaby and kody-basket of hope-2788
Point elementary team- olivia, gaby and kody-basket of hope-2788
SuperServiceChallenge2013
 
Traitement des données massives (INF442, A3)
Traitement des données massives (INF442, A3)Traitement des données massives (INF442, A3)
Traitement des données massives (INF442, A3)
Frank Nielsen
 
Team help our heroes dubuque hempstead i jag-3567
Team help our heroes dubuque hempstead i jag-3567Team help our heroes dubuque hempstead i jag-3567
Team help our heroes dubuque hempstead i jag-3567
SuperServiceChallenge2013
 
Ryan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through TwitterRyan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through Twitter
CM1TO
 
Using science and data to help players make smarter decisions about their hea...
Using science and data to help players make smarter decisions about their hea...Using science and data to help players make smarter decisions about their hea...
Using science and data to help players make smarter decisions about their hea...
Internet World
 
Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025
Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025
Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025
SuperServiceChallenge2013
 
On learning statistical mixtures maximizing the complete likelihood
On learning statistical mixtures maximizing the complete likelihoodOn learning statistical mixtures maximizing the complete likelihood
On learning statistical mixtures maximizing the complete likelihood
Frank Nielsen
 
Traitement des données massives (INF442, A5)
Traitement des données massives (INF442, A5)Traitement des données massives (INF442, A5)
Traitement des données massives (INF442, A5)
Frank Nielsen
 
(slides 5) Visual Computing: Geometry, Graphics, and Vision
(slides 5) Visual Computing: Geometry, Graphics, and Vision(slides 5) Visual Computing: Geometry, Graphics, and Vision
(slides 5) Visual Computing: Geometry, Graphics, and Vision
Frank Nielsen
 
Why email is (still) the killer app, Striata
Why email is (still) the killer app, StriataWhy email is (still) the killer app, Striata
Why email is (still) the killer app, Striata
Internet World
 
Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...
Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...
Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...
Frank Nielsen
 
(slides 2) Visual Computing: Geometry, Graphics, and Vision
(slides 2)  Visual Computing: Geometry, Graphics, and Vision(slides 2)  Visual Computing: Geometry, Graphics, and Vision
(slides 2) Visual Computing: Geometry, Graphics, and Vision
Frank Nielsen
 
(ISIA 4) Cours d'algorithmique (1995)
(ISIA 4) Cours d'algorithmique (1995)(ISIA 4) Cours d'algorithmique (1995)
(ISIA 4) Cours d'algorithmique (1995)
Frank Nielsen
 
On Approximating the Smallest Enclosing Bregman Balls
On Approximating the Smallest Enclosing Bregman Balls On Approximating the Smallest Enclosing Bregman Balls
On Approximating the Smallest Enclosing Bregman Balls
Frank Nielsen
 
Traitement des données massives (INF442, A7)
Traitement des données massives (INF442, A7)Traitement des données massives (INF442, A7)
Traitement des données massives (INF442, A7)
Frank Nielsen
 
Point elementary school team- taylor, sophia and mckenna-basket of hope-3031
Point elementary school team- taylor, sophia and mckenna-basket of hope-3031Point elementary school team- taylor, sophia and mckenna-basket of hope-3031
Point elementary school team- taylor, sophia and mckenna-basket of hope-3031
SuperServiceChallenge2013
 
Photographs for the magazine
Photographs for the magazinePhotographs for the magazine
Photographs for the magazine
gryall97
 
Q1 evaluation
Q1 evaluationQ1 evaluation
Q1 evaluation
gryall97
 
Andrew Cherwenka | Measurement
Andrew Cherwenka | MeasurementAndrew Cherwenka | Measurement
Andrew Cherwenka | Measurement
CM1TO
 
Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...
Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...
Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...
CM1TO
 
Point elementary team- olivia, gaby and kody-basket of hope-2788
Point elementary   team- olivia, gaby and kody-basket of hope-2788Point elementary   team- olivia, gaby and kody-basket of hope-2788
Point elementary team- olivia, gaby and kody-basket of hope-2788
SuperServiceChallenge2013
 
Traitement des données massives (INF442, A3)
Traitement des données massives (INF442, A3)Traitement des données massives (INF442, A3)
Traitement des données massives (INF442, A3)
Frank Nielsen
 
Team help our heroes dubuque hempstead i jag-3567
Team help our heroes dubuque hempstead i jag-3567Team help our heroes dubuque hempstead i jag-3567
Team help our heroes dubuque hempstead i jag-3567
SuperServiceChallenge2013
 
Ryan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through TwitterRyan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through Twitter
CM1TO
 
Using science and data to help players make smarter decisions about their hea...
Using science and data to help players make smarter decisions about their hea...Using science and data to help players make smarter decisions about their hea...
Using science and data to help players make smarter decisions about their hea...
Internet World
 
Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025
Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025
Point elementary school team- vinnie, aniimesh, and namari-basket of hope-3025
SuperServiceChallenge2013
 
On learning statistical mixtures maximizing the complete likelihood
On learning statistical mixtures maximizing the complete likelihoodOn learning statistical mixtures maximizing the complete likelihood
On learning statistical mixtures maximizing the complete likelihood
Frank Nielsen
 
Traitement des données massives (INF442, A5)
Traitement des données massives (INF442, A5)Traitement des données massives (INF442, A5)
Traitement des données massives (INF442, A5)
Frank Nielsen
 
(slides 5) Visual Computing: Geometry, Graphics, and Vision
(slides 5) Visual Computing: Geometry, Graphics, and Vision(slides 5) Visual Computing: Geometry, Graphics, and Vision
(slides 5) Visual Computing: Geometry, Graphics, and Vision
Frank Nielsen
 
Why email is (still) the killer app, Striata
Why email is (still) the killer app, StriataWhy email is (still) the killer app, Striata
Why email is (still) the killer app, Striata
Internet World
 
Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...
Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...
Slides: Total Jensen divergences: Definition, Properties and k-Means++ Cluste...
Frank Nielsen
 
(slides 2) Visual Computing: Geometry, Graphics, and Vision
(slides 2)  Visual Computing: Geometry, Graphics, and Vision(slides 2)  Visual Computing: Geometry, Graphics, and Vision
(slides 2) Visual Computing: Geometry, Graphics, and Vision
Frank Nielsen
 
(ISIA 4) Cours d'algorithmique (1995)
(ISIA 4) Cours d'algorithmique (1995)(ISIA 4) Cours d'algorithmique (1995)
(ISIA 4) Cours d'algorithmique (1995)
Frank Nielsen
 
On Approximating the Smallest Enclosing Bregman Balls
On Approximating the Smallest Enclosing Bregman Balls On Approximating the Smallest Enclosing Bregman Balls
On Approximating the Smallest Enclosing Bregman Balls
Frank Nielsen
 
Traitement des données massives (INF442, A7)
Traitement des données massives (INF442, A7)Traitement des données massives (INF442, A7)
Traitement des données massives (INF442, A7)
Frank Nielsen
 
Point elementary school team- taylor, sophia and mckenna-basket of hope-3031
Point elementary school team- taylor, sophia and mckenna-basket of hope-3031Point elementary school team- taylor, sophia and mckenna-basket of hope-3031
Point elementary school team- taylor, sophia and mckenna-basket of hope-3031
SuperServiceChallenge2013
 
Photographs for the magazine
Photographs for the magazinePhotographs for the magazine
Photographs for the magazine
gryall97
 
Q1 evaluation
Q1 evaluationQ1 evaluation
Q1 evaluation
gryall97
 
Andrew Cherwenka | Measurement
Andrew Cherwenka | MeasurementAndrew Cherwenka | Measurement
Andrew Cherwenka | Measurement
CM1TO
 
Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...
Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...
Erin Bury | It's Not Me, It's You: How to Turn Content From Promotional to Mu...
CM1TO
 
Ad

Similar to (chapter 6) A Concise and Practical Introduction to Programming Algorithms in Java (20)

(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
Lesson 2.2 abstraction
Lesson 2.2   abstractionLesson 2.2   abstraction
Lesson 2.2 abstraction
MLG College of Learning, Inc
 
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
slidlecturlecturlecturlecturlecturlecturlecturlectures06.pptslidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
KierenReynolds3
 
Arrays in Java Programming Language slides
Arrays in Java Programming Language slidesArrays in Java Programming Language slides
Arrays in Java Programming Language slides
ssuser5d6130
 
Ppt chapter11
Ppt chapter11Ppt chapter11
Ppt chapter11
Richard Styner
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
dplunkett
 
JAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptxJAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptx
fwzmypc2004
 
Introduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring arrayIntroduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
Array.ppt
Array.pptArray.ppt
Array.ppt
SbsOmit1
 
array.ppt
array.pptarray.ppt
array.ppt
rajput0302
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
SatyajeetGaur3
 
(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
slidlecturlecturlecturlecturlecturlecturlecturlectures06.pptslidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
KierenReynolds3
 
Arrays in Java Programming Language slides
Arrays in Java Programming Language slidesArrays in Java Programming Language slides
Arrays in Java Programming Language slides
ssuser5d6130
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 8) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
dplunkett
 
JAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptxJAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptx
fwzmypc2004
 
Introduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring arrayIntroduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 
Ad

Recently uploaded (20)

Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptxFIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptxFIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdfENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptxFIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdfHigh Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API CatalogMuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FMESupporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptxFIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptxFIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdfENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptxFIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdfHigh Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API CatalogMuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FMESupporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 

(chapter 6) A Concise and Practical Introduction to Programming Algorithms in Java

  • 1. 1A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Frank NIELSEN [email protected] A Concise and Practical Introduction to Programming Algorithms in Java Chapter 6: Searching and sorting
  • 2. 2A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Agenda ● Answering a few questions ● Searching (sequential, bisection) ● Sorting for faster searching ● Recursive sort ● Hashing
  • 3. 3A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Swapping Java is pass-by-value only. Arrays and objects: pass-by-reference (=memory `value') 0 y Memory for main class FunctionCallSample { public static void f(double x) { x=1; System.out.println(x);//1 return; } public static void main(String[] args) { int y=0; f(y); System.out.println(y);//y is still 0 }} Memory for f x1
  • 4. 4A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Swapping & Strings public static void Swap(String p, String q) { String tmp; tmp=p; p=q; q=tmp;} public static void Swap(String [] t, int i, int j) { String tmp; tmp=t[i]; t[i]=t[j]; t[j]=tmp;} String s1="toto"; String s2="titi"; String [] s=new String[2]; s[0]=new String("toto"); s[1]=new String("titi"); System.out.println(s1+" "+s2); Swap(s1,s2); System.out.println(s1+" "+s2); Swap(s,0,1); System.out.println(s[0]+" "+s[1]); Result
  • 5. 5A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Swapping using wrapped integers // Wrapping integers into an object class IntObj { private int x; public IntObj(int x) { this.x = x; } public int getValue() { return this.x; } public void insertValue(int newx) { this.x = newx;} } Let us wrap integers into a tailored object named IntObj
  • 6. 6A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen public class SwapInt { // Pass-by-value (= pass-by-reference for objects) static void swap(IntObj p, IntObj q) { // interchange values inside objects int t = p.getValue(); p.insertValue(q.getValue()); q.insertValue(t); } public static void main(String[] args) { int a = 23, b = 12; System.out.println("Before| a:" + a + ", b: " + b); IntObj aObj = new IntObj(a); IntObj bObj = new IntObj(b); swap(aObj, bObj); a = aObj.getValue(); b = bObj.getValue(); System.out.println("After| a:" + a + ", b: " + b); } } Java wrapper class for int is Integer BUT it doesn't allow you to alter the data field inside.
  • 7. 7A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen class SwapInteger { public static void main(String args[]) { Integer a, b; a = new Integer(10); b = new Integer(50); System.out.println("before swap..."); System.out.println("a is " + a); System.out.println("b is " + b); swap(a, b); // References did not change (PASS-BY-VALUE) System.out.println("after swap..."); System.out.println("a is " + a); System.out.println("b is " + b); } public static void swap(Integer a, Integer b) { Integer temp = a; a = b; b = temp; } }
  • 8. 8A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Searching: Problem statement ● Objects are accessed via a corresponding key ● Each object stores its key and additional fields ● One seeks for information stored in an object from its key (key= a handle) ● All objects are in the main memory (no external I/O) More challenging problem: Adding/removing or changing object attributes dynamically
  • 9. 9A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Searching: Basic examplesSearching: Basic examplesSearching: Basic examples Dictionary Key: word Object: (word, definition) class DictionaryEntry { String word; String definition; }
  • 10. 10A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Dictionary class DictionaryEntry { String word; String definition; DictionaryEntry(String w, String def) { this.word=new String(w); // Clone the strings this.definition=new String(def); } } class TestMyDictionary {public static void main(String[] args){ DictionaryEntry [] Dico =new DictionaryEntry[10]; Dico[0]=new DictionaryEntry("INF311","Cours d'informatique"); Dico[1]=new DictionaryEntry("ECO311","Cours d'economie"); } }
  • 11. 11A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Searching: Basic examples ● Objects denote people: Each object represents an individual ● The key (handle) is the lastname, firstname, ... ● Additional information fields are: address, phone number, etc. class Individual { String Firstname; String Lastname; String Address; String Phonenumber; int Age; boolean Sex; } Object Firstname Lastname Phonenumber int Age
  • 12. 12A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Searching: Basic examples ● Objects denote 2D point sets: Each object is a 2D point ● The key (handle) is the name, or x- or y- ordinate, etc. ● Additional information fields are: color, name, etc. class Color {int Red, Green, Blue;} class Point2D { double x; double y; String name; Color color; } x y name color
  • 13. 13A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Sequential (linear) search ● Objects are stored in an array (of objects): Any order is fine (= not necessarily sorted) ● To seek for an object, we browse sequentially the array until we find the object, or that we report that it is not inside ● Compare the keys of the query with object keys
  • 14. 14A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen class Person{ String firstname, lastname; // key for searching String phone; // additional fields go here // Constructor Person(String l, String f, String p) {firstname=f; lastname=l; phone=p;} }; static Person[] array=new Person[5]; static String LinearSearch(String lastname, String firstname) { for(int i=0;i<array.length;i++) { if ((lastname.equals(array[i].lastname) && (firstname.equals(array[i].firstname)))) {return array[i].phone;} } return "Not found in my dictionary"; }
  • 15. 15A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen class LinearSearch{ static Person[] array=new Person[5]; static String LinearSearch(String lastname, String firstname) {...} public static void main (String [] args) { array[0]=new Person("Nielsen","Frank","0169364089"); array[1]=new Person("Nelson","Franck","04227745221"); array[2]=new Person("Durand","Paul","0381846245"); array[3]=new Person("Dupond","Jean","0256234512"); array[4]=new Person("Tanaka","Ken","+81 3 1234 4567"); System.out.println(LinearSearch("Durand","Paul")); System.out.println(LinearSearch("Tanaka","Ken")); System.out.println(LinearSearch("Durand","Jean")); } } Array is class variable, here
  • 16. 16A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen ● In the worst case, we need to scan the full array ● On average, we scan half the array size (if object is inside) Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Section 6.1: Sequential Searching, pp.396–408. Complexity of sequential search
  • 17. 17A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Adding an element (object) ● To add dynamically an element, we first build a bigger array ● Then we use an index for storing the position of the last stored element ● When added an element, we increment the position of the 'end of array' ● Of course, we should check whether the array there is overflow or not ● Notice that it is difficult to erase elements...
  • 18. 18A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Adding an element to the corpus public static final int MAX_ELEMENTS=100; static int nbelements=0; static Person[] array=new Person[MAX_ELEMENTS]; static String LinearSearch(String lastname, String firstname) { for(int i=0;i<nbelements;i++) { if ((lastname.equals(array[i].lastname) && (firstname.equals(array[i].firstname)))) return array[i].phone; } return "Not found in my dictionary"; } static void AddElement(Person obj) {if (nbelements<MAX_ELEMENTS) array[nbelements++]=obj; // At most MAX_ELEMENTS-1 here // nbelements is at most equal to MAX_ELEMENTS now }
  • 19. 19A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Adding elements to the corpus public static void main (String [] args) { AddElement(new Person("Nielsen","Frank","0169334089")); AddElement(new Person("Nelson","Franck","04227745221")); AddElement(new Person("Durand","Paul","0381846245")); AddElement(new Person("Dupond","Jean","0256234512")); AddElement(new Person("Tanaka","Ken","+81 3 1234 4567")); System.out.println(LinearSearch("Durand","Paul")); System.out.println(LinearSearch("Tanaka","Ken")); System.out.println(LinearSearch("Durand","Jean")); AddElement(new Person("Durand", "Jean","0199989796")); System.out.println(LinearSearch("Durand","Jean")); } Check if a person already belongs to the array before adding it Under or oversaturated memory (not convenient for non-predictable array sizes)
  • 20. 20A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Dichotomic search (in sorted arrays) ● Assume all elements (keys) are totally sorted in the array ● For example, in increasing order array[0] < array[1] < ... < (any lexicographic order would do) ● To find information, use a dichotomic search on the key ● Compare the key of the middle element with the query key: ● If identical, we are done since we found it ● If the key is greater, we search in the rightmost array part ● If the key is less, we search in the leftmost array part ● If the range [left,right] is <=1 and element not inside then... .....conclude it is not inside the array
  • 21. 21A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Dichotomic search/Bisection search Think recursion! static int Dichotomy(int [] array, int left, int right, int key) { if (left>right) return -1; int m=(left+right)/2; // !!! Euclidean division !!! if (array[m]==key) return m; else { if (array[m]<key) return Dichotomy(array,m+1, right, key); else return Dichotomy(array,left,m-1, key); } } static int DichotomicSearch(int [] array, int key) { return Dichotomy(array,0,array.length-1, key); } Result is the index of the element position Does it always terminate? Why?
  • 22. 22A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Dichotomic search/Bisection search public static void main (String[] args) { int [] v={1, 6, 9, 12 , 45, 67, 76, 80, 95}; System.out.println("Seeking for element 6: Position "+DichotomicSearch(v,6)); System.out.println("Seeking for element 80: Position "+DichotomicSearch(v,80)); System.out.println("Seeking for element 33: Position "+DichotomicSearch(v,33)); } 1, 6, 9, 12 , 45, 67, 76, 80, 95 9 elements [0,8] m=4 6<45 1, 6, 9, 12 4 elements [0,3] m=1 6=6 Found it return 1
  • 23. 23A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen We halve by 2 the array range at every recursion call n => n/2 => (n/2)/2 => ((n/2)/2)/2 => ...etc... => 1 or 0 How many recursion calls? 2**k=n => k=log(n) (Execution stack (memory) is also of order log n) Big O(.)-notation of ALGORITHMSALGORITHMS & complexity Requires a logarithmic number of operations: O(log n) t(1024)=10, t(2048)=11, t(65556)=16 => (exponentially more) Efficient compare to sequential search !!! SORTING HELPS A LOT !!! Complexity of bisection search
  • 24. 24A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen static int Dichotomy(int [] array, int left, int right, int key) { if (left>right) {throw new RuntimeException("Terminal state reached"); //return -1; } int m=(left+right)/2; if (array[m]==key) {throw new RuntimeException("Terminal state reached"); //return m; } else { if (array[m]<key) return Dichotomy(array,m+1, right, key); else return Dichotomy(array,left,m-1, key); } }
  • 25. 25A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Sorting: A fundamental procedure ● Given an (unsorted) array of elements ● We are asked to get a sorted array in, say increasing order ● The only primitive operations (basic operations) are ● comparisons and ● element swappings static boolean GreaterThan(int a, int b) {return (a>b);} static void swap (int [] array, int i, int j) { int tmp=array[i]; array[i]=array[j]; array[j]=tmp; }
  • 26. 26A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Sorting: Sorting by selectionselection Many different sorting techniques (endless world) ● First, seek for the smallest element of the array (=SELECTION) ● Put it at the front (using a swapping operation) ● Iterate for the remaining part: array[1], ..., array[n-1] => we seek for the smallest elements for all (sub)arrays array[j], array[j+1], ..., array[n-1]
  • 27. 27A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen for i ← 0 to n-2 do min ← i for j ← (i + 1) to n-1 do if A[j] < A[min] min ← j swap A[i] and A[min] Sorting by selectionselection Pseudo-code: 2 NESTED LOOPS
  • 28. 28A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen for i ← 0 to n-2 do min ← i for j ← (i + 1) to n-1 do if A[j] < A[min] min ← j swap A[i] and A[min] The inner loop (i=0) for i ← 0 to n-2 do min ← i for j ← (i + 1) to n-1 do if A[j] < A[min] min ← j swap A[i] and A[min] The inner loop (i=0) 22 is my first min
  • 29. 29A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen for i ← 0 to n-2 do min ← i for j ← (i + 1) to n-1 do if A[j] < A[min] min ← j swap A[i] and A[min] The inner loop (i=1) 35 is my first min
  • 30. 30A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen for i ← 0 to n-2 do min ← i for j ← (i + 1) to n-1 do if A[j] < A[min] min ← j swap A[i] and A[min] The outer loop: min of respective subarrays
  • 31. 31A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Sorting by selection Two nested loops: ● Select and put the smallest element in front of the array (inner loop) ● Repeat for all subarrays (at the right) static boolean GreaterThan(int a, int b) {return (a>b);} static void swap (int [] array, int i, int j) {int tmp=array[i];array[i]=array[j];array[j]=tmp;} static void SelectionSort(int [] array) { int n=array.length; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ if (GreaterThan(array[i],array[j])) swap(array,i,j);} } }
  • 32. 32A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen public static void main(String[] args) { int [] array={22,35,19,26,20,13,42,37,11,24}; SelectionSort(array); for(int i=0;i<array.length;i++) System.out.print(array[i]+" "); System.out.println(""); } Sorting by selection
  • 33. 33A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen From sorting integers to objects ● Array array contains objects of the same type ● Define a predicate that returns whether an object is greater than another one or not ● Adjust the swap procedure In Java 1.5, there are generics, beyond the scope of this course Generic algorithms for sorting any kind (=type) of elementsGeneric algorithms for sorting any kind (=type) of elements
  • 34. 34A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen For example, sorting events class EventObject { int year, month, day; EventObject(int y, int m, int d) {year=y; month=m; day=d;} static void Display(EventObject obj) {System.out.println(obj.year+"/"+obj.month+"/"+obj.day);} } static boolean GreaterThan(EventObject a, EventObject b) {return (( a.year>b.year) || ( (a.year==b.year) && (a.month>b.month)) || ((a.year==b.year) && (a.month==b.month) && (a.day>b.day)) ) ;} static void swap (EventObject [] array, int i, int j) { EventObject tmp=array[i]; array[i]=array[j]; array[j]=tmp; }
  • 35. 35A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen sorting events static void SelectionSort(EventObject [] array) { int n=array.length; for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if (GreaterThan(array[i],array[j])) swap(array,i,j); } public static void main(String[] args) { EventObject [] array=new EventObject[5]; array[0]=new EventObject(2008,06,01); array[1]=new EventObject(2005,04,03); array[2]=new EventObject(2005,05,27); array[3]=new EventObject(2005,04,01); array[4]=new EventObject(2005,04,15); SelectionSort(array); for(int i=0;i<array.length;i++) EventObject.Display(array[i]); System.out.println(""); } 2005/4/1 2005/4/3 2005/4/15 2005/5/27 2008/6/1
  • 36. 36A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Worst-case complexity of selection sort Quadratic time: O(n^2) = number of elementary operations = number of comparisons+swap operations ...Try sorting a reversed sorted array... 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 1, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 1, 2, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3 ... nb=2*array.length*(array.length-1)/2 ( 2 times n choose 2)
  • 37. 37A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen class SelectionSort { static int nboperations; static boolean GreaterThan(int a, int b) {nboperations++; return (a>b);} static void swap (int [] array, int i, int j) { nboperations++; int tmp=array[i];array[i]=array[j];array[j]=tmp;} public static void main(String[] args) { int [] array={16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}; nboperations=0; SelectionSort(array); System.out.println("Number of operations:"+nboperations); int nb=2*array.length*(array.length-1)/2; System.out.println("Number of operations:"+nb); }
  • 38. 38A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen ● Classify the elements according to array[0] (the pivot): - those smaller than the pivot: arrayleft - those greater than the pivot: arrayright - those equal to the pivot (in case of ties): arraypivot ● Solve recursively the sorting in arrayleft / arrayright, and recompose as arrayleft arraypivot arrayright ● Note that a single element is sorted (=terminal case)
  • 39. 39A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Pivot A recursive approach: QuicksortQuicksort Partition the array into 3 pieces
  • 40. 40A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen
  • 41. 41A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen static int partition(int [] array, int left, int right) { int m=left;// pivot for(int i=left+1; i<=right;i++) { if (GreaterThan(array[left],array[i])) { swap(array,i,m+1); m++;// we extend left side array } } // place pivot now swap(array,left,m); return m; } Sorting is in-place (require no extre memory storage) Pivot
  • 42. 42A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen static void quicksort(int [] array, int left, int right) { if (right>left) { int pivot=partition(array,left,right); quicksort(array,left,pivot-1); quicksort(array,pivot+1,right); } } static void QuickSort(int [] array) { quicksort(array,0, array.length-1); }
  • 43. 43A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Quicksort: Analyzing the running time ● Worst-case running time is quadratic O(n^2) ● Expected running time is O(n log n) ● Best case is linear O(n) (all elements are identical) ● Lower bound is n log n: Any algorithm sorting n numbers require n log n comparison operations Las Vegas complexity analysis, tail bounds (how much deviation), etc... http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html
  • 44. 44A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Hashing: Principle and techniques ● Hashing: fundamental technique in computer science (see INF 421.a') ● Store object x in position h(x) (int) in an array ● Problem occurs if two objects x and y are stored on the same cell: Collision. ● Finding good hashing functions that minimize collisions, and adopting a good search policy in case of collisions are key problems of hashing. int i; array[i] Object Obj=new Object(); int i; i=h(Obj);// hashing function array[i]
  • 45. 45A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Hashing functions ● Given a universe X of keys and for any x in X, we need to find an integer h(x) between 0 and m ● Usually easy to transform the object into an integer: For example, for strings just add the ASCII codes of characters ● The problem is then to transform a set of n (sparse) integers into a compact array of size m<<N. (<< means much less than)
  • 46. 46A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen http://en.wikipedia.org/wiki/Hash_function Hashing strings Constant-size representation (compact) String lengths may vary much
  • 47. 47A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Hashing functions Key idea is to take the modulo operation h(k) = k mod m where m is a prime number. static int m=23; static int String2Integer(String s) { int result=0; for(int j=0;j<s.length();j++) result+=(int)s.charAt(j); return result; } // Note that m is a static variable static int HashFunction(int l) {return l%m;}
  • 48. 48A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen public static void main (String[] args) { String [] animals={"cat","dog","parrot","horse","fish", "shark","pelican","tortoise", "whale", "lion", "flamingo", "cow", "snake", "spider", "bee", "peacock", "elephant", "butterfly"}; int i; String [] HashTable=new String[m]; for(i=0;i<m;i++) HashTable[i]=new String("-->"); for(i=0;i<animals.length;i++) {int pos=HashFunction(String2Integer(animals[i])); HashTable[pos]+=(" "+animals[i]); } for(i=0;i<m;i++) System.out.println("Position "+i+"t"+HashTable[i]); }
  • 49. 49A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen Position 0 --> whale Position 1 --> snake Position 2 --> Position 3 --> Position 4 --> Position 5 --> Position 6 --> Position 7 --> cow Position 8 --> shark Position 9 --> Position 10 --> Position 11 --> Position 12 --> fish Position 13 --> cat Position 14 --> Position 15 --> dog tortoise Position 16 --> horse Position 17 --> flamingo Position 18 --> Position 19 --> pelican Position 20 --> parrot lion Position 21 --> Position 22 -->
  • 50. 50A Concise and Practical Introduction to Programming Algorithms in Java © 2009 Frank Nielsen