0% found this document useful (0 votes)
37 views6 pages

Sorting Algorithms Overview

The document provides an overview of sorting algorithms, including Bubble Sort, Selection Sort, and Insertion Sort, detailing their processes and algorithms. It also discusses the time complexity of these algorithms, noting that all have a time complexity of O(n^2). Additionally, the document includes multiple-choice questions to assess understanding of sorting concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Sorting Algorithms Overview

The document provides an overview of sorting algorithms, including Bubble Sort, Selection Sort, and Insertion Sort, detailing their processes and algorithms. It also discusses the time complexity of these algorithms, noting that all have a time complexity of O(n^2). Additionally, the document includes multiple-choice questions to assess understanding of sorting concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Yamuna sharma-Vijaya Vittala P U College, CS-Dept

Chapter 5. Sorting [1-MCQ,1-FIB,1-3M,1-5M(LOT/HOT)=10 M]


Sorting:
➢ Sorting is the process of ordering or arranging a given collection of elements in some particular
order.
➢ We can sort a collection of numbers in ascending (increasing) or descending (decreasing) order.
➢ If the collection is of strings, we can sort it in an alphabetical order (a-z or z-a) or according to the
length of the string.
➢ For example,
• words in a dictionary are sorted in alphabetical order;
• seats in an examination hall are ordered according to candidates’ roll number.
• sort a list of students based on their height or weight.
• Types:
1. Bubble sort 2. Selection sort 3. Insertion sort
Bubble sort:
• It sorts a given list of elements by repeatedly comparing the adjacent elements and swapping them if
they are unordered.
• Swapping two elements means changing their positions with each other.
• Algorithm : Example:
BUBBLESORT( numList, n)
Step 1: SET i = 0
Step 2: WHILE i< n REPEAT STEPS 3 to 8
Step 3: SET j = 0
Step 4: WHILE j< n-i-1,REPEAT STEPS 5 to 7
Step 5: IF numList[j] > numList[j+1] THEN
Step 6: swap(numList[j],numList[j+1])
Step 7: SET j=j+1
Step 8: SET i=i+1
• # program using function to sort the elements of list using bubble sort method
def bubble_sort(arr):
n = len(arr)
for i in range(n): OUTPUT:
for j in range(0, n-i-1): Enter the number of elements in the
if arr[j] > arr[j+1]: list: 5
arr[j], arr[j+1] = arr[j+1], arr[j] Enter element 34
def input_list(): Enter element 56
arr = [] Enter element 23
n = int(input("Enter the number of elements in the list: ")) Enter element 12
for i in range(n): Enter element 34
element = int(input("Enter element ")) Original list: [34, 56, 23, 12, 34]
[Link](element) Sorted list: [12, 23, 34, 34, 56]
return arr
arr = input_list()
print("Original list:", arr)
bubble_sort(arr)
print("Sorted list:", arr)

1
Yamuna sharma-Vijaya Vittala P U College, CS-Dept

Selection Sort:
• To sort a list having n elements, the selection sort makes (n-1) number of passes through the list.
• The list is considered to be divided into two lists –
• The left list containing the sorted elements,
• The right list containing the unsorted elements.
• Initially, the left list is empty, and the right list contains all the elements.
• For arranging elements in ascending order,
• In the first pass, all the elements in the unsorted list are traversed to find the smallest
element.
• The smallest element is then swapped with the leftmost element of the unsorted list.
• This element occupies the first position in the sorted list, and it is not considered in further
passes.
• In the second pass, the next smallest element is selected from the remaining elements in the
unsorted list and swapped with the leftmost element of the unsorted list.
• This element occupies the second position in the sorted list, and the unsorted list reduces by
one element for the third pass.
• This process continues until n-1 smallest elements are found and moved to their respective
places.
• The nth element is the last, and it is already in place.
• Example :

