Class 12 Computer Science NCERT Exercise Solution
Sorting
1. Consider a list of 10 elements:
numList = [7,11,3,10,17,23,1,4,21,5].
Display the partially sorted list after three complete passes of Bubble sort.
Answer: If the first element is bigger, it is swapped with the second. Else, no change is done.
2. Identify the number of swaps required for sorting the following list using selection sort and bubble sort and identify which is the better sorting technique with respect to the number of comparisons.
List 1 : 63, 42, 21, 9
Answer: Selection Sort vs. Bubble Sort
On the basis of number of swap, Selection Sort is better than Bubble Sort.
3. Consider the following lists:
List 1: 2 3 5 7 11
List 2: 11 7 5 3 2
If the lists are sorted using Insertion sort then which of the lists List1 or List 2 will make the minimum number of comparisons? Justify using diagrammatic representation.
Answer: In case of sorting in Ascending order, List 1 will make minimum number of comparisons as it is already in ascending order.
In case of Descending order, List 2 will make minimum number of comparisons as it is in descending order.
Following diagram shows comparisons for ascending order sorting:
4. Write a program using user defined functions that accepts a List of numbers as an argument and finds its median.
(Hint : Use bubble sort to sort the accepted list. If there are odd number of terms, the median is the center term. If there are even number of terms, add the two middle terms and divide by 2 get median)
Answer:
def bubblesort(numlist):
length = len(numlist)
for i in range( length - 1):
for j in range(0, n-1-i):
if numlist[j] > numlist[j+1]:
numlist[j], numlist[j+1] = numlist[j+1], numlist[j]
numbers = eval(input("Enter list using bracket : "))
bubblesort(numbers)
size = len(numbers)
mid = size // 2
if size % 2 == 0:
median = (numbers[mid] + numbers[mid+1]) / 2
else:
median = numbers[mid]
print("Given list in sorted orders : ")
print(numbers)
print("Median : ", median)
5. All the branches of XYZ school conducted an aptitude test for all the students in the age group 14 – 16. There were a total of n students. The marks of n students are stored in a list.
Write a program using a user defined function that accepts a list of marks as an argument and calculates the ‘xth’ percentile (where x is any number between 0 and 100).
You are required to perform the following steps to be able to calculate the ‘xth’ percentile. Note: Percentile is a measure of relative performance i.e. It is calculated based on a candidate’s performance with respect to others.
For example : If a candidate’s score is in the 90th percentile, that means she/he scored better than 90% of people who took the test.
Steps to calculate the xth percentile:
I. Order all the values in the data set from smallest to largest using Selection Sort. In general any of the sorting methods can be used.
II. Calculate index by multiplying x percent by the total number of values, n.
For example: to find 90th percentile for 120 students:
0.90 * 120 = 108
III. Ensure that the index is a whole number by using math.round()
VI. Display the value at the index obtained in Step 3.
The corresponding value in the list is the xth percentile.
Answer:
def bubblesort(numlist):
length = len(numlist)
for i in range( length - 1):
for j in range(0, n-1-i):
if numlist[j] > numlist[j+1]:
numlist[j], numlist[j+1] = numlist[j+1], numlist[j]
numbers = eval(input("Enter list of marks using bracket : "))
bubblesort(numbers)
x = int(input("For xth percentile, Enter x : "))
size = len(numbers)
index = int(round((size * x ) / 100, 0))
print(str(x) + "th percentile is : ")
print(numbers[index])
6. During admission in a course, the names of the students are inserted in ascending order. Thus, performing the sorting operation at the time of inserting elements in a list.
Identify the type of sorting technique being used and write a program using a user defined function that is invoked every time a name is input and stores the name in ascending order of names in the list.
Answer: Insertion sort, as this technique allows to insert a value at its position in a sorted list.
lista = [ ]
i = 1
more = 'y'
while more == 'y':
name = input ("Enter next name : ")
lista.append(name)
j = i
pos = len(lista)
save = list[pos-1]
j = pos - 1
while (j > 0 and lista[j-1] > save):
lista[j] = lista[j-i]
j = j - 1
lista[j] = save
more = input("Want to enter more ? (y/n) :")
print("final list")
print(lista)
Class 12 Computer Science NCERT Solutions
Class 11 Computer Science NCERT Solutions
- Class 11 Computer Science NCERT Exercise Solution
- Class 11 Computer Science – Chapter 1: Computer Systems
- Ch 2 Emerging Trends NCERT Exercise Solution – Class 11 Computer Science
- Encoding Schemes and Number Systems NCERT Exercise solutions
- Class 11 Computer Science Chapter 5 Getting Started with Python NCERT Solution
- Class 11 Computer Science Chapter 4 Introduction to Problem Solving NCERT Solution
- Class 11 Computer Science Ch 6 Flow of Control NCERT Exercise Solution
- Class 11 Computer Science Ch 7 Functions in Python NCERT Book Exercise Solution
- Class 11 Computer Science Ch 8 Strings in Python NCERT Book Exercise Solution
- Class 11 Computer Science Ch 9 Lists in Python NCERT Book Exercise Solution