Python Search
Python Search
PROBLEM-SOLVING
With Python
LINEAR SEARCH
9
• AT Index 4
BINARY SEARCH
8 4 2 7 9
2 4 7 8 9
L M U
• M = (L+U)/2
• (0+5)/2 = 2
• Mid Index now become L
2 4 7 8 9
L M U
BUBBLE SORT
• Sorting algorithm that compares two adjacent elements
• Swaps the elements to put them in desired order.
8 4 2 7 9 i=0
4 8 2 7 9 i=1
4 2 8 7 9 i=2
4 2 7 8 9 i=3
• This iteration will continue till the list is sorted
• At the end of the iteration our output will be
2 4 7 8 9
INSERTION SORT
• This sort uses an algorithm to place an unsorted element at its suitable place.
• Lets assume that we have the following list to sort.
0 1 2 3 4 Index no
10 4 25 1 5 Element
• 1st element will be assumed as the sorted element which is Index 0 and element 10
• Current = 4 Sorted 10
• If current is less than sorted = True; swap the places
0 1 2 3 4 • Same process will be repeated and this time 25 will be compared
with 10. Now, current is 25 and sorted is 10
4 10 25 1 5
• If current is < sorted = false; do not swap
0 1 2 3 4 • Now, current is 1 and sorted is 25
4 10 25 1 5 • Above process will be repeated to compare the current with sorted
• If true it will keep swapping the places.