Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director

Similar presentations


Presentation on theme: "Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director"— Presentation transcript:

1 Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Bharat Institute of Engineering &Technology Hyderabad Mobile

2 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.

3 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.

4 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.

5 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.

6 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.

7 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.

8 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.

9 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.

10 Binary search Example consider the following sequence of integers sorted in ascending order and say we are looking for the number 55: The search starts at middle (41) . Since the key(55) is greater than 41 the search should continue in the subset Once again the search stars at the middle(72).

11 s

12 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

13 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.

14 Classification of sorting techniques
Exchange ex: bubble sort and quick sort Selection ex: simple selection sort, heap sort Insertion ex: straight insertion sort, Shell sort Merge ex: ex: Two way merge sort Distribution ex: Radix and bucket sorts

15 Sorting algorithms that we will study
Bubble Sort Selection sort (simple) Insertion sort (straight) Quick sort Merge sort.

16 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.

17 Bubble sort example Consider the following set of numbers
Pass 1: Pass 2: Pass 3: Pass 4: Pass 5:

18 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 comparisons pass n comparison. Total comparisons are n*(n-1)/2 which is a polynomial of degree 2. Therefore the time complexity is O(n*n );

19 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

20 Example of selection sort
Consider the following set of numbers Pass 1: Pass 2: Pass 3: Pass 4: Pass 5:

21 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

22 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.

23 Example of insertion sort
given set : end of pass 1 : end of pass 2 : end of pass 3 : end of pass 4 : end of pass 5 : end of pass 6 : end of pass 7 : end of pass 8 :

24 Example of insertion sort
given order : end of pass 1 : end of pass 2 : end of pass 3 : end of pass 4 : end of pass 5 :

25 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)

26 Quick sort algorithm 1. 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.

27 Partition algorithm 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.

28 Quick sort example l=0 h=9 j= l=0 h=4 j= l=0 h=2 j= l=0 h=0 recursive call ends l=2 h=2 recursive call ends l=4 h=4 recursive call ends

29 Quick sort Example contd.
l=6 h=9 j= l=6 h=8 j= l=6 h=7 j= 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

30 Merge-sort


Download ppt "Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director"

Similar presentations


Ads by Google