Algorithm:
SELECTIONSORT( numList, n)
Step 1: SET i=0
Step 2: WHILE i< n REPEAT STEPS 3 to 11 Step 10: swap(numList[i],numList[min])
Step 3: SET min = i, flag = 0 Step 11: SET j=j-1
Step 4: SET j= i+1 Step 12: SET i=i+1
Step 5: WHILE j<n, REPEAT STEPS 6 to 10
Step 6: IF numList[j] < numList[min] THEN
Step 7: min = j
Step 8: flag = 1
Step 9: IF flag = 1 THEN

2
Yamuna sharma-Vijaya Vittala P U College, CS-Dept

# program using function to sort the elements of list using selection sort method
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
for j in range(i+1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
def input_list():
arr = [] OUTPUT:
n = int(input("Enter the number of elements in the Enter the number of elements in the list: 5
list: ")) Enter element 32
for i in range(n): Enter element 45
element = int(input("Enter element ")) Enter element 67
[Link](element) Enter element 34
return arr Enter element 23
arr = input_list() Original list: [32, 45, 67, 34, 23]
Sorted list : [23, 32, 34, 45, 67]
print("Original list:", arr)
selection_sort(arr)
print("Sorted list:", arr)

Insertion Sort:
➢ In Insertion sort the list is divided into two parts - one of sorted elements and another of
unsorted elements.
➢ Each element in the unsorted list is considered one by one and is inserted into the sorted list
at its appropriate position.
➢ In each pass, the sorted list is traversed from the backward direction to find the position
where the unsorted element could be inserted. Hence the sorting method is called insertion
sort.
➢ This continues till all the elements in unsorted lists are inserted at appropriate positions in the
sorted list.
➢ ex:

3
Yamuna sharma-Vijaya Vittala P U College, CS-Dept

Algorithm: INSERTIONSORT( numList, n)


Step 1: SET i=1
Step 2: WHILE i< n REPEAT STEPS 3 to 9
Step 3: temp = numList[i]
Step 4:SET j = i-1
Step 5:WHILE j> = 0 and numList[j]>temp,REPEAT STEPS 6 to 7
Step 6: numList[j+1] = numList[j]
Step 7: SET j=j-1
Step 8: numList[j+1] = temp
Step 9: SET i=i+1
• # program using function to sort the elements of list using Insertion sort method
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1 OUTPUT:
arr[j + 1] = key Enter the number of elements in the list: 5
def input_list(): Enter element 32
arr = [] Enter element 45
n = int(input("Enter the number of elements in the list: ")) Enter element 67
for i in range(n): Enter element 34
element = int(input("Enter element ")) Enter element 23
[Link](element) Original list: [32, 45, 67, 34, 23]
return arr Sorted list : [23, 32, 34, 45, 67]
arr = input_list()
print("Original list:", arr)
insertion_sort(arr)
print("Sorted list :", arr)

Time Complexity of Algorithms:


➢ The amount of time an algorithm takes to process a given data can be called its time complexity.
➢ The following tips will guide us in estimating the time complexity of an algorithm.
• Any algorithm that does not have any loop will have time complexity as 1 since the number of
instructions to be executed will be constant, irrespective of the data size. Such algorithms are known
as Constant time algorithms.
• Any algorithm that has a loop (usually 1 to n) will have the time complexity as n because the loop
will execute the statement inside its body n number of times. Such algorithms are known as Linear
time algorithms.
• A loop within a loop (nested loop) will have the time complexity as n2. Such algorithms are known
as Quadratic time algorithms.
• If there is a nested loop and also a single loop, the time complexity will be estimated on the basis of
the nested loop only.
Note: According to the above rules, all the sorting algorithms namely, bubble sort, selection sort and
insertion sort have a time complexity of n2

4
Yamuna sharma-Vijaya Vittala P U College, CS-Dept

MCQ.
1. The process of ordering or arranging elements in some particular order is.
a. Searching b. traversing c. inserting d. sorting

2. Following is not a sorting technique.


a. Bubble sort b. insertion sort c. selection sort d. Linear sort

3. The first sorting technique is


a. Bubble sort b. Insertion sort c. selection sort d. Quick sort
4. The sorting technique where adjacent elements are compared and swapped is
a. quick sort b. Insertion sort c. selection sort d. bubble sort

5. In which sorting technique the largest element is identified after each pass and placed at the correct
position in the list.
a. quick sort b. Insertion sort c. bubble sort d. selection sort

6. In which sorting technique, all the elements of unsorted list is traversed to identify the smallest element.
a. Bubble sort b. Insertion sort c. selection sort d. Quick sort

7. In which sorting technique, all the elements of unsorted list is shifted to sorted part and placed in correct
position.
a. Bubble sort b. Insertion sort c. selection sort d. none of the above.

8. setting i=1, in insertion sort indicate.


a. index of first element in unsorted list b. index of first element in sorted list
c. index of element present in sorted list d. index of element not to get sorted

9. The amount of time taken by an algorithm to process the data is


a. time duration b. time taken c. time complexity d. time trade off

10. Time complexity of an algorithm depends on the following


a. data input b. code complexity c. data size d. logic

11. Any algorithm that does not have any loop is known as
a. Constant time algorithms. b. Linear time algorithms.
c. Quadratic time algorithms d. simple algorithms

12. Any algorithm that has one loop is known as


a. Constant time algorithms. b. Linear time algorithms.
c. Quadratic time algorithms d. simple algorithms

13. Any algorithm that have a nested loop is known as


a. Constant time algorithms. b. Linear time algorithms.
c. Quadratic time algorithms d. simple algorithms

14. Constant time algorithms have time complexity as


a. 1 b. n c. n2 [Link]

15. Linear time algorithms have time complexity as


a. 1 b. n c. n2 [Link]

16. Quadratic time algorithms have time complexity as


5
Yamuna sharma-Vijaya Vittala P U College, CS-Dept

a. 1 b. n c. n2 [Link]

17. Time complexity of bubble sort is


a. 1 b. n c. n2 [Link]

18. Time complexity of selection sort is


a. 1 b. n c. n2 [Link]

19. Time complexity of Insertion sort is


a. 1 b. n c. n2 [Link]

20. If there is a nested loop and also a single loop, the time complexity will be estimated on the basis of the
a. nested loop only. b. single loop c. no loop d. both a and b

21. In which algorithm, in each pass, the sorted list is traversed from the backward direction to find the
position where the unsorted element could be inserted.
a. Bubble sort b. Insertion sort c. selection sort d. none of the above.

22. In which algorithm, process continues until n-1 smallest elements are found and moved to their respective
places. The nth element is the last, and it is already in place.
a. Bubble sort b. Insertion sort c. selection sort d. none of the above.

23. The selection sort makes ______ number of passes through the list to sort the elements.
a. n b. n-1 c. n2 d. n-i

24. In ____ sort initially, the left list is empty, and the right list contains all the elements.
a. Bubble sort b. Insertion sort c. selection sort d. none of the above.

25. In __________ sort, there is already one element in the sorted part of the list.
a. Bubble sort b. Insertion sort c. selection sort d. none of the above.

26. The collection of string can be sorted as


a. ascending b. descending c. a-z or z-a d. all of the above

27. The list of numbers can be sorted as


a. ascending b. descending c. a-z or z-a d. both a and b

28. list=[‘a’,’b’,’z’,123,432] can be sorted as


a. [‘a’,’b’,’z’,123,432] b. [123,432, ‘a’,’b’,’z’] c. Generates an error d. none of the above

29. my_list = [5, "apple", 2, "mango", 1]


Sort(my_list)
The above code generates
[Link] list as output [Link] exception [Link] error [Link] an unsorted list

30. what is n in sorting?


a. size of the list b. length of the list c. number of elements in the list d. all of the above.

You might also like