SlideShare a Scribd company logo
1 of 58
Chapter 11
Sorting and Searching
Chapter 10 Objectives
After you have read and studied this chapter, you
should be able to
• Perform linear and binary search algorithms on small arrays.
• Determine whether a linear or binary search is more effective
for a given situation.
• Perform selection and bubble sort algorithms.
• Describe the heapsort algorithm and show how its performance
is superior to the other two algorithms.
• Apply basic sorting algorithms to sort an array of objects.
Searching
• When we maintain a collection of data, one of the
operations we need is a search routine to locate desired
data quickly.
• Here’s the problem statement:
Given a value X, return the index of X in the array, if such X exists. Otherwise,
return NOT_FOUND (-1). We assume there are no duplicate entries in the
array.

• We will count the number of comparisons the algorithms
make to analyze their performance.
– The ideal searching algorithm will make the least possible number
of comparisons to locate the desired data.
– Two separate performance analyses are normally done, one for
successful search and another for unsuccessful search.
Search Result
Unsuccessful Search:

search( 45 )

NOT_FOUND

Successful Search:

search( 12 )

4

number

0

1

2

3

4

5

6

7

8

23

17

5

90

12

44

38

84

77
Linear Search
• Search the array from the first to the last position
in linear progression.
public int linearSearch ( int[] number, int searchValue ) {
int

loc = 0;

while (loc < number.length
searchValue) {

&&

number[loc] !=

loc++;
}
if (loc == number.length) { //Not found
return NOT_FOUND;
} else {
return loc;
}
}

//Found, return the position
Linear Search Performance
• We analyze the successful and unsuccessful
searches separately.
• We count how many times the search value is
compared against the array elements.
• Successful Search
– Best Case – 1 comparison
– Worst Case – N comparisons (N – array size)

• Unsuccessful Search
– Best Case =

Worst Case – N comparisons
Binary Search
• If the array is sorted, then we can apply the binary search
technique.
number

0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

• The basic idea is straightforward. First search the value in
the middle position. If X is less than this value, then search
the middle of the left half next. If X is greater than this
value, then search the middle of the right half next.
Continue in this manner.
Sequence of Successful Search - 1
low
#1

high

0

8

search( 44 )

mid
4

 low + high 
mid = 

2



0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

low

mid
38 < 44

high

low = mid+1 = 5
Sequence of Successful Search - 2
low
#1
#2

high

0
5

8
8

search( 44 )

mid
4
6

 low + high 
mid = 

2



0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

low

high = mid-1=5

mid
44 < 77

high
Sequence of Successful Search - 3
low
#1
#2
#3

high

mid

search( 44 )

0
5
5

8
8
5

4
6
5

 low + high 
mid = 

2



0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

Successful Search!!
44 == 44

low high
mid
Sequence of Unsuccessful Search - 1
low
#1

high

0

8

search( 45 )

mid
4

 low + high 
mid = 

2



0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

low

mid
38 < 45

high

low = mid+1 = 5
Sequence of Unsuccessful Search - 2
low
#1
#2

high

0
5

8
8

search( 45 )

mid
4
6

 low + high 
mid = 

2



0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

low

high = mid-1=5

mid
45 < 77

high
Sequence of Unsuccessful Search - 3
low
#1
#2
#3

high

mid

search( 45 )

0
5
5

8
8
5

4
6
5

 low + high 
mid = 

2



0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

low high
mid

44 < 45

low = mid+1 = 6
Sequence of Unsuccessful Search - 4
low
#1
#2
#3
#4

high

mid

search( 45 )

0
5
5
6

8
8
5
5

4
6
5

 low + high 
mid = 

2



0

1

2

3

4

5

6

7

8

5

12

17 23

38

44

77

84

90

high

low

Unsuccessful Search
no more elements to search

low > high
Binary Search Routine
public int binarySearch (int[] number, int searchValue) {
int

low
high
mid

= 0,
= number.length - 1,
= (low + high) / 2;

while (low <= high && number[mid] != searchValue) {
if (number[mid] < searchValue) {
low = mid + 1;
} else { //number[mid] > searchValue
high = mid - 1;
}
mid
= (low + high) / 2; //integer division
will truncate
}
if (low > high) {
mid = NOT_FOUND;
}
return mid;
}
Binary Search Performance
• Successful Search
– Best Case – 1 comparison
– Worst Case – log2N comparisons

• Unsuccessful Search
– Best Case =
Worst Case – log2N comparisons

• Since the portion of an array to search is cut into half after
every comparison, we compute how many times the array
can be divided into halves.
• After K comparisons, there will be N/2K elements in the list.
We solve for K when N/2K = 1, deriving K = log2N.
Comparing N and log2N Performance
Array Size

10
50
100
500
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

Linear – N

10
50
100
500
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

Binary – log2N

4
6
7
9
10
11
12
12
13
13
13
13
14
14
Sorting
• When we maintain a collection of data, many applications
call for rearranging the data in certain order, e.g. arranging
Person information in ascending order of age.
• Here’s the problem statement:
Given an array of N integer values, arrange the values into ascending
order.

• We will count the number of comparisons the algorithms
make to analyze their performance.
– The ideal sorting algorithm will make the least possible number of
comparisons to arrange data in a designated order.

• We will compare different sorting algorithms by analyzing
their worst-case performance.
Selection Sort
min

first

1. Find the smallest element
in the list.
2. Exchange the element in
the first position and the
smallest element. Now
the smallest element is in
the first position.
3. Repeat Step 1 and 2 with
the list having one less
element (i.e., the smallest
element is discarded from
further processing).

0

1

2

3

4

5

6

7

8

23

17

5

90

12

44

38

84

77

exchange

0

1

2

3

4

5

6

7

8

5

17

23 90

12

44

38

84

77

sorted

unsorted

This is the result of one pass.
Selection Sort Passes
Pass #
1

0

1

2

3

4

5

6

7

8

5

17

23 90

12

44

38

84

77

sorted

2

5

12

23 90

17

44

38

84

77

3

5

12

17 90

23

44

38

84

77

7

5

12

17 23

38

44

77

84

90

8

5

12

17 23

38

44

77

84

90

