5 - Sorting and Searching Algorithm
5 - Sorting and Searching Algorithm
http://visualgo.net/
Go to the link for course materials:
https://drive.google.com/open?id=0B88ti1PlrP6qRjVmSkxsOXJGcXM
Scope
• Several sorting algorithms, including:
– bubble sort
– selection sort
– insertion sort
– counting sort
•Searching, includes:
– linear search
– binary search algorithms
2
Sorting
Process to convert the Unordered list to Ordered list.
Index 0 1 2 3 4
Unordered 21 20 27 19 16
list
Index 0 1 2 3 4
Ordered 16 19 20 21 27
list
Ascending order
Descending order
3
Bubble Sort
Concept:
Compare 1st two elements
If out of order, exchange them to put in order
Move down one element, compare 2nd and 3rd
elements, exchange if necessary. Continue until end
of array.
Pass through array again, exchanging as necessary
Repeat until pass made with no exchanges.
4
Step-by-step example
First Pass:( 5 1 4 2 8 )
( 1 5 4 2 8 ), Swaps since 5 > 1. move on
( 1 4 5 2 8 ), Swap since 5 > 4. move on
( 1 4 2 5 8 ), Swap since 5 > 2. move on
( 1 4 2 5 8 ), does not swap. End of pass#1
2nd Pass: ( 1 4 2 5 8 )
( 1 4 2 5 8 ) does not swap. move on
( 1 2 4 5 8 ), Swap since 4 > 2. move on
( 1 2 4 5 8 ) does not swap. move on
( 1 2 4 5 8 ) does not swap. End of pass#2
5
Now, the array is already sorted, but the algorithm
does not know if it is completed. The algorithm
needs one whole pass without any swap to know it is
sorted.
Third Pass:( 1 2 4 5 8 )
(12458)
(12458)
( 1 2 4 5 8 ) Terminate.
6
Bubble Sort Function
8
Bubble SortFunction (re-define)
void SWAP(int *a,int *b) { int t; t=*a;
*a=*b; *b=t; }
void bubble( int a[], int n ) {
int pass, j, flag;
for(pass=1;pass<n;pass++) {//break if no swap
flag = 0;
for(j=0;j<(n-pass);j++) { //discard the
last
if( a[j]>a[j+1] ) {
SWAP(&a[j+1],&a[j]); flag = 1;}
}
if (flag==0) break;
} 9
}
Bubble Sort - Tradeoffs
Benefit:
Easy to understand and implement
Disadvantage:
Inefficient: slow for large arrays
1-10
Selection Sort
Concept for sort in ascending order:
Locate smallest element in array.
Exchange it with element in position 0
Locate next smallest element in array.
Exchange it with element in position 1.
Continue until all elements are arranged in
order
1-11
Selection Sort - Example
11 2 29 3
12
Example (Continued)
2. Next smallest element is 3. Exchange 3 with
element in 2nd position in array:
2 3 29 11
2 3 11 29
13
For 4 elements:
How many positions were selected?
14
Min Min
value Index
8 0
5 1
1 3
5 1
3 5
7 2
5 5
8 3
7 5
9 4
8 5
15
A Selection Sort Function:
void selectionSort(int array[], int n)
{
int select, minIndex, minValue;
Disadvantage:
May not be as easy as Bubble Sort to
understand
1-17
Insertion Sort
18
Pseudocode of Insertion Sort
19
Insertion Sort
To sort array A[0..n-1], sort A[0..n-2] recursively and
then insert A[n-1] in its proper place among the sorted
A[0..n-2]
Usually implemented bottom up (non-recursively)
Example: 6, 4, 1, 8, 5
0 1 2 3 4
6 |4 1 8 5
4 6|1 8 5
1 4 6|8 5
1 4 6 8|5
1 4 5 6 8
20
Example: The following table shows the steps for
sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each
step, the item under consideration is underlined. The
item that was moved (or left in place because it was
biggest yet considered) in the previous step is shown
in bold.
37495261
37495261
34795261
34795261
34579261
23457961
23456791
12345679 21
Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It
works by counting the number of objects having distinct key values. Then
doing some arithmetic to calculate the position of each object in the output
sequence
Points to be noted:
1. Counting sort is efficient if the range of input data is not significantly greater
than the number of objects to be sorted. Consider the situation where the input
sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
2. It is not a comparison based sorting. It running time complexity is O(n) with
space proportional to the range of data.
3. Counting sort uses a partial hashing to count the occurrence of the data
object in O(1).
4. Counting sort can be extended to work for negative inputs also.
22
23
Introduction to Search Algorithms
25
Linear Search - Example
17 23 5 11 2 29 3
Searching for the the value 11
linear search examines 17, 23, 5, and 11 then
stop with getting 11 at index 3 in the
list.
Searching for the the value 7
linear search examines 17, 23, 5, 11, 2, 29,
and 3 then stop without getting 7 in the
list.
26
Linear Search
Algorithm:
Start search operation for 0 index to last index
Compare the item to the first element of array
If item is equal to the array element stop search
and send the found message with the index.
If item is not found continue the comparison with
the next element by increasing the index by 1
until the last element.
27
A Linear Search Function
Disadvantages:
Inefficient (slow): for array of N elements,
examines N/2 elements on average for value
in array, N elements for value not in array
1-29
Binary Search
31
Binary Search
Disadvantages:
Requires that array elements be sorted
1-34
Ref. link
1. http://visualgo.net/
2. http://www.comp.nus.edu.sg/~stevenha/visualization/index.html
3. http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort-
example-300px.gif
4. https://www.youtube.com/watch?v=JP5KkzdUEYI
5. http://en.wikipedia.org/wiki/Selection_sort#mediaviewer/File:Selection-
Sort-Animation.gif
35