Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Sorting and searching (DS with python)

Rating
-
Sold
-
Pages
9
Uploaded on
22-03-2023
Written in
2022/2023

This document contains well-specified notes on sorting and searching algorithms with code, examples as well as algorithms!!!

Institution
Course

Content preview

Searching and Sorting
Searching:

I. Linear Search Algorithm:

 This method of searching is also called as sequential search

 Linear search work on both sorted as well as unsorted list.

 Suppose we want to search an element X from the list of N elements. First
the element X is compared with the I element in the List. If the element is
matched then search is successful.

 If the element is not matched then the element X is compared with next
element and this process is repeated until match is found or all the elements
in the list are compared.

 Algorithm:

step-1 : Start from left most element of list. And one by one compare it with each
element of the list.

step-2: If it matches with the given element, return index

step-3: If it doesn't match with any element, return None.

 Example:

The list consist of 5 elements as shown below:




Suppose we want to find 55 in the list. So 55 is compared with the first element in the
list which is 11.




Since match is not found 55 is compared with next element in the list and this
process is repeated until match found or all elements in the list is compared as
shown below.

,  Code:

def linear_search(list, x):

for i in range(len(list)):

if list == x:

return i

return None

linear_search([7, 3, 5, 8, 9], 5)

II. Binary Search Algorithm.

 Binary search is an efficient algorithm for finding an item from a sorted list of
items.

 It is used to reduce the time complexity.

 This method of searching is more efficient as compared to linear search
method. Because we does not have to compare the search element with all
the elements in the list until the element is found.

 Algorithm:

step-1: Compare x with middle element.

step-2: If x matches with middle element, we return mid index.

step-3: Else if x is greater than mid element, then x can only lie in right half part after
mid element. Then we apply algorithm again for right half part

step-4: Else if x is smaller than mid element, x must lie in left half part, so we apply
algorithm for left half part

 Example:

Written for

Institution
Course

Document information

Uploaded on
March 22, 2023
Number of pages
9
Written in
2022/2023
Type
SUMMARY

Subjects

$3.79
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
tishavarma

Get to know the seller

Seller avatar
tishavarma Gujarat Technical University
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions