1. JSPM’s JSCOE ,HADAPSAR ,Pune-412207
Department of Computer Engineering
Unit 03: Sorting and Searching
Presented By: Prof .S.A. Patil
2. 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.
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 && number[loc] !=
searchValue) {
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.
• 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.
number
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
14. Sequence of Unsuccessful Search - 4
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )
low high mid
0 8
#1
2
high
low
mid
4
5 8
#2 6
5 5
#3 5
high low
6 5
#4
Unsuccessful Search
low > high
no more elements to search
15. Binary Search Routine
public int binarySearch (int[] number, int searchValue) {
int low = 0,
high = number.length - 1,
mid = (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.
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
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
min
first
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.
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;
//find the smallest in this pass at position
minIndex
for (int i = startIndex+1; i <= length-1; i++) {
if (number[i] < number[minIndex])
minIndex = i;
}
//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
The largest value 90 is at the end of
the list.
0 1 2 3 4 5 6 7 8
23 17 5 90 12 44 38 84 77
17 23 5 90 12 44 38 84 77
17 5 23 90 12 44 38 84 77
17 5 23 12 90 44 38 84 77
17 5 23 12 44 90 38 84 77
17 5 23 12 44 38 90 84 77
17 5 23 12 44 38 84 90 77
17 5 23 12 44 38 84 77 90
exchange
ok
exchange
exchange
exchange
exchange
exchange
exchange
25. Bubble Sort Routine
public void bubbleSort(int[] number) {
int temp, bottom, i;
boolean exchanged = true;
bottom = number.length - 2;
while (exchanged) {
exchanged = false;
for (i = 0; i <= bottom; i++) {
if (number[i] > number[i+1]) {
temp =
number[i]; //exchange
number[i] = number[i+1];
number[i+1] = temp;
exchanged = true; //exchange
is made
}
}
bottom--;
}
}
Editor's Notes
#6: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.
#17: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.
#20: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.
#24: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.
#25: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.