Unit 5 - (Sorting, Higher Order Sort, Merge List)
Unit 5 - (Sorting, Higher Order Sort, Merge List)
(KNC- 302)
UNIT 5 (Continued)
For example, names and contact information may be sorted in alphabetical order to allow the
person looking for a name to see if it's available.
–Example:
•Contact list on your phone.
•Ordering marks before assignment of grades.
Common sorting algorithms
Bubble/Shell sort: Exchange two adjacent elements if they are out of order. Repeat until array is
sorted.
Insertion sort: Scan successive elements for an out-of-order item, then insert the item in the
proper place.
Selection sort: Find the smallest (or biggest) element in the array, and put it in the proper place.
Swap it with the value in the first position. Repeat until array is sorted.
Quick sort: Partition the array into two segments. In the first segment, all elements are less than
or equal to the pivot value. In the second segment, all elements are greater than or equal to the
pivot value. Finally, sort the two segments recursively.
'Merge sort': Divide the list of elements in two parts, sort the two parts individually and then
merge it.
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from unsorted part and putting it at the
beginning.
i=j=k=0
# Until we reach either end of either L or M, pick larger among elements L and M and place them in the correct position at A[ p..r]
while i < len(L) and j < len(M):
if L[i] < M[j]:
array[k] = L[i]
i += 1
else:
array[k] = M[j]
j += 1
k += 1
# When we run out of elements in either L or M, pick up the remaining elements and put in A[p..r]
while i < len(L):
array[k] = L[i]
i += 1
k += 1
# run loops two times: one for walking throught the array
# and the other for comparison
for i in range(len(array)):
for j in range(0, len(array) - i - 1):
num1=[1,2,3]
num2=[4,5,6]
num1.append(num2)
print (num1)
#Output:[1, 2, 3, [4, 5, 6]]
2. extend
•The extend method will extend the list by appending all the items from the iterable.
•The length of the list will be increased depends on the length of the iterable.
•It will update the original list itself.
•Return type is None.
num1=[1,2,3]
num2=[4,5,6]
num1.extend(num2)
print (num1)
#Output:[1, 2, 3, 4, 5, 6]
3. Concatenation
•The list also supports concatenation operations.
•We can add two or more lists using + operator.
•It won’t update the original list.
•Return type is a new list object.
Example 1:Concatenating two lists
num1=[1,2,3]
num2=[4,5,6]
num3=num1+num2
print (num3)
#Output:[1, 2, 3, 4, 5, 6]
5. itertools.chain
Makes an iterator that returns an element from the first iterable until its exhausted, then proceeds to
the next iterable. It will treat consecutive sequences as a single sequence.
itertools.chain(*iterables)
The list is also iterable, so we can use itertools.chain() to merge two dictionaries. The return type will
be itertools.chain object. We can convert to a list using the list() constructor.
import itertools
num1=itertools.chain([1,2,3],[4,5,6])
#Returns an iterator object
7. for loop
•First for loop to go through the lists. (for n in (num1,num2)
•Second for loop to go through the elements in the list. (for x in n)
•Then it will append each element to the empty list created before num3
num1=[1,2,3]
num2=[4,5,6]
num3=[]
for n in (num1,num2):
for x in n:
num3.append(x)
print (num3)
#Output:[1, 2, 3, 4, 5, 6]
How to join all strings in a list:
str.join(iterable)
Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are
any non-string values in iterable, including bytes objects. The separator between elements is the string
providing this method.
Example 1: Joining all strings in the list. The separator is given as space.
num1=["Welcome", "to", "python", "programming", "language"]
num2=" ".join(num1)
print (num2)
#Output:Welcome to python programming language