Presentation is loading. Please wait.

Presentation is loading. Please wait.

Principles of Computing – UFCFA3-30-1

Similar presentations


Presentation on theme: "Principles of Computing – UFCFA3-30-1"— Presentation transcript:

1 Principles of Computing – UFCFA3-30-1
Searching and Sorting Instructor : Mazhar H Malik Global College of Engineering and Technology

2 Today’s Lecture Sorting Searching Linear Search Binary Search
Selection Sort Insertion Sort Bubble Sort Searching & Sorting

3 Learning Objective Explain algorithmic behaviour of programs in appropriate formal terms. LO[3] Component B

4 Linear Array A linear array is the list of finite number of data elements (i.e. same type of data). Data Store in the array identify by the index numbers.

5 Searching A search algorithm is a method of locating a specific item of information in a larger collection of data. This section discusses two algorithms for searching the contents of an array. Linear Search Binary Search

6 The Linear Search This is a very simple algorithm.
It uses a loop to sequentially step through an array, starting with the first element. It compares each element with the value being searched for and stops when that value is found or the end of the array is reached.

7 The Linear Search-Algorithm
Initialize the elements of the array Input “X” number to search while i<length of array if(array[index] == X): print("%d found at %dth position"%(X,i)) break i=i+1

8 Efficiency of the Linear Search
The advantage is its simplicity. It is easy to understand Easy to implement Does not require the array to be in order The disadvantage is its inefficiency If there are 20,000 items in the array and what you are looking for is in the 19,999th element, you need to search through the entire list.

9 Binary Search The binary search is much more efficient than the linear search. It requires the list to be in order. The algorithm starts searching with the middle element. If the item is less than the middle element, it starts over searching the first half of the list. If the item is greater than the middle element, the search starts over starting with the middle element in the second half of the list and it continues halving the list until the item is found.

10 1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Item := 33 Mid Beg := 1 End : = 9 Mid := Int (( Beg + End ) / 2)

11 1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Mid Item := 33 Beg := 1 End : = 4 Mid := Int (( Beg + End ) / 2)

12 1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Item := 33 Mid Beg := 3 End : = 4 Mid := Int (( Beg + End ) / 2)

13 1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Item := 33 Mid Mid := 3

14 Efficiency of the Binary Search
Much more efficient than the linear search. Array should be in sorting condition before applying binary search

15 How Fast is a Binary Search?
Worst case: 11 items in the list took 4 tries How about the worst case for a list with 32 items ? 1st try - list has 16 items 2nd try - list has 8 items 3rd try - list has 4 items 4th try - list has 2 items 5th try - list has 1 item

16 How Fast is a Binary Search? (con’t)
List has 512 items 1st try items 2nd try items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item List has 250 items 1st try items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item

17 What’s the Pattern? List of 11 took 4 tries List of 32 took 5 tries

18 Since binary algorithm is very efficient (e. g
Since binary algorithm is very efficient (e.g. it requires only about 20 comparisons with an initial list of 1,000,000 elements) but observe that algorithm requires two conditions The list must be sorted One must have direct access to middle element in sub list

19 Sorting Sorting — arranging items in order — is the most fundamental task in computation. Sorting enables efficient searching algorithms such as binary search. Selection Sort Insertion Sort Bubble Sort

20 Sorting Selection sort: repeatedly pick the smallest element to append to the result. Insertion sort: repeatedly add new element to the sorted result. Bubble sort: repeatedly compare neighbor pairs and swap if necessary.

21 Selection Sort Selection sort is to repetitively pick up the smallest element and put it into the right position: Find the smallest element, and put it to the first position. Find the next smallest element, and put it to the second position. Repeat until all elements are in the right positions.

22 Selection Sort

23 Insertion Sort Insertion sort maintains a sorted sub-array, and repetitively inserts new elements into it. The process is as following: Take the first element as a sorted sub-array. Insert the second element into the sorted sub-array (shift elements if needed). Insert the third element into the sorted sub-array. Repeat until all elements are inserted.

24 Insertion Sort

25 Bubble Sort Bubble sort repetitively compares adjacent pairs of elements and swaps if necessary. : Scan the array, swapping adjacent pair of elements if they are not in relative order. This bubbles up the largest element to the end. Scan the array again, bubbling up the second largest element. Repeat until all elements are in order.

26 Bubble Sort

27 Bubble Sort Apply Bubble sort on the following array {38, 27, 43,9, 82, 10} 

28 Home Work {38, 27, 43, 3, 9, 82, 10} Using above dataset, perform
{38, 27, 43, 3, 9, 82, 10}  Using above dataset, perform Searching Linear Search Binary Search Sorting Insertion Sort Selection Sort Bubble Sort Due Date:

29 Lab Work: Linear Search Python Implementation
Write a program that will input a number from user and will check whether number exist in the array or not using Linear Search Algorithm. After execution display suitable message (i.e Input number not exist or Input number is at index[n] of array.

30 Summary In This lecture we discussed about the array searching and Sorting techniques. Searching Techniques? Sorting Techniques?

31 Reference Chapter 6, Page Hein J H (2010). Discrete Structures, Logic, and Computability, 4th ed. Jones and Bartlett

32 Questions?


Download ppt "Principles of Computing – UFCFA3-30-1"

Similar presentations


Ads by Google