Please refer to Sorting Class 11 Computer Science notes and questions with solutions below. These revision notes and important examination questions have been prepared based on the latest Computer Science books for Class 11. You can go through the questions and solutions below which will help you to get better marks in your examinations.
Class 11 Computer Science Sorting Notes and Questions
10.1 DEFINITION:
To arrange the elements in ascending or descending order.
In this chapter we shall discuss two sorting techniques:
- Bubble Sort
- Insertion Sort
1. BUBBLE SORT: Bubble sort is a simple sorting algorithm. It is based on comparisons, in which each element is compared to its adjacent element and the elements are swapped if they are not in proper order.
PROGRAM:
L=eval(input(“Enter the elements:”))
n=len(L)
for p in range(0,n-1):
for i in range(0,n-1):
if L[i]>L[i+1]:
L[i], L[i+1] = L[i+1],L[i]
print(“The sorted list is : “, L)
OUTPUT:
Enter the elements:[60, 24, 8, 90, 45, 87, 12, 77]
The sorted list is : [8, 12, 24, 45, 60, 77, 87, 90]
Calculating Number of Operations (Bubble sort):
2. INSERTION SORT: Sorts the elements by shifting them one by one and inserting the element at right position.
PROGRAM:
L=eval(input(“Enter the elements: “))
n=len(L)
for j in range(1,n):
temp=L[j]
prev=j-1
while prev>=0 and L[prev]>temp: # comparison the elements
L[prev+1]=L[prev] # shift the element forward
prev=prev-1
L[prev+1]=temp #inserting the element at proper position
print(“The sorted list is :”,L)
OUTPUT:
Enter the elements: [45, 11, 78, 2, 56, 34, 90, 19]
The sorted list is : [2, 11, 19, 34, 45, 56, 78, 90]
Calculating Number of Operations (Insertion Sort):
10.2 How insertion sort is better than bubble sort?