Result AFTER one
Result AFTER one
pass is completed.
pass is completed.
Selection Sort Routine
public void selectionSort(int[] number) {
int startIndex, minIndex, length, temp;
length = number.length;
for (startIndex = 0; startIndex <= length-2; startIndex++){
//each iteration of the for loop is one pass
minIndex = startIndex;
minIndex
minIndex = i;

//find the smallest in this pass at position
for (int i = startIndex+1; i <= length-1; i++) {
if (number[i] < number[minIndex])

}
//exchange number[startIndex] and number[minIndex]
temp
=
number[startIndex];
number[startIndex] = number[minIndex];
number[minIndex]
= temp;
}
}
Selection Sort Performance
• We derive the total number of
comparisons by counting the
number of times the inner loop
is executed.
• For each execution of the outer
loop, the inner loop is executed
length – start times.
• The variable length is the size of
the array. Replacing length with
N, the array size, the sum is
derived as…
Bubble Sort
• With the selection sort, we make one exchange at the end
of one pass.
• The bubble sort improves the performance by making
more than one exchange during its pass.
• By making multiple exchanges, we will be able to move
more elements toward their correct positions using the
same number of comparisons as the selection sort makes.
• The key idea of the bubble sort is to make pairwise
comparisons and exchange the positions of the pair if they
are out of order.
One Pass of Bubble Sort
0

1

23 17

2

3

5

4

5

6

7

8

90 12 44 38 84 77

17 5

exchange

17 23

5

exchange

90 12 44 38 84 77

17 5

exchange

17 5

17 5

23 12 44 38 90 84 77
exchange

23 90 12 44 38 84 77
ok

23 12 44 90 38 84 77

17 5

exchange

23 12 90 44 38 84 77
exchange

23 12 44 38 84 90 77
exchange

17 5

23 12 44 38 84 77 90

The largest value 90 is at the end of
The largest value 90 is at the end of
the list.
the list.
Bubble Sort Routine
public void bubbleSort(int[] number) {
int
boolean

temp, bottom, i;
exchanged = true;

bottom = number.length - 2;
while (exchanged) {
exchanged = false;

number[i];

for (i = 0; i <= bottom; i++) {
if (number[i] > number[i+1]) {
temp
=
//exchange
number[i]
= number[i+1];
number[i+1] = temp;
exchanged = true; //exchange

is made
}
}
bottom--;
}
}
Bubble Sort Performance
• In the worst case, the outer while loop
is executed N-1 times for carrying out
N-1 passes.
• For each execution of the outer loop,
the inner loop is executed bottom+1
times. The number of comparisons in
each successive pass is N-1, N-2, … ,
1. Summing these will result in the
total number of comparisons.
• So the performances of the bubble
sort and the selection sort are
approximately equivalent. However, on
the average, the bubble sort performs
much better than the selection sort
because it can finish the sorting
without doing all N-1 passes.
Heapsort
• Selection and bubble sorts are two fundamental sorting
algorithms that take approximately N2 comparisons to sort
an array of N elements.
• One sorting algorithm that improves the performance to
approximately 1.5Nlog2N is called heapsort.
• The heapsort algorithm uses a special data structure
called a heap.
• A heap structure can be implemented very effectively by
using an array.
Sample Heap
Level #

0

1

root

90
90
1

84
84

2

index

2

44
44

3

7

4

17
17

5

6

77
77

3

4

12
12

5
5

38
38

left child
of
44

right child
of
44

8

23
23
Heap Constraints
• A heap must satisfy the following two constraints:
– Structural Constraint: Nodes in a heap with N nodes
must occupy the positions numbered 0, 1, ..., N-1.
– Value Relationship Constraint: A value stored in a node
must be larger than the maximum of the values stored
in its left and right children.
Structural Constraints
• Sample heaps and nonheaps that violate the structural
constraints:

Heaps

Nonheaps
Value Relationship Constraints
• Sample heaps and nonheaps that violate the value
relationship constraints:
45
45
25
25

Heaps

12
12

16
16
3
3

22
22

45
45

12
12

Nonheaps

58
58

25
25

12
12

22
22

45
45
25
25

33
33
34 23
34 23

24
24
13 16
13 16

12
12

45
45
16
16

3
3

35
35

34
34
11 22
11 22

9
9

45
45
55
55

90
90

22
22

55
55
3
3

12
12
Heapsort Algorithm
• How can we use the heap structure to sort N
elements?
• Heapsort is carried out in two phases:
– Construction Phase: Construct a heap with given N
elements.
– Extraction Phase: Pull out the value in the root
successively, creating a new heap with one less
element after each extraction.
Extraction Phase
• After each extraction, a heap must be rebuilt with one
less element. One sample extraction step:
0

1

2

3

4

5

6

7

8

0

1

2

3

4

1
84
84

7
17
17

8
23
23

1
84
77
23
84
77
23

2
44
44
4
12
12

Before

6

7

8
90

0
84
23
90
84
23
90

0
90
90

3
77
77

5

5
5
5

6
38
38

7
17
17

3
77
23
77
23

2
44
44
4
12
12

5
5
5

6
38
38

8
23
23 >23
max{17}?
max{77, 12}? NO, so stop.
max{84, 44}? YES,so swap.

After
Rebuild Steps
• A sequence of rebuild steps to sort the list.
0

1

2

3

4

5

6

7

8

0
90
90

7
17
17

8
23
23

2
44
44
4
12
12

Before

1
12

2
17

3
23

4
5
38 44

6
77

7
8
84 90

0
84
90
38
77
44
12
17
23
5
12
17
23
38
44
77
84
90
5

1
84
84
3
77
77

0
5

5
5
5

6
38
38

1
2
Sorting was completed
17
84
5
12
23
77
38
44
5
17
84
5
12
23
77
44
38
5
after 8 rebuild steps.
3
4
5
12
17
12
5
77
23
12
12
5
77
17
23
7
8
17
23
17
23

Done

6
38
38
Construction Phase
• Rebuild steps are applied from position (N-2)/2  to
position 0.
0

0
0

(9-2)/2  = 3

23
23
1

2

11

17
17

5
5

90
23
90
23

84
90
17
84
90
17

2
44
5
44
5

3

4

5

6

3

4

5

6

90
90

12
12

44
44

38
38

77
84
90
77
84
90

12
12

44
5
44
5

38
38

7

8

84
84

77
77

Before

7

8

17
84
17
84

23
77
23
77

Done
Heap Implementation
• We need to implement the abstract data structure
heap into a concrete data structure.
0
90
90
1
84
84

