Liner List Manipulation

Sponsor Area

Question
CBSEENCO12011542

What will be the status of the following list after the First, Second and Third pass of the bubble sort method used for arranging the following elements in descending order ?
Note: Show the status of all the elements after each pass very clearly underlining the changes.
152, 104, -100, 604, 190, 204

Solution

Phase I

152

104

-100

604

190

204

152

104

-100

604

190

204

152

104

-100

604

190

204

152

104

604

-100

190

204

152

104

604

190

-100

204

152

104

604

190

204

-100

 Phase II

152

104

604

190

204

-100

152

104

604

190

204

-100

152

604

104

190

204

-100

152

604

190

104

204

-100

152

604

190

204

104

-100

 Phase III

152

604

190

204

104

-100

604

152

190

204

104

-100

604

190

152

204

104

-100

604

190

204

152

104

-100

Sponsor Area

Question
CBSEENCO12011689

What will be the status of the following list after the fourth pass of bubble sort and fourth pass of selection sort used for arranging the following elements in descending order?
14, 10, –12, 9, 15, 35

Solution

Bubble Sort

Original Content

  14 10 12 9 15 35
(i) 14 10 9 15 35 12
(ii) 14 10 15 35 9 12
(iii) 14 15 35 10 9 12
(iv) 15 35 14 10 9 12

Unsorted status after 4th pass

Selection Sort

  14 10 12 9 15 35
(i) 35 10 12 9 15 14
(ii) 35 15 12 9 10 14
(iii) 35 15 14 9 10 12
(iv) 35 15 14 10 9 12

Question
CBSEENCO12011690

Write a method in python to search for a value in a given list (assuming that the elements in the list are in ascending order) with the help of Binary Search method. The method should return –1 if the value does not present else it should return the position of the value present in the list.

Solution

def bSearch(L, key):
	low = 0
	high = len(L)-1
	found = False
	while (low <= high) and (not found):
		mid = (low+high)//2
		if L[mid] == key:
			found = True
		elif L[mid] < key:
			low = mid + 1
		else:
			high = mid - 1
		if found:
			return mid+1 # may even be 'return mid'
		else:
			return -1