Data Structures: Deepak Varadam
Data Structures: Deepak Varadam
Data Structures
Deepak Varadam
Foundation
Session Objectives
To understand the basic sorting and searching methods
Foundation
Topics
Sorting algorithms Searching algorithms
Foundation
Sorting
Sorting is the process of arranging a set of similar information into an increasing or decreasing order. Sorting algorithms have also been extensively analyzed and are well understood. 1. 2. 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.
Foundation
Sorting contd
There are three general methods for sorting arrays
Exchange Selection Insertion -Eg. Bubble sort -Eg. Selection sort -Eg. Insertion sort
Foundation
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
Foundation
Foundation
Foundation
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
Foundation
10
Foundation
Insertion Sort
Insertion sort initially sorts the first two members of the array. Next it inserts the third member into its sorted position in relation to the first two members. Then it inserts the fourth element and so on The process continues until all elements have been sorted.
11
Foundation
12
Foundation
Searching
13
Foundation
Searching
Searching is the process of traversing through a set of records looking for a specific key value. Generally, the purpose of the search is to report whether 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; a pointer to the rest of the record associated with that key.
14
Foundation
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 invoked task, then it is worth the overhead to sort data first.
15
Foundation
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 most two 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 at most n children
16
Foundation
Foundation
The data must be stored in a directly indexable structure (i.e., an array) 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 in the top half of the array (i.e., the lower index positions) If the midpoint value is smaller than the target, repeat the search in the bottom half of the array (i.e., the higher index positions)
18
Foundation
Consider an array with 13 entries, in index positions 0 through 12, and a search target of 59.
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 setting low = mid+1
0 10 low 17 21 26 30 35 6 42 mid 47 59 63 68 73 12 84 high
19
Foundation
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
0 10 17 21 26 30 35 6 42 7 47 59 9 63 68 73 12 84
low
mid
high
20
Foundation
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 setting low = mid+1
0 10 17 21 26 30 35 6 42 7 47 low mid 8 59 high 9 63 68 73 12 84
21
Foundation
0 10 17 21 26 30 35
6 42
7 47
9 63 68 73
12 84
22
Foundation
Consider the same array, but this time search for a target that is not present, say 28
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 greater than the target (28), so the search is repeated in the lower end of the array by setting high = mid-1
0 10 low 17 21 26 30 35 6 42 mid 47 59 63 68 73 12 84 high
23
Foundation
0 10
5 35
6 42 47 59 63 68 73
12 84
low
mid
high
24
Foundation
Check the midpoint value against the target: a[mid] is 30, so not found If not found, decide where to repeat: a[mid] (30) is greater than the target (28), so the search is repeated in the lower end of the array by setting high = mid-1
0 10 17
2 21
3 26 low
4 30 mid
5 35 high
6 42 47 59 63 68 73
12 84
25
Foundation
26
Foundation
0 10 17
2 21
3 26
4 30
5 35
6 42 47 59 63 68 73
12 84
high
low
27
Foundation
Summary
Sorting 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.
M.S. Ramaiah School of Advanced Studies, Bengaluru 28
Foundation
Thank You
29