0

2
44
44

1

2

3

4

5

90 84 44 77 12 5

3

4

5

12
12

5
5

7

8

38 17 23

6

77
77

6

38
38

7

8

17
17

23
23

Abstract Heap Structure

Concrete Implementation
The construct Method
private void construct( ) {
for (int i = (heap.length-2) / 2; i >= 0; i--) {
current = i;
done
= false;

child) {

while ( !done ) {
if ( current has no children ) {
done = true;
} else {
if (current node < larger
swap the two nodes;
set current points

to the larger child;

} else {
done = true;
}
}
}
}
}
The extract Method
private void extract( ) {
for (int size = heap.length-1; size >= 0; size--) {
remove the root node data;
move the last node to the root;
done

element

= false; //rebuild the heap with one less

while (!done) {
if (current has no children) {
done = true;
} else {
if (current node < larger child)

{

swap the two nodes;
set current points to

the larger child;
} else {

done = true;
}

}

}

}

}
Heapsort Performance
• The total number of comparisons in the extraction phase
is (N-1)×K where K is the depth of a heap.
• We solve for K using the fact that the heap must satisfy
the structural constraint:
K

2i −1 = 2 K − 1
∑
i =1

2K −1 − 1 < N ≤ 2K − 1

K =  log2 ( N + 1)
Therefore,

( N − 1) × K ≅ N log2 N

