0% found this document useful (0 votes)
35 views

Data Structures: Deepak Varadam

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. Searching will be a key operation on the data searching sorted data is more efficient than searching unsorted data.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Data Structures: Deepak Varadam

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. Searching will be a key operation on the data searching sorted data is more efficient than searching unsorted data.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Foundation

Data Structures

Session delivered by:

Deepak Varadam

M.S. Ramaiah School of Advanced Studies, Bengaluru

Foundation

Session Objectives
To understand the basic sorting and searching methods

M.S. Ramaiah School of Advanced Studies, Bengaluru

Foundation

Topics
Sorting algorithms Searching algorithms

M.S. Ramaiah School of Advanced Studies, Bengaluru

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.

Sorting is typically undertaken for one of two reasons:

M.S. Ramaiah School of Advanced Studies, Bengaluru

Foundation

Sorting contd
There are three general methods for sorting arrays
Exchange Selection Insertion -Eg. Bubble sort -Eg. Selection sort -Eg. Insertion sort

M.S. Ramaiah School of Advanced Studies, Bengaluru

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

Foundation

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 dcab pass1 adcb pass2 abdc pass3 abcd 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.

M.S. Ramaiah School of Advanced Studies, Bengaluru

Foundation

Bubble Sort contd


The bubble sort always performs (n2-n) . It is derived from the fact that 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.

M.S. Ramaiah School of Advanced Studies, Bengaluru

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

Foundation

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 and selection sort is the same, the number of exchanges in the average case is far less for the selection sort. Initial pass1 pass2 pass3 dcab acdb abdc abcd

M.S. Ramaiah School of Advanced Studies, Bengaluru

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.

M.S. Ramaiah School of Advanced Studies, Bengaluru

11

Foundation

Insertion Sort contd


Unlike the bubble and selection sorts, the number of comparisons that occur during an insertion sort depends upon how 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 of n-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.

M.S. Ramaiah School of Advanced Studies, Bengaluru

12

Foundation

Searching

M.S. Ramaiah School of Advanced Studies, Bengaluru

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.

M.S. Ramaiah School of Advanced Studies, Bengaluru

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.

M.S. Ramaiah School of Advanced Studies, Bengaluru

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

16

Foundation

The Sequential Search


The sequential search is simple to code. The following function searches a integer array of known length until a match of the specified key is found. int sequential_search(int items[], int count, int key) { int t; for(t=0; t<count; ++t) if(key==items[t] return t; return -1; /*no match*/ }
M.S. Ramaiah School of Advanced Studies, Bengaluru 17

The Binary Search


The Binary Search has two preconditions:

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)

M.S. Ramaiah School of Advanced Studies, Bengaluru

18

The Binary Search


Initialization: low = 0, high = (n-1); Step One: Compute the midpoint: mid = (low + high) / 2 = 6

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

19

The Binary Search


Step Two:
Compute the midpoint: mid = (low + high) / 2 = (7 + 12)/2 = 9

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

20

The Binary Search


Step Three: Compute the midpoint: mid = (low + high) / 2 = (7 + 8)/2 = 7

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

21

Foundation

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;

0 10 17 21 26 30 35

6 42

7 47

8 59 high low mid

9 63 68 73

12 84

M.S. Ramaiah School of Advanced Studies, Bengaluru

22

The Binary Search


Initialization: low = 0, high = (n-1); Step One: Compute the midpoint: mid = (low + high) / 2 = 6

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

23

Foundation

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 array by setting low = mid+1
2 17 21 26 30

0 10

5 35

6 42 47 59 63 68 73

12 84

low

mid

high

M.S. Ramaiah School of Advanced Studies, Bengaluru

24

The Binary Search


Step Three: Compute the midpoint: mid = (low + high) / 2 = (3 + 5)/2 = 4

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

25

Foundation

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
0 10 17 2 21 3 26 high low mid 4 30 5 35 6 42 47 59 63 68 73 12 84

M.S. Ramaiah School of Advanced Studies, Bengaluru

26

Foundation

The Binary Search


Step Five: The value of the low index has exceeded the value of the high index, which tells us that the target is not present in the array, so the search ends with a failure

0 10 17

2 21

3 26

4 30

5 35

6 42 47 59 63 68 73

12 84

high

low

M.S. Ramaiah School of Advanced Studies, Bengaluru

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

M.S. Ramaiah School of Advanced Studies, Bengaluru

29

You might also like