0% found this document useful (0 votes)
26 views22 pages

Chapter 9

Uploaded by

Adarsh Sah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views22 pages

Chapter 9

Uploaded by

Adarsh Sah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

PROGRAMMING AND PROBLEM

SOLVING WITH PYTHON

CHAPTER 9
LIST
PROCESSING :SEARCHI
NG AND SORTING

Copyright © 2018 McGraw Hill Education, All Rights Reserved.

PROPRIETARY MATERIAL © 2018 The McGraw Hill Education, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced
or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers
and educators permitted by McGraw Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it
without permission.
Introduction to Searching Techniques
 Searching is a technique of finding an element from
a given list of elements.

 The searching techniques should be able to locate


the element to be searched as quickly as possible.

The search techniques are categorized as follows.


a) Linear or Sequential Search
b) Binary Search
Linear/Sequential Search

 In Linear Search elements are examined


sequentially starting from the first element.
 It compares the element to be searched i.e. the
(Key element) sequentially with each element in the
list.
 The process of searching terminates when the
element to be searched is found within the list .
 The Process of searching the element to be searched
may be exhausted if the element to be searched is
not found within the list.
Unordered List – Analysis of Sequential
Search
Case Best Case Worst Case Average Case

Element is 1 N
present in the
list
Element is not N N N
present in the
List
Sorted List – Analysis of Sequential
Search
 Expected number of comparisons required for
unsuccessful search can be reduced if the list is
sorted.
 Example:
List1[] = 10 15 20 25 50 60 70 80

Element to be searched = 30
Search should terminate here

Best Case Worst Case Average Case

Element is 1 N
present in the
list
Element is not 1 N
present in the
List
The Binary Search

Need of Binary Search ?


 Let us consider the size of List is 1 Million
(220).
 Thus, if we want to search using sequential search
algorithm then in the worst case we require 220
comparisons.
 It means sequential search algorithm is not good
for list with large size.
 Thus we require more efficient algorithms and
binary search algorithm is one of the simple and
efficient algorithm.
Binary Search…….
To implement binary search algorithm the element within the
list must be in sorted order i.e. in ascending order.

The binary search algorithm is based on the following three


conditions.

 If the key is less than the list’s middle element, then


the programmer has to search only in the first half of
the list.
 If the key is greater than the list’s middle element,
then the programmer has to search only in the second half
of the list.
 If the element to be found i.e. the key element is equal
to lists middle element then search ends.
 If the element to be found is not present within the list
then the it returns None or -1 which it indicates the
element to be searched is not present in the list.
 Consider the sorted List of 10 integers.
10 18 19 20 25 28 48 55 62 70
Element to be searched = 48
Introduction to Sorting Techniques
 Sorting means rearranging the elements of a list, so that they
are kept in some relevant order.

 The order can be either ascending or descending. Consider list


L1, if the elements within the list are arranged in ascending
order in such a way that L1[0] < L1[1] < …………<L[N].

 Sorting algorithms are broadly classified into two types


a) Internal sorting - main memory
b) External Sorting - Secondary memory
Various sorting algorithms are as follows
a) Bubble sort,
b) Selection sort
c) Insertion sort
d) Quick Sort
e) Merge Sort
Bubble Sort
 Bubble sort, sorts the list elements by repeatedly
moving the largest element to the highest index
position of the list.

 The consecutive adjacent pair of elements in lists


is compared with each other.

 If the element at lower index is greater than the


element at the higher index, then the two elements
are interchanged so that element with smaller value
is placed before the bigger one.

 The algorithm repeats this process till the list of


unsorted element exhausts.
Example of Bubble sort:
Consider elements within the list as follows
L1 = [30, 50, 45, 20, 90, 78]
Sort the list using Bubble sort.
Selection Sort
The whole process of selection sort will be as follows

Iteration 1:
Search the smallest element from list[0] to list[N-1].
Interchange list[0] with smallest element.
Result: list[0] is sorted.

Iteration 2:
Search the smallest element from list[1] to list[N-1].
Interchange list[1] with smallest element.
Result: list[0],list[1] is sorted.

Iteration N-1:
Search the smallest element from list[N-1] to list[N-1].
Interchange list[N-1] with smallest element.
Result: list[0]…………list[N-1].
Insertion Sort
 Insertion sort is based on the principle of
inserting the elements at its correct place in a
previously sorted list.
 It always maintains a sorted sublist in the lower
portion of the list.
 Each new element is inserted back into the
previous sub list.
Quick Sort
Quick sort in one of the fastest internal sorting
algorithms. The quick sort algorithm is based on three
main strategies.

Split or Partition: Select a random element called pivot


from the sequence of elements to be sorted. Suppose the
selected element is X where X is any number. Now split
(Divide) the list into the two small lists Y and Z such
that
 All the elements of the first part Y are less than the
selected element pivot.
 All the elements of the second part Z are greater than
the selected element Pivot.
 Sort the Sub arrays.
 Merge (Join/Concatenate) the sorted sub array.
Merge Sort
 The merge sort is based on three main strategies.
 Split the List into two Sub List (Split or Divide):
Split means partitioning the n elements of list
into two sub lists. Where each sub list contains
n/2 elements each.
 Sort Sub Lists (Conquer): Sorting two sub arrays
recursively using merge sort.
 Merge the sorted sub lists (Combine): Combine means
merging two sorted sub lists each of size n/2 to
produce the sorted list of n elements.
Example: Merge Sort
Conclusion
 Linear search, searches element sequentially from
the list till the element is found.

 Binary search is most efficient algorithm which


reduces no of comparisons to search the element.

 Various sorting algorithms such as bubble sort,


selection sort, quick sort and merge sort have been
covered.

You might also like