Until today, we have heard about various techniques of Python. As a new programming language, we are still in the hunt for something more. Today we are going to talk about searching and sorting methods in Python. For an example take a list of 1000 names and it would be very frustrating if you are asked to select some names from the list. Moreover, if the list is not in a definite order, it becomes tough for us and we would quite that after few minutes. As a tech savvy person, I always prefer things which are organized and in order. If it’s in order, then it takes less effort to sort the names from the list.
For a program, the same strategy is applied. Getting something from a huge list of codes is not only difficult but also seems impossible sometimes. If all the things can be arranged in a certain order then finding out something becomes easier, and efficiency of the process gets better. So as a programming enthusiastic we always want to have an exact bug free sorting and searching algorithm.
Let’s know what is sorting and what is searching in a digital world. Sorting is nothing but putting a list of values in a certain order while searching is the easier way to find the position of a required value.
Sort algorithm is purely based on respective and successive short selection of maxima and minima values. Take an example- if you are asked to sort numbers in ascending order, then the smallest number will be at the initial position.
If the original list of numbers is like as follows-
| 7 | 5 | 3.5 | 4 | 3.1 |
The first and foremost thing we need to do is finding out the minimum value which is 3.1 in this case.
Then the string of values will be arranged in the certain value.
| 3.1 | 3.5 | 4 | 5 | 7 |
All the five values are in the correct positions.
This is how the sort selection algorithm is implemented in Python programming to find out a particular value (it’s solely based on Isai Damier):
for i in range(len(aList)):
least = i
for k in range(i+1, len(aList)):
if aList[k] < aList[least]:
least = k
swap(aList, least, i)
def swap(A, x, y):
temp = A[x]
A[x] = A[y]
A[y] = temp
The above-mentioned script is ended up with the below-mentioned lines. Try it now and see the magic.
|my_list = [5.76,4.7,25.3,4.6,32.4,55.3,52.3,7.6,7.3,86.7,43.5]
In this case the output will be-
[4.6, 4.7, 5.76, 7.3, 7.6, 25.3, 32.4, 43.5, 52.3, 55.3, 86.7]
Linear Search Algorithm
Linear Search algorithm is known as one of the simplest algorithm for Python. Here each and every item in the list (right from the first item) is verified until and unless the required item is found by the program or the search ends with the last item of the list.
The linear search method can be like the following (based on Python School):
found = False
position = 0
while position < len(my_list) and not found:
if my_list[position] == item:
found = True
position = position + 1
To test the script we need to add this below script at the end of the last line and check what it produces.
|bag = [‘book’,’pencil’,’pen’,’note book’,’sharpener’,’rubber’]
item = input(‘What item do you want to check for in the bag?’)
itemFound = linearSearch(item,bag)
print ‘Yes, the item is in the bag’
print ‘Oops, your item seems not to be in the bag’
If you enter the input, always make sure one thing and that is, it’s between single or between the double quotes (‘pencil’). If you input’pencil’, then you will get the following output-
Yes, the item is in the bag
As an advanced programming language Python has made the full coding experience very flexible and easier. Searching and sorting have also become very easy to set and implement anytime without much hassle.
As we can see, Python proves itself again to be a programming language that makes it easy to program algorithmic concepts as we did here, dealing with sorting and searching algorithms.
Must Read Articles :- How To Debug a Bug in Python Code