This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

Data Structures

Session delivered by:

Deepak Varadam

© M.S. Ramaiah School of Advanced Studies, Bengaluru

1

Foundation

Session Objectives

• To understand the basic sorting and searching methods

© M.S. Ramaiah School of Advanced Studies, Bengaluru

2

S. Bengaluru 3 . Ramaiah School of Advanced Studies.Foundation Topics • • Sorting algorithms Searching algorithms © M.

Bengaluru 4 . Searching will be a key operation on the data searching sorted data is more efficient than searching unsorted data. Ramaiah School of Advanced Studies. 2. Sorted data can be presented in a meaningful manner.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. Sorting is typically undertaken for one of two reasons: © M. 1.S.

Ramaiah School of Advanced Studies. Bubble sort -Eg. Bengaluru 5 .Foundation Sorting cont’d • There are three general methods for sorting arrays – Exchange – Selection – Insertion -Eg. Selection sort -Eg. Insertion sort © M.S.

Foundation Bubble sort • It is the most well known sorting technique • It is an exchange sort • It involves repeated comparison and if necessary.S. 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. Bengaluru 6 . – The inner lop actually performs the comparisons and exchanges © M. Ramaiah School of Advanced Studies.

Initial dcab pass1 adcb pass2 abdc pass3 abcd With the bubble sort. Bengaluru 7 .S. • © M. Each pass is shown here. 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. Ramaiah School of Advanced Studies.Foundation Bubble Sort cont’d • To see how the bubble sort works. assume that the array to be sorted is dcab.

Bengaluru 8 . Ramaiah School of Advanced Studies. • • © M. 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.Foundation Bubble Sort cont’d • The bubble sort always performs ½(n2-n) .S. 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.

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. Bengaluru 9 . Ramaiah School of Advanced Studies.S.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 .

Bengaluru 10 . Initial pass1 pass2 pass3 dcab acdb abdc abcd © M. the number of exchanges in the average case is far less for the selection sort. Ramaiah School of Advanced Studies.S. 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.Foundation Selection Sort Cont’d • 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.

Foundation Insertion Sort • Insertion sort initially sorts the first two members of the array. © M.S. Bengaluru 11 . Ramaiah School of Advanced Studies. • 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.

• It leaves the order of the equal keys the same. the number of comparisons is n-1. Ramaiah School of Advanced Studies. It works the least when the array is already sorted and the hardest when the array is sorted in reverse order. © M.S.its performance is on the order of n-squared. Bengaluru 12 . ie.ow. • If the list is in order. This means that if a list is sorted by two keys. Insertion sort has two advantages • It behaves naturally. it remain sorted for both keys after an insertion sort. the number of comparisons that occur during an insertion sort depends upon how the list is initially ordered.Foundation Insertion Sort cont’d • Unlike the bubble and selection sorts.

S. Bengaluru 13 .Foundation Searching © M. Ramaiah School of Advanced Studies.

Generally.Foundation Searching • Searching is the process of traversing through a set of records looking for a specific key value. Ramaiah School of Advanced Studies.S. – the rest of the record associated with that key. Bengaluru 14 . Often. – a pointer to the rest of the record associated with that key. the search will also return one (or more) of the following: – the location of the target key value within the data set. the purpose of the search is to report whether or not the target key is present. © M.

• Searching can be conducted on ordered (sorted) data.S.Foundation Searching • Searching can be conducted on unordered data. then it is worth the overhead to sort data first. © M. This is usually the least efficient searching method. Bengaluru 15 . • If it is known that searching is a major and frequently invoked task. • Searching sorted data is generally far more efficient than searching unordered data. Ramaiah School of Advanced Studies.

However. step through each item one at a time.e. • 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. Ramaiah School of Advanced Studies. • The Binary Search is a highly efficient searching algorithm.S.Foundation Searching Techniques • The sequential search is basically a loop – starting at the beginning of the set. Bengaluru 16 . it has two preconditions: – The data must be stored in a directly indexable structure (i. stopping if the target is found.. an array) – The data must be ordered.

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

the higher index positions) © M. Bengaluru 18 . an array) • The data must be ordered. repeat the search in the “bottom half” of the array (i... the lower index positions) • If the midpoint value is smaller than the target.e.S.e.e. • 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. Ramaiah School of Advanced Studies.The Binary Search • The Binary Search has two preconditions: Foundation • The data must be stored in a directly indexable structure (i. repeat the search in the “top half” of the array (i..

Ramaiah School of Advanced Studies. so not found. 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. • Check the midpoint value against the target: a[mid] is 42. and a search target of 59.S.The Binary Search Initialization: low = 0. Step One: • Compute the midpoint: mid = (low + high) / 2 = 6 Foundation Consider an array with 13 entries. • If not found. in index positions 0 through 12. Bengaluru 19 . high = (n-1). decide where to repeat: a[mid] (42) is less than the target (59).

so not found. Ramaiah School of Advanced Studies. Bengaluru 20 . decide where to repeat: a[mid] (63) is greater than the target (59).S. • If not found. 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.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.

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. decide where to repeat: a[mid] (47) is less than the target (59). • If not found. Bengaluru 21 . so not found.S. 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. Ramaiah School of Advanced Studies.

S. so target found. Bengaluru 22 . 0 10 17 21 26 30 35 6 42 7 47 8 59 high low mid 9 63 68 73 12 84 © M.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. Ramaiah School of Advanced Studies.

• If not found. Ramaiah School of Advanced Studies.The Binary Search Initialization: low = 0. say 28 • Check the midpoint value against the target: a[mid] is 42. decide where to repeat: a[mid] (42) is greater than the target (28). high = (n-1). so not found.S. Step One: • Compute the midpoint: mid = (low + high) / 2 = 6 Foundation • Consider the same array. Bengaluru 23 . 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. but this time search for a target that is not present.

decide where to repeat: a[mid] (21) is less than the target (28). • If not found. Ramaiah School of Advanced Studies. so not found.S. Bengaluru 24 .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 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.

Ramaiah School of Advanced Studies. so not found • If not found. decide where to repeat: a[mid] (30) is greater than the target (28). Bengaluru 25 .S. 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.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. Ramaiah School of Advanced Studies. Bengaluru 26 .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. decide where to repeat: a[mid] (26) is less than the target (28).S. 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.

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. which tells us that the target is not present in the array. Ramaiah School of Advanced Studies.S.Foundation The Binary Search Step Five: The value of the low index has exceeded the value of the high index. Bengaluru 27 .

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. Ramaiah School of Advanced Studies. • Sorted data can be presented in a meaningful manner • There are three general methods for sorting arrays: Bubble Sort.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.S. Bengaluru 28 . © M.

S. Bengaluru 29 . Ramaiah School of Advanced Studies.Foundation Thank You © M.

- Unit28_SearchingAndSorting3 (1)
- L11 - Sorting
- An Efficient Approach of Fast External Sorting Algorithm in Data Warehouse
- Algorithm *Fit
- Start - Up C Programs Write a Program
- C programming Lesson on One-Dimensional Arrays
- ADA_Lab
- Data structures and algorithms -Heaps
- Gpu Recipe
- 01(1)
- Writing Test Bench
- Javase Array
- c all pgms
- daa
- qp_iyear_isem_2011_c_programming
- C faqs
- Appliness #9 – December 2012
- Slides
- LEARN PHP
- Dynamic Memory Allocation
- Bas
- 0910SEM1-CG1101
- Wipro Placement Aptitude Test Paper
- Mwore Ember JsMwore Ember JsMwore Ember JsMwore Ember JsMwore Ember JsMwore Ember Js
- Programming in C Language Question and Answers
- Workbook-CS 2202 Foundations of Computer Science Wk 1-6
- 01 Matlab Intro
- Questions to Consider When Selecting an All-flash Array
- Microphone Array Processing for Distant Speech Recognition
- Change Log

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd