sorting and searching by dr p.padmanabham professor (cse)&director bharat institute of...

30
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 [email protected]

Upload: mavis-nash

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Sorting and Searching

byDr P.Padmanabham

Professor (CSE)&Director Bharat Institute

of Engineering &Technology Hyderabad

Mobile 9866245898 [email protected]

Page 2: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

What is searching?

• In computer science search is a method for finding a particular value in a list.

• The value against which the search is made is known as “Search Key”.

• Normally the list is a set of records. For Example: the records of employees in an organization or students records.

Page 3: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

What is searching? Contd..

• Search-key (usually referred as Key) may be to search for unique record or duplicate records.

• The search stops when the required record is found in the case of unique record and the search continues till the end of data if the record is not unique; for example you are searching for all employees above 40 years of age.

Page 4: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Linear Search on unsorted data.

• Linear search or sequential search is a method for finding a particular record matching the key by examining each record sequentially till the record is found or till the end of data (unsuccessful search).

• In an unordered list; we travel down the list until we find the element or reach the end –unsuccessful search.

Page 5: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Ordered or sorted data

• In an ordered list the sequential search is stopped when either the record is found or when the key value of the current record exceeds the search key value –unsuccessful search.

• The linear or sequential search is normally used for unsorted lists as a better (faster) searching technique known as “Binary Search” is used for ordered data.

Page 6: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Assumption made for sorting and searching programs

• All key values are assumed to be integers.• All Keys are assumed to be available in an

array in the memory.• The search or sorting technique will be

programmed on this array.• In reality the search or sorting key will be

apart of the data record.

Page 7: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Why search?

• Search a particular record to 1. display the contents 2. to modify the record 3. to delete the record 4. to insert a new record.• In this course we will limit our study to only search as

the other functions need the knowledge of data structures (like dictionaries, binary search trees, hash tables etc) which will be covered in the next course.

Page 8: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Linear Search in unordered data

• A linear search in a set of n elements takes n comparisons for an unsuccessful search.

• The worst case (unsuccessful search) time complexity of this search is denoted by O(n) - in other words saying as Order of n.

Page 9: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Binary Search

• Binary search is one of the fundamental algorithms in computer science.

• The search technique works on only sorted data.

• In order to explore it, we'll first examine an example.

Page 10: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Binary search Example• consider the following sequence of integers

sorted in ascending order and say we are looking for the number 55:

5 7 15 41 68 72 98The search starts at middle (41) . Since the

key(55) is greater than 41 the search should continue in the subset

68 72 98Once again the search stars at the middle(72).

Page 11: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

s

Page 12: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Time complexity of Binary search• In the previous Example we have seen in 7

elements the number of comparisons in worst case are 3 which is equal to ceil of log(7) to base 2.

• For n elements it is easy to see that the order of complexity is ceil(log(n) to base 2).

• For 1000 elements the worst case comparisons are ceil(log1000 to base 2) which is equal to 10 and for a linear search it is equal to 1000

Page 13: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

What is Sorting?

• A set of records are said to be sorted on key k[] (in ascending or non-descending order)if and only if k[i]<k[j] for every i and j i<j , where k[i] & k[j] are the keys of records i & j.

• The default sorting order is ascending order or alphabetical order.

Page 14: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Classification of sorting techniques

1. Exchange ex: bubble sort and quick sort2. Selection ex: simple selection sort, heap sort3. Insertion ex: straight insertion sort, Shell sort4. Merge ex: ex: Two way merge sort5. Distribution ex: Radix and bucket sorts

Page 15: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Sorting algorithms that we will study

1. Bubble Sort2. Selection sort (simple)3. Insertion sort (straight)4. Quick sort5. Merge sort.

Page 16: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Bubble Sort

• To sort n elements bubble sort makes n-1 passes through the data exchanging adjacent elements if the first one is greater than the next in every pass eliminating the highest element bubbled out.

Page 17: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Bubble sort example

• Consider the following set of numbers 12 67 45 32 90 25• Pass 1: 12 45 32 67 25 90• Pass 2: 12 32 45 25 67 90• Pass 3: 12 32 25 45 67 90• Pass 4: 12 25 32 45 67 90• Pass 5: 12 25 32 45 67 90

Page 18: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Time complexity of bubble sort

• The number of comparisons made in each pass on n elements is as follows:

Pass 1 n-1 comparisons, pass 2 n-2 comparisons, … etc , pass n-2 2 comparisons

pass n-1 1 comparison.Total comparisons are n*(n-1)/2 which is a

polynomial of degree 2.Therefore the time complexity is O(n*n );

Page 19: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Selection sort (simple)

• Like bubble sort selection sort also takes n-1 passes through the data.

• In each pass it selects the maximum among the reaming elements and puts it in the correct place

Page 20: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Example of selection sort

• Consider the following set of numbers 12 67 45 32 90 25• Pass 1: 12 67 45 32 25 90• Pass 2: 12 25 45 32 67 90• Pass 3: 12 25 32 45 67 90• Pass 4: 12 25 32 45 67 90• Pass 5: 12 25 32 45 67 90

Page 21: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Insertion sort

• Simple implementation• Efficient for data sets that are already

substantially sorted.• More efficient in practice than most other

simple quadratic (O(n2)) algorithms such as selection sort or bubble sort;

• The best case (nearly sorted input) is O(n)• Stable i.e., does not change the relative order

of elements with equal keys

Page 22: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

• In every pass of insertion sort removes an element from the input data, inserting it into the correct position in the already-sorted list, until no input elements remain.

• Sorting is typically done in-place.

Page 23: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Example of insertion sort

• given set : 66 91 125 15 230 34 78 89 90• end of pass 1 : 66 91 125 15 230 34 78 89 90• end of pass 2 : 66 91 125 15 230 34 78 89 90• end of pass 3 : 15 66 91 125 230 34 78 89 90• end of pass 4 : 15 66 91 125 230 34 78 89 90• end of pass 5 : 15 34 66 91 125 230 78 89 90• end of pass 6 : 15 34 66 78 91 125 230 89 90• end of pass 7 : 15 34 66 78 89 91 125 230 90• end of pass 8 : 15 34 66 78 89 90 91 125 230

Page 24: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Example of insertion sort

• given order : 91 25 15 23 34 78• end of pass 1 : 25 91 15 23 34 78• end of pass 2 : 15 25 91 23 34 78• end of pass 3 : 15 23 25 91 34 78• end of pass 4 : 15 23 25 34 91 78• end of pass 5 : 15 23 25 34 78 91

Page 25: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Quick sort

• Quick sort is a fast sorting algorithm, which is used widely in practice.

• On the average, it has O(n log n) complexity, making quick sort suitable for sorting big data volumes.

• The idea of the algorithm is quite simple and is based on divide and conquer strategy.

• In the worst case (already in sorted order) is O(n2)

Page 26: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Quick sort algorithm1. Choose a pivot value: Take the value of the first

element as pivot value, but it can be any value in the set.

2. Partition: Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array, and the pivot at the correct place. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts.

3. Sort both parts: Apply quick sort algorithm recursively to the left and the right parts.

Page 27: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Partition algorithm

1. Chose two indices i and j and at the very beginning of the partition algorithm i points to the first element in the set and j points to the last one. Select the first element as pivot.

2. Then move i forward, until an element with value greater or equal to the pivot is found. Move j backward, until an element with value lesser or equal to the pivot is found.

3. If i < j then elements are swapped i steps forward and j steps backward steps 2 & 3 are repeated until i becomes greater than j.

4. Copy j-th element to the pivot position and pivot to j-th position.5. The partition is complete. ie, all values before(left of) pivot

element are less than or equal to the pivot and all values after(right of) pivot element are greater or equal to the pivot.

Page 28: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Quick sort example66 91 12 15 23 34 56 78 89 90 l=0 h=9 j=5 34 56 12 15 23 66 91 78 89 90 l=0 h=4 j=3 15 23 12 34 56 66 91 78 89 90 l=0 h=2 j=1 12 15 23 34 56 66 91 78 89 90 l=0 h=0 recursive call ends l=2 h=2 recursive call ends l=4 h=4 recursive call ends

Page 29: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Quick sort Example contd.

l=6 h=9 j=9 12 15 23 34 56 66 90 78 89 91 l=6 h=8 j=8 12 15 23 34 56 66 89 78 90 91 l=6 h=7 j=7 12 15 23 34 56 66 78 89 90 91 l=6 h=6 recursive call ends l=8 h=7 recursive call ends l=9 h=8 recursive call ends l=10 h=9 recursive call ends

Page 30: Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

Merge-sort