SlideShare une entreprise Scribd logo
1  sur  50
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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 Changesstevensreinout
 
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 202Mahmoud Samir Fayed
 
Java Puzzle
Java PuzzleJava Puzzle
Java PuzzleSFilipp
 
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheetanand_study
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arraysIntro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classesIntro 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 212Mahmoud Samir Fayed
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus 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 ObjectsPayel Guria
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 

Tendances (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
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
 
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.
 
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
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
 
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheet
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
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
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
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
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Java generics
Java genericsJava generics
Java generics
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 

En vedette

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-2788SuperServiceChallenge2013
 
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-3567SuperServiceChallenge2013
 
Ryan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through TwitterRyan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through TwitterCM1TO
 
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-3025SuperServiceChallenge2013
 
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 likelihoodFrank 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 VisionFrank 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, StriataInternet 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 VisionFrank 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-3031SuperServiceChallenge2013
 
Photographs for the magazine
Photographs for the magazinePhotographs for the magazine
Photographs for the magazinegryall97
 
Q1 evaluation
Q1 evaluationQ1 evaluation
Q1 evaluationgryall97
 
Andrew Cherwenka | Measurement
Andrew Cherwenka | MeasurementAndrew Cherwenka | Measurement
Andrew Cherwenka | MeasurementCM1TO
 
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
 

En vedette (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
 
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)
 
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
 
Ryan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through TwitterRyan Ginsberg | Building Community Through Twitter
Ryan Ginsberg | Building Community Through Twitter
 
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...
 
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
 
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
 
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)
 
(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
 
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
 
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...
 
(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
 
(ISIA 4) Cours d'algorithmique (1995)
(ISIA 4) Cours d'algorithmique (1995)(ISIA 4) Cours d'algorithmique (1995)
(ISIA 4) Cours d'algorithmique (1995)
 
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
 
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)
 
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
 
Photographs for the magazine
Photographs for the magazinePhotographs for the magazine
Photographs for the magazine
 
Q1 evaluation
Q1 evaluationQ1 evaluation
Q1 evaluation
 
Andrew Cherwenka | Measurement
Andrew Cherwenka | MeasurementAndrew Cherwenka | Measurement
Andrew Cherwenka | Measurement
 
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...
 

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

(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
 
(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
 
(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
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIjagriti srivastava
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingGarth Gilmour
 
Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosGenevaJUG
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Codemotion
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
(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 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
 

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

(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...
 
(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...
 
(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...
 
Scala
ScalaScala
Scala
 
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
Initial Java Core Concept
Initial Java Core ConceptInitial Java Core Concept
Initial Java Core Concept
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
 
Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian Dragos
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
Java programs
Java programsJava programs
Java programs
 
Python for Dummies
Python for DummiesPython for Dummies
Python for Dummies
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
(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...
 
Scala taxonomy
Scala taxonomyScala taxonomy
Scala taxonomy
 
(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...
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
 

Dernier

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 

Dernier (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 

(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 nielsen@lix.polytechnique.fr 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