0% found this document useful (0 votes)
3 views3 pages

python 3

The document discusses searching and sorting techniques, focusing on sequential search and bubble sort algorithms. Sequential search checks each element in a list until the target is found, while bubble sort repeatedly compares and swaps adjacent items to sort a list. The document includes examples and explanations of how these algorithms work, including their efficiency and the number of comparisons needed.

Uploaded by

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

python 3

The document discusses searching and sorting techniques, focusing on sequential search and bubble sort algorithms. Sequential search checks each element in a list until the target is found, while bubble sort repeatedly compares and swaps adjacent items to sort a list. The document includes examples and explanations of how these algorithms work, including their efficiency and the number of comparisons needed.

Uploaded by

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

Searching and Sorting Data

Searching Techniques
The process used to find the location of atarget aong a list of objeets
Location Wanted
(3)

a<o] a(1) a<2) a(3] a<4] a(5] a(6] a(7 al8| a<9) a10) a11|
21 36 1462 91 8 22 7 81 77 10
Target Given
(14)

Eg: Sequential search, Binary search

Sequential Search
Linear search or sequential search is a mnethod for finding a particular value in a list that nisists rf
checking every one of its clements, one at a time and in sequence, until the desired one is found.
Itis aspecial case of brute-forcesearch. The worst case is all clements might have to be tested.
The sequential search is used whenever the list is not ordered.

Eg:
def segsearch (list, value):
look = 0
while look < len (list) and list [look] != value:
look = look + 1
if look == len (list):
return "item not found"
else:
return look

Sorting Techniques
Sorting is an operation that segregates items into groups according to specified criterion.
Sorting takes an unordered collection and makes it an ordered one.
Eg: A={3162 1345 90}
A={0112334569}

Eg: Bubble sort, Selection sort

1
Bubble Sort
Bubble sort is a simple sorting algorithm that works by repcatedly stepping through the list to be sorted,
companng cach pair of adjacent items and swapping thcm if they are in the wrong order. The pass
thhough thc list is repcated until no swaps are nceded, which indicates that the list is sortcd.

Example:

9, 6. 2, 12, 11, 9, 3, 7
5ubbiesort compares the numbers in pairs from left to right exchanging when necessary. Here the first
number is compared to the second and as it is larger they are exchanged.
6, 9, 2, 12, 11, 9, 3, 7
Now thenext pair of numbers is compared. Again the 9 is the larger and so this pair is also exchanged.

6, 2,9, 12) 11, 9, 3, 7


In the third comparison, the 9 is not larger than the 12 so no exchange is made. Move on to compare
the next pair without any change to the list.

6, 2, 9, 12, 1), 9, 3, 7
The 12 is larger than the 11 so they are exchanged.

6, 2, 9, 11, 12, 9) 3, 7
The twelve is greater than the 9 so they are exchanged

6, 2, 9, 11, 9, (12, 3) 7
The 12 is greater than the 3 so they are exchanged.

6, 2, 9, 11, 9, 3, 12, 7
The 12 is greater than the 7 so they are exchanged.

The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list
must be largest number in the list and sois now in the correct position. Then a new pass should be
started from left to right.
First Pass

Second Pass
6, 2, 9, 11, 9, 3, 7, 12
2,(6)9. 9,3, 07,)11) 12
Notice that this time we do not have to compare the last two numbers as we know the 12 is in position.
This pass therefore only requires 6 comparisons.
Third Pass
2, 6, 9,3, 7, 9, 11, 12
2
This timc the Iland 12 arc in position This pass thercfore only requires 5
ompariss
Fouth Pass

2, 6, 3, 7, 9, 9, |1, 12
Each pass rcquires fewer comparisons. This time only 4are necded.
Fith PaNK

2, 3, 6, 7, 9, 9, l1, 12
The Ist isnow sonedbut the algorithm does not know this until it completes apass with no exchanues
Sixth Pas

2, 3, 6,7, 9, 9, 11, 12
This pass no exchanges are made so the algorithm knows the list is sorted. It carn therefore save time
by not doing the final pass. Withother lists this check could save much more work.

Eg:
def bubbleSort (alist):
for passnum in range (len (alist)-1,0, -1) :
for i in range (passnum)
if alist [i] >alist [i+1]:
temp = alist [il
alist [i] = alist [i+l]
alist [i+1] = temp

alist = (54,26,93,17, 77, 31,44, 55,20]


bubbleSort (alist)
print (alist)

Q& A
pass?
1. Which number is definitely in its correct position at the end of the first
Answer: The last number must be the largest.
number increases?
2. How does the number of comparisons required change as the pass
Answer: Each pass requires one fewer comparison than the last.

3. How does the algorithm know whcn thc list is sorted?


Answer: When a pass with no exchanges Occurs

a list of 10 numbers?
4. What is the maximum number of comparisons required for
Answer: 9comparisons, then 8, 7, 6, 5, 4,3, 2, l so total 45

You might also like