c language session 4
TRANSCRIPT
-
7/31/2019 C Language Session 4
1/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Data Structures
Session delivered by:Session delivered by:
Deepak VaradamDeepak Varadam
1
-
7/31/2019 C Language Session 4
2/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Session Objectives To understand the basic sorting and searching methods
2
-
7/31/2019 C Language Session 4
3/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Topics Sorting algorithms Searching algorithms
3
-
7/31/2019 C Language Session 4
4/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Sorting Sorting is the process ofarranging a set of similar information into
an increasing or decreasing order. Sorting algorithms have also been extensively analyzed and are well
understood.
Sorting is typically undertaken for one of two reasons:
1. Searching will be a key operation on the data searching sorteddata is more efficient than searching unsorted data.
2. Sorted data can be presented in a meaningfulmanner.
4
-
7/31/2019 C Language Session 4
5/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Sorting contd
There are three general methods for sorting arrays Exchange -Eg. Bubble sort
Selection -Eg. Selection sort
Insertion -Eg. Insertion sort
5
-
7/31/2019 C Language Session 4
6/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Bubble sort It is the most well known sorting technique
It is an exchange sort
It involves repeated comparison and if necessary, the exchange of
adjacent elements
The elements are like bubbles in a tank of water-each seeks its
own level It is driven by two loops
Given that there are count elements in the array.
The outer loop causes the array to be scanned count-1 times and ensures
that in the worst case, every element is in its proper position when the
function terminates. The inner lop actually performs the comparisons and exchanges
6
-
7/31/2019 C Language Session 4
7/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Bubble Sort contd To see how the bubble sort works, assume that the array to be sorted is
dcab. Each pass is shown here.Initial d c a b
pass1 a d c b
pass2 a b d c
pass3 a b c d
With the bubble sort, the number of comparisons is always the same
because the two for loops repeat the specified number of times
whether the list is initially ordered or not.
7
-
7/31/2019 C Language Session 4
8/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Bubble Sort contd
The bubble sort always performs (n2
-n) . It is derived from the factthat the outer loop executes n-1 times and the inner loop executes an
average of n/2 times.
An n-squared algorithm is ineffective when applied to large no of
elements because the execution time grows exponentially relative to
the no of elements(for the average and worst case) The no of exchanges is zero for the best case.
8
-
7/31/2019 C Language Session 4
9/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Selection sort A selection sort selects the element with the lowest value and
exchange it with the first element From the remaining n-1 elements ,the element with the smallest
key is found and exchanged with the second element and so forth
The exchanges continue to the last two elements
9
-
7/31/2019 C Language Session 4
10/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Selection Sort Contd As with the bubble sort the outer loop executes n-1 times and the
inner loop averages n/2 times As a result it requires1/2(n2-n) comparisons
This is an n squared algorithm, which makes it too slow for
sorting a large number of items
Although the number of comparisons for both bubble sort andselection sort is the same, the number of exchanges in the average
case is far less for the selection sort.
Initial d c a b
pass1 a c d bpass2 a b d c
pass3 a b c d
10
-
7/31/2019 C Language Session 4
11/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Insertion Sort
Insertion sort initially sorts the first two members of the array.
Next it inserts the third member into its sorted position in relationto the first two members. Then it inserts the fourth element and so
on
The process continues until all elements have been sorted.
11
-
7/31/2019 C Language Session 4
12/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Insertion Sort contd
Unlike the bubble and selection sorts, the number of
comparisons that occur during an insertion sort depends uponhow the list is initially ordered.
If the list is in order, the number of comparisons is n-1;ow.its
performance is on the order ofn-squared.
Insertion sort has two advantages It behaves naturally. ie. It works the least when the array is
already sorted and the hardest when the array is sorted in
reverse order.
It leaves the order of the equal keys the same. This means that
if a list is sorted by two keys, it remain sorted for both keys
after an insertion sort.
12
-
7/31/2019 C Language Session 4
13/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Searching
13
-
7/31/2019 C Language Session 4
14/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Searching
Searching is theprocess of traversing through a set of recordslooking for
a specific key value. Generally, the purpose of the search is to reportwhether or not the target key is present. Often, the search will also return
one (or more) of the following:
the location of the target key value within the data set;
the rest of the record associated with that key;
apointerto the rest of the record associated with that key.
14
F d i
-
7/31/2019 C Language Session 4
15/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Searching Searching can be conducted on unordered data. This is usually
the least efficient searching method. Searching can be conducted on ordered (sorted) data.
Searching sorted data is generally far more efficient than
searching unordered data.
If it is known that searching is a major and frequently invokedtask, then it is worth the overhead to sort data first.
15
F d ti
-
7/31/2019 C Language Session 4
16/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
Searching Techniques The sequential search is basically a loop starting at the
beginning of the set, step through each item one at a time,stopping if the target is found.
The Binary Search is a highly efficient searching algorithm.However, it has two preconditions:
The data must be stored in a directly indexable structure (i.e.,
an array) The data must be ordered.
Binary search tree: Each node has one key and at mosttwo children
Trinary search tree: Each node has one or two keys and at
most three children n-ary search tree: Each node has up to (n-1) keys and atmost n children
16
F d ti
-
7/31/2019 C Language Session 4
17/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Sequential Search
The sequential search is simple to code.
The following function searches a integer array of known lengthuntil a match of the specified key is found.
int sequential_search(int items[], int count, int key)
{int t;
for(t=0; t
-
7/31/2019 C Language Session 4
18/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
The Binary Search has two preconditions:
The data must be stored in a directly indexable structure (i.e., anarray)
The data must be ordered.
The Binary Search works:
Compute the middle point of the array Compare the value at the midpoint against the target
If the midpoint value is greater than the target, repeat the search inthe top half of the array (i.e., the lower index positions)
If the midpoint value is smaller than the target, repeat the search inthe bottom half of the array (i.e., the higher index positions)
18
Foundation
-
7/31/2019 C Language Session 4
19/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary SearchConsider an array with 13 entries, in index positions 0 through 12, and a
search target of59.
Initialization:
low = 0, high = (n-1);
Step One:
Compute the midpoint: mid = (low + high) / 2 = 6 Check the midpoint value against the target: a[mid] is 42, so not found;
If not found, decide where to repeat: a[mid] (42) is less than the target(59), so the search is repeated in the upper end of the array by settinglow = mid+1
low highmid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 126
19
Foundation
-
7/31/2019 C Language Session 4
20/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Step Two:
Compute the midpoint: mid = (low + high) / 2 = (7 + 12)/2 = 9 Check the midpoint value against the target: a[mid] is 63, so not found;
If not found, decide where to repeat: a[mid] (63) is greater than the target
(59), so the search is repeated in the lower end of the array by setting high
= mid-1
low highmid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 126 97
20
Foundation
-
7/31/2019 C Language Session 4
21/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Step Three:
Compute the midpoint: mid = (low + high) / 2 = (7 + 8)/2 = 7
Check the midpoint value against the target: a[mid] is 47, so not found;
If not found, decide where to repeat: a[mid] (47) is less than the target
(59), so the search is repeated in the upper end of the array by settinglow = mid+1
low highmid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 126 97 8
21
Foundation
-
7/31/2019 C Language Session 4
22/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Step Four:
Compute the midpoint: mid = (low + high) / 2 = (8 + 8)/2 = 8
Check the midpoint value against the target: a[mid] is 59, so target
found;
low
high
mid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 126 97 8
22
Foundation
-
7/31/2019 C Language Session 4
23/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Consider the same array, but this time search for a target that is notpresent, say 28
Initialization:
low = 0, high = (n-1);
Step One:
Compute the midpoint: mid = (low + high) / 2 = 6
Check the midpoint value against the target: a[mid] is 42, so notfound;
If not found, decide where to repeat: a[mid] (42) is greater than thetarget (28), so the search is repeated in the lower end of the array by
setting high = mid-1
low highmid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 126
23
Foundation
-
7/31/2019 C Language Session 4
24/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Step Two:
Compute the midpoint: mid = (low + high) / 2 = (0 + 5)/2 = 2
Check the midpoint value against the target: a[mid] is 21, so not
found;
If not found, decide where to repeat: a[mid] (21) is less than the
target (28), so the search is repeated in the upper end of the arrayby setting low = mid+1
low highmid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 12652
24
Foundation
-
7/31/2019 C Language Session 4
25/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Step Three:
Compute the midpoint: mid = (low + high) / 2 = (3 + 5)/2 = 4
Check the midpoint value against the target: a[mid] is 30, so notfound
If not found, decide where to repeat: a[mid] (30) is greater than thetarget (28), so the search is repeated in the lower end of the array bysetting high = mid-1
low highmid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 1262 53 4
25
Foundation
-
7/31/2019 C Language Session 4
26/29
Foundation
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Step Four:
Compute the midpoint: mid = (low + high) / 2 = (3 + 3)/2 = 3
Check the midpoint value against the target: a[mid] is 26, so not
found
If not found, decide where to repeat: a[mid] (26) is less than the
target (28), so repeat the search in the upper end of the array by
setting low = mid+1
low
high
mid
10 17 21 26 30 35 42 47 59 63 68 73 84
0 12642 3 5
26
Foundation
-
7/31/2019 C Language Session 4
27/29
M.S. Ramaiah School of Advanced Studies, Bengaluru
The Binary Search
Step Five: The value of the low index has exceeded the value of thehigh index, which tells us that the target is not present in the array, so
the search ends with a failure
lowhigh
10 17 21 26 30 35 42 47 59 63 68 73 84
0 12642 3 5
27
Foundation
-
7/31/2019 C Language Session 4
28/29
M.S. Ramaiah School of Advanced Studies, Bengaluru
SummarySorting is typically undertaken for one of two reasons:
Searching will be a key operation on the data (searching sorted
data is more efficient than searching unsorted data;
Sorted data can be presented in a meaningful manner
There are three general methods for sorting arrays: Bubble Sort,
Selection Sort and Insertion Sort
Searching is the process of traversing through a set of records
looking for a specific key value
The Binary Search has two preconditions:The data must be stored in a directly indexable structure (i.e.,
an array)
The data must be ordered.
28
Foundation
-
7/31/2019 C Language Session 4
29/29
M.S. Ramaiah School of Advanced Studies, Bengaluru
Thank You
29