total # nodes in a heap
with depth K
this holds because
of the structural
constraint
Heapsort Performance (cont'd)
• There are N/2 rebuild steps in the construction phase.
• Each rebuild step will take no more than K comparisons.
• The total number of comparisons in the construction
phase is approximately

N
log2 N
2
Therefore, the total number of comparisons for both
phases is

N
log2 N + N log2 N = 1.5 N log2 N
2
Problem Statement
• Problem statement:
Add the sorting capability to the AddressBook
class from Chapter 10. The new AddressBook
class will include a method that sort Person
objects in alphabetical order of their names or in
ascending order of their ages.
Overall Plan
• Instead of going through the development steps,
we will present three different implementations.
• The three versions are named AddressBookVer1,
AddressBookVer2, and AddressBookVer3.
• These classes will implement the AddressBook
interface.
The AddressBook Interface
interface AddressBook {
public void add(Person newPerson);
public boolean delete(String
searchName);
public Person search(String searchName);
public Person[ ] sort (int attribute);
}
AddressBookVer1 Design
• We use an array of Person objects
• We provide our own sorting method in the
AddressBookVer1 class
• We define the Person class so we can compare
Person objects by name or by age
Comparing Person Objects
• First, we need to determine how to
compare Person objects
• The following does not make sense:
Person p1 = …;
Person p2 = …;
if ( p1 < p2 ) { … }
Modify the Person Class
• Modify the Person class to include a variable that tells
which attribute to compare.
class Person {
…
public
public
public
…
public
{

static final int NAME = 0;
static final int AGE = 1;
static int compareAttribute = NAME;
static void setCompareAttribute(int attribute)
compareAttribute = attribute;

}
…
}
The compareTo Method
• To compare Person objects, first set the comparison
attribute and then call the compareTo Method.
Person.setCompareAttribute
(Person.NAME);

public int compareTo(Person p) {

int compResult = p1.compareTo(p2);
AGE ) {
if (compResult < 0) {
//p1 “less than” p2

int compResult;
if ( comparisonAttribute ==

p.getAge();
p2age ) {

} else if (compResult == 0) {
//p1 “equals” pw
} else {
//p2 “greater than” p2
}

LESS;

int p2age =
if ( this.age <
compResult =

}
…
} else { //compare Name
String p2Name =
p.getName();
compResult =
this.name.
compareTo(p2Name);
}
return compResult;
The sort Method
public Person[ ] sort (int attribute) {
Person[ ] sortedList = new Person[count];
Person p1, p2, temp;
//copy references to sortedList
for (int i = 0; i < count; i++) {
sortedList[i] = entry[i];
}
//Set the comparison attribute
Person.setCompareAttribute(attribute);
//begin the bubble sort on sortedList
…
}
return sortedList;
}
The Use of sortedList in the sort Method
AddressBookVer1 Code
Program source file is too big to list here. From now on, we ask
you to view the source files using your Java IDE.

Directory:
Directory:

Chapter11/
Chapter11/

Source Files: AddressBookVer1.java
Source Files: AddressBookVer1.java
AddressBookVer2 Design
• We use the java.util.Arrays class to sort an array
of Person objects
• The Person class does not include any
comparison methods. Instead, we implement the
Comparator interface. We can define the
implementation class so we can compare Person
objects using any combination of their attributes
– For example, we can define a comparator that
compares Person objects by using both name and age
The AgeComparator Class
• This class compares the age attributes of Person objects
class AgeComparator implements Comparator {
private final int LESS = -1;
private final int EQUAL = 0;
private final int MORE = 1;
public int compare(Object p1, Object p2) {
int comparisonResult;
int p1age = ((Person)p1).getAge( );
int p2age = ((Person)p2).getAge( );
if (p1age < p2age) {
comparisonResult = LESS;
} else if (p1age == p2age) {
comparisonResult = EQUAL;
} else {
assert p1age > p2age;
comparisonResult = MORE;
}
return comparisonResult;
}
}
The NameComparator Class
• This class compares the age attributes of Person objects
• Because the name attribute is a string we can use the
compareTo method of the String class
class NameComparator implements Comparator {
public int compare(Object p1, Object p2) {
String p1name = ((Person)p1).getName( );
String p2name = ((Person)p2).getName( );
return p1name.compareTo(p2name);
}
}
The sort Method
• We use the sort method of the java.util.Arrays class
public Person[ ] sort ( int attribute ) {
if (!(attribute == Person.NAME ||
attribute == Person.AGE) ) {
throw new IllegalArgumentException( );
}
Person[ ] sortedList = new Person[ count ];
//copy references to sortedList
for (int i = 0; i < count; i++) {
sortedList[i] = entry[i];
}
Arrays.sort(sortedList,
getComparator(attribute));
return sortedList;
}
AddressBookVer2 Code
Program source file is too big to list here. From now on, we ask
you to view the source files using your Java IDE.

Directory:
Directory:

Chapter11/
Chapter11/

Source Files: AddressBookVer2.java
Source Files: AddressBookVer2.java
AddressBookVer3 Design
• In the previous two versions, we used an array
data structure to maintain a collection of Person
objects
• In the third version, we don't use an array at all.
• Instead, we use a Map from the Java Collection
Framework to maintain a collection of Person
objects.
• We use the person's name as the key of a Map
entry and the person object as the value of a Map
entry.
The sort Method
• We retrieve a collection of values in the map, converts it to
an array, and use the sort method of the java.util.Arrays
class to sort this array.
public Person[ ] sort ( int attribute ) {
if (!(attribute == Person.NAME ||
attribute == Person.AGE) ) {
throw new IllegalArgumentException( );
}
Person[ ] sortedList = new Person[entry.size()];
entry.values().toArray(sortedList);
Arrays.sort(sortedList,
getComparator(attribute));
return sortedList;
}
AddressBookVer3 Code
Program source file is too big to list here. From now on, we ask
you to view the source files using your Java IDE.

Directory:
Directory:

Chapter11/
Chapter11/

Source Files: AddressBookVer3.java
Source Files: AddressBookVer3.java

More Related Content

What's hot

What's hot (20)

Threads in python
Threads in pythonThreads in python
Threads in python
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Linked list
Linked listLinked list
Linked list
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Linked list
Linked listLinked list
Linked list
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Php array
Php arrayPhp array
Php array
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 

Viewers also liked

Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
Aakash deep Singhal
 
Pengenalan Masyarakat Sikh
Pengenalan Masyarakat Sikh Pengenalan Masyarakat Sikh
Pengenalan Masyarakat Sikh
Fadhil Ismail
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
Aakash deep Singhal
 
Bab 1 malaysia kesepaduan dalam kepelbagaian
Bab 1  malaysia kesepaduan dalam kepelbagaianBab 1  malaysia kesepaduan dalam kepelbagaian
Bab 1 malaysia kesepaduan dalam kepelbagaian
INSTAQLIM
 

Viewers also liked (20)

Software System Engineering - Chapter 5
Software System Engineering - Chapter 5Software System Engineering - Chapter 5
Software System Engineering - Chapter 5
 
Sorting
SortingSorting
Sorting
 
Software System Engineering - Chapter 2
Software System Engineering - Chapter 2Software System Engineering - Chapter 2
Software System Engineering - Chapter 2
 
Software System Engineering - Chapter 14
Software System Engineering - Chapter 14Software System Engineering - Chapter 14
Software System Engineering - Chapter 14
 
Software System Engineering - Chapter 15
Software System Engineering - Chapter 15Software System Engineering - Chapter 15
Software System Engineering - Chapter 15
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
 
Common Law Presentation (22034)
Common Law Presentation (22034)Common Law Presentation (22034)
Common Law Presentation (22034)
 
Pengenalan Masyarakat Sikh
Pengenalan Masyarakat Sikh Pengenalan Masyarakat Sikh
Pengenalan Masyarakat Sikh
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Isu-isu Kekeluargaan - Poligami
Isu-isu Kekeluargaan - PoligamiIsu-isu Kekeluargaan - Poligami
Isu-isu Kekeluargaan - Poligami
 
Hubungan Etnik Bab-5
Hubungan Etnik Bab-5Hubungan Etnik Bab-5
Hubungan Etnik Bab-5
 
Hubungan Etnik Bab-6
Hubungan Etnik Bab-6Hubungan Etnik Bab-6
Hubungan Etnik Bab-6
 
Hubungan Etnik Bab-2
Hubungan Etnik Bab-2Hubungan Etnik Bab-2
Hubungan Etnik Bab-2
 
Hubungan Etnik Bab 3
Hubungan Etnik Bab 3Hubungan Etnik Bab 3
Hubungan Etnik Bab 3
 
Bab 1 malaysia kesepaduan dalam kepelbagaian
Bab 1  malaysia kesepaduan dalam kepelbagaianBab 1  malaysia kesepaduan dalam kepelbagaian
Bab 1 malaysia kesepaduan dalam kepelbagaian
 
Hubungan Etnik Bab-4
Hubungan Etnik Bab-4Hubungan Etnik Bab-4
Hubungan Etnik Bab-4
 
Hubungan Etnik Bab-1
Hubungan Etnik Bab-1Hubungan Etnik Bab-1
Hubungan Etnik Bab-1
 

Similar to (Data Structure) Chapter11 searching & sorting

Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 

Similar to (Data Structure) Chapter11 searching & sorting (20)

Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Sorting
SortingSorting
Sorting
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Numeric Range Queries in Lucene and Solr
Numeric Range Queries in Lucene and SolrNumeric Range Queries in Lucene and Solr
Numeric Range Queries in Lucene and Solr
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 

More from Fadhil Ismail

Nota pengajian islam mpw 1143 t12
Nota pengajian islam mpw 1143 t12Nota pengajian islam mpw 1143 t12
Nota pengajian islam mpw 1143 t12
Fadhil Ismail
 
Nota pengajian islam mpw 1143 t11
Nota pengajian islam mpw 1143 t11Nota pengajian islam mpw 1143 t11
Nota pengajian islam mpw 1143 t11
Fadhil Ismail
 
Nota pengajian islam mpw 1143 t10
Nota pengajian islam mpw 1143 t10Nota pengajian islam mpw 1143 t10
Nota pengajian islam mpw 1143 t10
Fadhil Ismail
 

More from Fadhil Ismail (17)

Kertas Kerja Tenis
Kertas Kerja TenisKertas Kerja Tenis
Kertas Kerja Tenis
 
Contoh Borang Soal Selidik Poligami
Contoh Borang Soal Selidik PoligamiContoh Borang Soal Selidik Poligami
Contoh Borang Soal Selidik Poligami
 
Frontpage vs dreamweaver
Frontpage vs dreamweaverFrontpage vs dreamweaver
Frontpage vs dreamweaver
 
Software System Engineering - Chapter 13
Software System Engineering - Chapter 13Software System Engineering - Chapter 13
Software System Engineering - Chapter 13
 
Software System Engineering - Chapter 12
Software System Engineering - Chapter 12Software System Engineering - Chapter 12
Software System Engineering - Chapter 12
 
Software System Engineering - Chapter 11
Software System Engineering - Chapter 11Software System Engineering - Chapter 11
Software System Engineering - Chapter 11
 
Software System Engineering - Chapter 10
Software System Engineering - Chapter 10Software System Engineering - Chapter 10
Software System Engineering - Chapter 10
 
Software System Engineering - Chapter 9
Software System Engineering - Chapter 9Software System Engineering - Chapter 9
Software System Engineering - Chapter 9
 
Software System Engineering - Chapter 8
Software System Engineering - Chapter 8Software System Engineering - Chapter 8
Software System Engineering - Chapter 8
 
Software System Engineering - Chapter 7
Software System Engineering - Chapter 7Software System Engineering - Chapter 7
Software System Engineering - Chapter 7
 
Software System Engineering - Chapter 6
Software System Engineering - Chapter 6Software System Engineering - Chapter 6
Software System Engineering - Chapter 6
 
Software System Engineering - Chapter 4
Software System Engineering - Chapter 4Software System Engineering - Chapter 4
Software System Engineering - Chapter 4
 
Software System Engineering - Chapter 3
Software System Engineering - Chapter 3Software System Engineering - Chapter 3
Software System Engineering - Chapter 3
 
Software System Engineering - Chapter 1
Software System Engineering - Chapter 1Software System Engineering - Chapter 1
Software System Engineering - Chapter 1
 
Nota pengajian islam mpw 1143 t12
Nota pengajian islam mpw 1143 t12Nota pengajian islam mpw 1143 t12
Nota pengajian islam mpw 1143 t12
 
Nota pengajian islam mpw 1143 t11
Nota pengajian islam mpw 1143 t11Nota pengajian islam mpw 1143 t11
Nota pengajian islam mpw 1143 t11
 
Nota pengajian islam mpw 1143 t10
Nota pengajian islam mpw 1143 t10Nota pengajian islam mpw 1143 t10
Nota pengajian islam mpw 1143 t10
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

(Data Structure) Chapter11 searching & sorting

  • 2. Chapter 10 Objectives After you have read and studied this chapter, you should be able to • Perform linear and binary search algorithms on small arrays. • Determine whether a linear or binary search is more effective for a given situation. • Perform selection and bubble sort algorithms. • Describe the heapsort algorithm and show how its performance is superior to the other two algorithms. • Apply basic sorting algorithms to sort an array of objects.
  • 3. Searching • When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly. • Here’s the problem statement: Given a value X, return the index of X in the array, if such X exists. Otherwise, return NOT_FOUND (-1). We assume there are no duplicate entries in the array. • We will count the number of comparisons the algorithms make to analyze their performance. – The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. – Two separate performance analyses are normally done, one for successful search and another for unsuccessful search.
  • 4. Search Result Unsuccessful Search: search( 45 ) NOT_FOUND Successful Search: search( 12 ) 4 number 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77
  • 5. Linear Search • Search the array from the first to the last position in linear progression. public int linearSearch ( int[] number, int searchValue ) { int loc = 0; while (loc < number.length searchValue) { && number[loc] != loc++; } if (loc == number.length) { //Not found return NOT_FOUND; } else { return loc; } } //Found, return the position
  • 6. Linear Search Performance • We analyze the successful and unsuccessful searches separately. • We count how many times the search value is compared against the array elements. • Successful Search – Best Case – 1 comparison – Worst Case – N comparisons (N – array size) • Unsuccessful Search – Best Case = Worst Case – N comparisons
  • 7. Binary Search • If the array is sorted, then we can apply the binary search technique. number 0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 • The basic idea is straightforward. First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner.
  • 8. Sequence of Successful Search - 1 low #1 high 0 8 search( 44 ) mid 4  low + high  mid =   2   0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 low mid 38 < 44 high low = mid+1 = 5
  • 9. Sequence of Successful Search - 2 low #1 #2 high 0 5 8 8 search( 44 ) mid 4 6  low + high  mid =   2   0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 low high = mid-1=5 mid 44 < 77 high
  • 10. Sequence of Successful Search - 3 low #1 #2 #3 high mid search( 44 ) 0 5 5 8 8 5 4 6 5  low + high  mid =   2   0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 Successful Search!! 44 == 44 low high mid
  • 11. Sequence of Unsuccessful Search - 1 low #1 high 0 8 search( 45 ) mid 4  low + high  mid =   2   0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 low mid 38 < 45 high low = mid+1 = 5
  • 12. Sequence of Unsuccessful Search - 2 low #1 #2 high 0 5 8 8 search( 45 ) mid 4 6  low + high  mid =   2   0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 low high = mid-1=5 mid 45 < 77 high
  • 13. Sequence of Unsuccessful Search - 3 low #1 #2 #3 high mid search( 45 ) 0 5 5 8 8 5 4 6 5  low + high  mid =   2   0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 low high mid 44 < 45 low = mid+1 = 6
  • 14. Sequence of Unsuccessful Search - 4 low #1 #2 #3 #4 high mid search( 45 ) 0 5 5 6 8 8 5 5 4 6 5  low + high  mid =   2   0 1 2 3 4 5 6 7 8 5 12 17 23 38 44 77 84 90 high low Unsuccessful Search no more elements to search low > high
  • 15. Binary Search Routine public int binarySearch (int[] number, int searchValue) { int low high mid = 0, = number.length - 1, = (low + high) / 2; while (low <= high && number[mid] != searchValue) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if (low > high) { mid = NOT_FOUND; } return mid; }
  • 16. Binary Search Performance • Successful Search – Best Case – 1 comparison – Worst Case – log2N comparisons • Unsuccessful Search – Best Case = Worst Case – log2N comparisons • Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. • After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1, deriving K = log2N.
  • 17. Comparing N and log2N Performance Array Size 10 50 100 500 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Linear – N 10 50 100 500 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Binary – log2N 4 6 7 9 10 11 12 12 13 13 13 13 14 14
  • 18. Sorting • When we maintain a collection of data, many applications call for rearranging the data in certain order, e.g. arranging Person information in ascending order of age. • Here’s the problem statement: Given an array of N integer values, arrange the values into ascending order. • We will count the number of comparisons the algorithms make to analyze their performance. – The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. • We will compare different sorting algorithms by analyzing their worst-case performance.
  • 19. Selection Sort min first 1. Find the smallest element in the list. 2. Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. 3. Repeat Step 1 and 2 with the list having one less element (i.e., the smallest element is discarded from further processing). 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77 exchange 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted unsorted This is the result of one pass.
  • 20. Selection Sort Passes Pass # 1 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted 2 5 12 23 90 17 44 38 84 77 3 5 12 17 90 23 44 38 84 77 7 5 12 17 23 38 44 77 84 90 8 5 12 17 23 38 44 77 84 90 Result AFTER one Result AFTER one pass is completed. pass is completed.
  • 21. Selection Sort Routine public void selectionSort(int[] number) { int startIndex, minIndex, length, temp; length = number.length; for (startIndex = 0; startIndex <= length-2; startIndex++){ //each iteration of the for loop is one pass minIndex = startIndex; minIndex minIndex = i; //find the smallest in this pass at position for (int i = startIndex+1; i <= length-1; i++) { if (number[i] < number[minIndex]) } //exchange number[startIndex] and number[minIndex] temp = number[startIndex]; number[startIndex] = number[minIndex]; number[minIndex] = temp; } }
  • 22. Selection Sort Performance • We derive the total number of comparisons by counting the number of times the inner loop is executed. • For each execution of the outer loop, the inner loop is executed length – start times. • The variable length is the size of the array. Replacing length with N, the array size, the sum is derived as…
  • 23. Bubble Sort • With the selection sort, we make one exchange at the end of one pass. • The bubble sort improves the performance by making more than one exchange during its pass. • By making multiple exchanges, we will be able to move more elements toward their correct positions using the same number of comparisons as the selection sort makes. • The key idea of the bubble sort is to make pairwise comparisons and exchange the positions of the pair if they are out of order.
  • 24. One Pass of Bubble Sort 0 1 23 17 2 3 5 4 5 6 7 8 90 12 44 38 84 77 17 5 exchange 17 23 5 exchange 90 12 44 38 84 77 17 5 exchange 17 5 17 5 23 12 44 38 90 84 77 exchange 23 90 12 44 38 84 77 ok 23 12 44 90 38 84 77 17 5 exchange 23 12 90 44 38 84 77 exchange 23 12 44 38 84 90 77 exchange 17 5 23 12 44 38 84 77 90 The largest value 90 is at the end of The largest value 90 is at the end of the list. the list.
  • 25. Bubble Sort Routine public void bubbleSort(int[] number) { int boolean temp, bottom, i; exchanged = true; bottom = number.length - 2; while (exchanged) { exchanged = false; number[i]; for (i = 0; i <= bottom; i++) { if (number[i] > number[i+1]) { temp = //exchange number[i] = number[i+1]; number[i+1] = temp; exchanged = true; //exchange is made } } bottom--; } }
  • 26. Bubble Sort Performance • In the worst case, the outer while loop is executed N-1 times for carrying out N-1 passes. • For each execution of the outer loop, the inner loop is executed bottom+1 times. The number of comparisons in each successive pass is N-1, N-2, … , 1. Summing these will result in the total number of comparisons. • So the performances of the bubble sort and the selection sort are approximately equivalent. However, on the average, the bubble sort performs much better than the selection sort because it can finish the sorting without doing all N-1 passes.
  • 27. Heapsort • Selection and bubble sorts are two fundamental sorting algorithms that take approximately N2 comparisons to sort an array of N elements. • One sorting algorithm that improves the performance to approximately 1.5Nlog2N is called heapsort. • The heapsort algorithm uses a special data structure called a heap. • A heap structure can be implemented very effectively by using an array.
  • 29. Heap Constraints • A heap must satisfy the following two constraints: – Structural Constraint: Nodes in a heap with N nodes must occupy the positions numbered 0, 1, ..., N-1. – Value Relationship Constraint: A value stored in a node must be larger than the maximum of the values stored in its left and right children.
  • 30. Structural Constraints • Sample heaps and nonheaps that violate the structural constraints: Heaps Nonheaps
  • 31. Value Relationship Constraints • Sample heaps and nonheaps that violate the value relationship constraints: 45 45 25 25 Heaps 12 12 16 16 3 3 22 22 45 45 12 12 Nonheaps 58 58 25 25 12 12 22 22 45 45 25 25 33 33 34 23 34 23 24 24 13 16 13 16 12 12 45 45 16 16 3 3 35 35 34 34 11 22 11 22 9 9 45 45 55 55 90 90 22 22 55 55 3 3 12 12
  • 32. Heapsort Algorithm • How can we use the heap structure to sort N elements? • Heapsort is carried out in two phases: – Construction Phase: Construct a heap with given N elements. – Extraction Phase: Pull out the value in the root successively, creating a new heap with one less element after each extraction.
  • 33. Extraction Phase • After each extraction, a heap must be rebuilt with one less element. One sample extraction step: 0 1 2 3 4 5 6 7 8 0 1 2 3 4 1 84 84 7 17 17 8 23 23 1 84 77 23 84 77 23 2 44 44 4 12 12 Before 6 7 8 90 0 84 23 90 84 23 90 0 90 90 3 77 77 5 5 5 5 6 38 38 7 17 17 3 77 23 77 23 2 44 44 4 12 12 5 5 5 6 38 38 8 23 23 >23 max{17}? max{77, 12}? NO, so stop. max{84, 44}? YES,so swap. After
  • 34. Rebuild Steps • A sequence of rebuild steps to sort the list. 0 1 2 3 4 5 6 7 8 0 90 90 7 17 17 8 23 23 2 44 44 4 12 12 Before 1 12 2 17 3 23 4 5 38 44 6 77 7 8 84 90 0 84 90 38 77 44 12 17 23 5 12 17 23 38 44 77 84 90 5 1 84 84 3 77 77 0 5 5 5 5 6 38 38 1 2 Sorting was completed 17 84 5 12 23 77 38 44 5 17 84 5 12 23 77 44 38 5 after 8 rebuild steps. 3 4 5 12 17 12 5 77 23 12 12 5 77 17 23 7 8 17 23 17 23 Done 6 38 38
  • 35. Construction Phase • Rebuild steps are applied from position (N-2)/2  to position 0. 0 0 0 (9-2)/2  = 3 23 23 1 2 11 17 17 5 5 90 23 90 23 84 90 17 84 90 17 2 44 5 44 5 3 4 5 6 3 4 5 6 90 90 12 12 44 44 38 38 77 84 90 77 84 90 12 12 44 5 44 5 38 38 7 8 84 84 77 77 Before 7 8 17 84 17 84 23 77 23 77 Done
  • 36. Heap Implementation • We need to implement the abstract data structure heap into a concrete data structure. 0 90 90 1 84 84 0 2 44 44 1 2 3 4 5 90 84 44 77 12 5 3 4 5 12 12 5 5 7 8 38 17 23 6 77 77 6 38 38 7 8 17 17 23 23 Abstract Heap Structure Concrete Implementation
  • 37. The construct Method private void construct( ) { for (int i = (heap.length-2) / 2; i >= 0; i--) { current = i; done = false; child) { while ( !done ) { if ( current has no children ) { done = true; } else { if (current node < larger swap the two nodes; set current points to the larger child; } else { done = true; } } } } }
  • 38. The extract Method private void extract( ) { for (int size = heap.length-1; size >= 0; size--) { remove the root node data; move the last node to the root; done element = false; //rebuild the heap with one less while (!done) { if (current has no children) { done = true; } else { if (current node < larger child) { swap the two nodes; set current points to the larger child; } else { done = true; } } } } }
  • 39. Heapsort Performance • The total number of comparisons in the extraction phase is (N-1)×K where K is the depth of a heap. • We solve for K using the fact that the heap must satisfy the structural constraint: K 2i −1 = 2 K − 1 ∑ i =1 2K −1 − 1 < N ≤ 2K − 1 K =  log2 ( N + 1) Therefore, ( N − 1) × K ≅ N log2 N total # nodes in a heap with depth K this holds because of the structural constraint
  • 40. Heapsort Performance (cont'd) • There are N/2 rebuild steps in the construction phase. • Each rebuild step will take no more than K comparisons. • The total number of comparisons in the construction phase is approximately N log2 N 2 Therefore, the total number of comparisons for both phases is N log2 N + N log2 N = 1.5 N log2 N 2
  • 41. Problem Statement • Problem statement: Add the sorting capability to the AddressBook class from Chapter 10. The new AddressBook class will include a method that sort Person objects in alphabetical order of their names or in ascending order of their ages.
  • 42. Overall Plan • Instead of going through the development steps, we will present three different implementations. • The three versions are named AddressBookVer1, AddressBookVer2, and AddressBookVer3. • These classes will implement the AddressBook interface.
  • 43. The AddressBook Interface interface AddressBook { public void add(Person newPerson); public boolean delete(String searchName); public Person search(String searchName); public Person[ ] sort (int attribute); }
  • 44. AddressBookVer1 Design • We use an array of Person objects • We provide our own sorting method in the AddressBookVer1 class • We define the Person class so we can compare Person objects by name or by age
  • 45. Comparing Person Objects • First, we need to determine how to compare Person objects • The following does not make sense: Person p1 = …; Person p2 = …; if ( p1 < p2 ) { … }
  • 46. Modify the Person Class • Modify the Person class to include a variable that tells which attribute to compare. class Person { … public public public … public { static final int NAME = 0; static final int AGE = 1; static int compareAttribute = NAME; static void setCompareAttribute(int attribute) compareAttribute = attribute; } … }
  • 47. The compareTo Method • To compare Person objects, first set the comparison attribute and then call the compareTo Method. Person.setCompareAttribute (Person.NAME); public int compareTo(Person p) { int compResult = p1.compareTo(p2); AGE ) { if (compResult < 0) { //p1 “less than” p2 int compResult; if ( comparisonAttribute == p.getAge(); p2age ) { } else if (compResult == 0) { //p1 “equals” pw } else { //p2 “greater than” p2 } LESS; int p2age = if ( this.age < compResult = } … } else { //compare Name String p2Name = p.getName(); compResult = this.name. compareTo(p2Name); } return compResult;
  • 48. The sort Method public Person[ ] sort (int attribute) { Person[ ] sortedList = new Person[count]; Person p1, p2, temp; //copy references to sortedList for (int i = 0; i < count; i++) { sortedList[i] = entry[i]; } //Set the comparison attribute Person.setCompareAttribute(attribute); //begin the bubble sort on sortedList … } return sortedList; }
  • 49. The Use of sortedList in the sort Method
  • 50. AddressBookVer1 Code Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE. Directory: Directory: Chapter11/ Chapter11/ Source Files: AddressBookVer1.java Source Files: AddressBookVer1.java
  • 51. AddressBookVer2 Design • We use the java.util.Arrays class to sort an array of Person objects • The Person class does not include any comparison methods. Instead, we implement the Comparator interface. We can define the implementation class so we can compare Person objects using any combination of their attributes – For example, we can define a comparator that compares Person objects by using both name and age
  • 52. The AgeComparator Class • This class compares the age attributes of Person objects class AgeComparator implements Comparator { private final int LESS = -1; private final int EQUAL = 0; private final int MORE = 1; public int compare(Object p1, Object p2) { int comparisonResult; int p1age = ((Person)p1).getAge( ); int p2age = ((Person)p2).getAge( ); if (p1age < p2age) { comparisonResult = LESS; } else if (p1age == p2age) { comparisonResult = EQUAL; } else { assert p1age > p2age; comparisonResult = MORE; } return comparisonResult; } }
  • 53. The NameComparator Class • This class compares the age attributes of Person objects • Because the name attribute is a string we can use the compareTo method of the String class class NameComparator implements Comparator { public int compare(Object p1, Object p2) { String p1name = ((Person)p1).getName( ); String p2name = ((Person)p2).getName( ); return p1name.compareTo(p2name); } }
  • 54. The sort Method • We use the sort method of the java.util.Arrays class public Person[ ] sort ( int attribute ) { if (!(attribute == Person.NAME || attribute == Person.AGE) ) { throw new IllegalArgumentException( ); } Person[ ] sortedList = new Person[ count ]; //copy references to sortedList for (int i = 0; i < count; i++) { sortedList[i] = entry[i]; } Arrays.sort(sortedList, getComparator(attribute)); return sortedList; }
  • 55. AddressBookVer2 Code Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE. Directory: Directory: Chapter11/ Chapter11/ Source Files: AddressBookVer2.java Source Files: AddressBookVer2.java
  • 56. AddressBookVer3 Design • In the previous two versions, we used an array data structure to maintain a collection of Person objects • In the third version, we don't use an array at all. • Instead, we use a Map from the Java Collection Framework to maintain a collection of Person objects. • We use the person's name as the key of a Map entry and the person object as the value of a Map entry.
  • 57. The sort Method • We retrieve a collection of values in the map, converts it to an array, and use the sort method of the java.util.Arrays class to sort this array. public Person[ ] sort ( int attribute ) { if (!(attribute == Person.NAME || attribute == Person.AGE) ) { throw new IllegalArgumentException( ); } Person[ ] sortedList = new Person[entry.size()]; entry.values().toArray(sortedList); Arrays.sort(sortedList, getComparator(attribute)); return sortedList; }
  • 58. AddressBookVer3 Code Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE. Directory: Directory: Chapter11/ Chapter11/ Source Files: AddressBookVer3.java Source Files: AddressBookVer3.java

Editor's Notes

  1. Note: In the case of unsuccessful search, all elements in the array are compared against the search value, so the total number of comparisons is N. However, the actual number of times the while loop is executed is N+1 because we need one more go around to determine that loc becomes equal to number.length.
  2. Note: The value of log2N is a real number. Since the number of comparisons done is an integer, the result of log2N is rounded up to an integer in the third column.
  3. The figure shows the whole eight passes. The illustration for each pass shows the state right BEFORE the exchange was made for that pass. The illustrations in the slide show the state AFTER the exchange was made for that pass.
  4. What is the meaning behind the name “bubble” sort? Notice the effect of one pass is to migrate the largest element to the last position of the array. In addition, because we are exchanging the pairs if they are out of order, other elements migrate toward the end of the array. If we view the array vertically with the first position at the bottom and the last position at the top, the data movements we see is like bubbles moving toward the surface of water.
  5. The bubble sort routine is implemented by exploiting the following two properties: 1. After one pass through the array, the largest element will be at the end of the array. 2. During one pass, if no pair of consecutive entries is out of order, then the array is sorted.
  6. The selection sort will carry out N-1 passes no matter what. Whether the array is already sorted or very close to being sorted does not matter to the selection sort. It will carry out the same number of comparisons in sorting a given array. The bubble sort, on the other hand, can detect if the array is or has become sorted during the sorting process. So the bubble sort is adaptable, it does fewer comparisons for the arrays that are closer to being sorted than those that are not. In addition to the larger number of data movements during a single pass, the capability of checking the “sortedness” of the array enables the bubble sort routine to complete the whole sorting process much quicker than the selection sort. Indeed, the only situation that causes the bubble sort routine to execute the worst case number of comparisons is when the original array is in the reverse order (i.e., in descending order).
  7. We use integers as data values for the examples in this section. The topmost node is called the root of a heap. Nodes in a heap are indexed 0, 1, 2, and so forth in the top-to-bottom, left-to-right order starting from the root. A node in a heap has either zero, one, or two children. The children of a node are distinguished as the node’s left and right children. If a node has only one child, then it is the left child of the node.
  8. The heap structure can be characterized as an abstract data structure because the Java language (and others) does not include such data structure as a part of its language definition. An array is a concrete data structure that is a part of the Java language and the one which we can use effectively here to implement the abstract data structure heap. This slide shows the correspondence between the heap and the array representation. An important aspect in deciding which data structure to use is the ease of locating a given node’s left and right child. With an array implementation, we can locate any node’s left and right child easily. A node with index I has its left child at index 2ÞI + 1 and right child at index 2ÞI + 2.
  9. private void construct( ) { int current, maxChildIndex; boolean done; for (int i = (heap.length-2) / 2; i &gt;= 0; i--) { current = i; done = false; while ( !done ) {//perform one rebuild step //with the node at index i if ( 2*current+1 &gt; heap.length-1 ) { //current node has no children, so stop done = true; } else { //current node has at least one child, //get the index of larger child maxChildIndex = maxChild( current, heap.length-1 ); if ( heap[current] &lt; heap[maxChildIndex] ) { //a child is larger, so swap and continue swap( current, maxChildIndex ); current = maxChildIndex; } else { //the value relationship constraint //is satisfied, so stop done = true; } } } } testPrint( heap.length ); //TEMP }
  10. private void extract( ) { int current, maxChildIndex; boolean done; for (int size = heap.length-1; size &gt;= 0; size--) { //remove the root node data sortedList[size] = heap[ 0 ]; //move the last node to the root heap[ 0 ] = heap[size]; //rebuild the heap with one less element current = 0; done = false; while ( !done ) { if ( 2*current+1 &gt; size ) {//current node has no children, so stop done = true; } else { //current node has at least one child, //get the index of larger child maxChildIndex = maxChild( current, size ); if ( heap[current] &lt; heap[maxChildIndex] ) { //a child is larger, so swap and continue swap( current, maxChildIndex ); current = maxChildIndex; } else { //value relationship constraint //is satisfied, so stop done = true; } } } testPrint( size );//TEMP } }
  11. public int compareTo( Person person ) { int comparisonResult; if ( compareAttribute == AGE ) { int p2age = person.getAge( ); if (this.age &lt; p2age) { comparisonResult = LESS; } else if (this.age == p2age) { comparisonResult = EQUAL; } else { //this.age &gt; p2age comparisonResult = MORE; } } else { //compare names with String’s compareTo String p2name = person.getName( ); comparisonResult = this.name.compareTo( p2name ); } return comparisonResult; }
  12. public Person[ ] sort ( int attribute ) { Person[ ] sortedList = new Person[ count ]; Person p1, p2, temp; //copy references to sortedList; see Figure 10.17 for (int i = 0; i &lt; count; i++) { sortedList[i] = entry[i]; } //Set the comparison attribute Person.setCompareAttribute( attribute ); //begin the bubble sort on sortedList int bottom, i, comparisonResult; booleanexchanged = true; bottom = sortedList.length - 2; while ( exchanged ) { exchanged = false; for (i = 0; i &lt;= bottom; i++) { p1 = sortedList[i]; p2 = sortedList[i+1]; comparisonResult = p1.compareTo(p2, attribute); if ( comparisonResult &gt; 0 ) { //p1 is &apos;larger&apos; sortedList[i] = p2;//than p2,so sortedList[i+1] = p1;//exchange exchanged = true; //exchange is made } } bottom--; } return sortedList; }
  13. Please use your Java IDE to view the source files and run the program.
  14. Please use your Java IDE to view the source files and run the program.
  15. Please use your Java IDE to view the source files and run the program.