You are on page 1of 5

SORTING

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. This process is repeated until the entire
list is sorted. Here's a Python program that implements the bubble sort algorithm:

def bubble_sort(arr):

n = len(arr)

# Traverse through all array elements

for i in range(n-1):

# Last i elements are already in place

for j in range(0, n-i-1):

# Swap if the element found is greater than the next element

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

return arr

# Example usage

numbers = [64, 34, 25, 12, 22, 11, 90]

sorted_numbers = bubble_sort(numbers)

print("Sorted array:", sorted_numbers)

In this program, the bubble_sort function takes an array as input and performs the bubble sort
algorithm on it. The outer loop iterates from the first element to the second-to-last element (n-1),
while the inner loop compares each element with its adjacent element. If an element is greater than
its adjacent element, they are swapped.

After completing the inner loop for each iteration of the outer loop, the largest element in the
unsorted portion of the array "bubbles up" to the correct position at the end of the array. This
process is repeated until the entire array is sorted.
In the example usage, we have an array of numbers and apply the bubble_sort function to sort
them. The sorted array is then printed to the console.

Insertion sort is a simple sorting algorithm that works by repeatedly inserting an element from an
unsorted portion of the list into its correct position in the sorted portion of the list. Here's an
explanation of the algorithm along with a Python program to implement it:

def insertion_sort(arr):

# Traverse through 1 to len(arr)

for i in range(1, len(arr)):

key = arr[i] # Current element to be inserted

# Move elements of arr[0:i-1], that are greater than key,

# to one position ahead of their current position

j=i-1

while j >= 0 and arr[j] > key:

arr[j + 1] = arr[j]

j -= 1

arr[j + 1] = key

# Test the algorithm

array = [9, 5, 1, 4, 3]

insertion_sort(array)

print("Sorted array:", array)

In this implementation, we start with the second element (index 1) and iterate through the entire
array. For each element, we compare it with the elements in the sorted portion of the list (arr[0:i-1]).
If we find an element that is greater than the current element, we shift that element to the right to
make space for the current element. This process continues until we find the correct position for the
current element, and then we insert it there.

The outer loop runs for n-1 iterations (n is the length of the array), and the inner while loop iterates
backwards from i-1 to 0, comparing and shifting elements. Overall, the time complexity of insertion
sort is O(n^2), but it has the advantage of performing well on small or nearly sorted lists
DICTIONARIES

19. d = {'a': 'Delhi', 'b': 'Mumbai', 'c': 'Kolkata'}

for i in d:

if i in d[i]:

x = len(d[i])

print(x)

OUTPUT : 6

The dictionary d has three key-value pairs. The loop iterates over the keys of the dictionary ('a', 'b',
and 'c'). For each iteration, it checks if the key i is present in the corresponding value d[i].

For the first iteration (i = 'a'), the condition if i in d[i] evaluates to True since 'a' is present in the
string 'Delhi'. Therefore, x is assigned the length of 'Delhi', which is 5.

For the second iteration (i = 'b'), the condition if i in d[i] evaluates to True since 'b' is present in the
string 'Mumbai'. Therefore, x is updated with the length of 'Mumbai', which is 6.

For the third iteration (i = 'c'), the condition if i in d[i] evaluates to True since 'c' is present in the
string 'Kolkata'. Again, x is updated with the length of 'Kolkata', which is 7.

After the loop finishes executing, the value of x will be the length of the last city name
encountered, which is 7 in this case. However, since x is not used outside the loop, there is no
need to print it.

10. Write the output of the following code given below:

Marks = {'Sidhi': 65, 'Prakul': 62, 'Suchitra': 64, 'Prakash': 50}

newMarks = {'Suchitra': 66, 'Arun': 55, 'Prakash': 49}

Marks.update(newMarks)

for key, value in Marks.items():

print(key, 'scored', value, 'marks in Pre Board', end=' ')

if value < 55:

print('and needs improvement.', end=' ')

print()

OUTPUT

Sidhi scored 65 marks in Pre Board

Prakul scored 62 marks in Pre Board

Suchitra scored 66 marks in Pre Board


Prakash scored 49 marks in Pre Board and needs improvement.

Arun scored 55 marks in Pre Board

Explanation:

The initial dictionary Marks contains the names of students as keys and their respective marks as
values.

The newMarks dictionary contains additional student names and their marks.

The Marks.update(newMarks) statement updates the Marks dictionary by adding the key-value
pairs from newMarks.

The for loop iterates over each key-value pair in the Marks dictionary.

Inside the loop, the print statement prints the student's name, their score, and the phrase "marks
in Pre Board" using string formatting.

If the student's score is less than 55, an additional phrase "and needs improvement" is printed.

After printing each student's information, an empty print() statement is used to print a blank line
for better readability.
15. Predict the output of the following:
P=1400
D={'KTM':45,'RoyalEnfield':75,'Jawa':33,'TVS':89,'Yamaha':111}
for j in D:
if(len(j)>5):
P=P-D[j]
print(j)
X={'Suzuki':1000,'Bajaja':21}
D.update(X)
print(X)
print(D)
print(D.get('Jawa','KTM'))

OUTPUT

Yamaha

{'Suzuki': 1000, 'Bajaja': 21}

{'KTM': 45, 'RoyalEnfield': 75, 'Jawa': 33, 'TVS': 89, 'Yamaha': 111, 'Suzuki': 1000, 'Bajaja':
21}

33

OUTPUT EXPLANATION

The key 'Yamaha' satisfies the condition len(j) > 5, so it is printed.

The dictionary X is printed: {'Suzuki': 1000, 'Bajaja': 21}.

The updated dictionary D is printed: {'KTM': 45, 'RoyalEnfield': 75, 'Jawa': 33, 'TVS': 89, 'Yamaha':
111, 'Suzuki': 1000, 'Bajaja': 21}.

The D.get('Jawa', 'KTM') statement retrieves the value associated with the key 'Jawa' from D, which
is 33, so it is printed.

You might also like