Problem Descriptions:
The following tasks need to be solved using Sorting Algorithms. Sorting algorithm is an algorithm that is used to arrange data in certain order, i,e- either ascending or descending order. We use sorting algorithms on numerical and lexicographical data to optimize the efficiency of other algorithms. You have learned several sorting algorithms such as bubble sort, insertion sort, selection sort, quick sort,merge sort etc. The problems below are related or similar to some of the sorting algorithms you learned in class. Read the task descriptions carefully and implement the algorithm using either Java or Python. The output format MUST match exactly as shown in each task.
Task 1:
Here is code of bubble sort. Itβs run time complexity is ΞΈ(n2). Change the code in a way so that its time complexity is ΞΈ(n) for the best case scenario. Find the best case and change the code a little bit. And explain how you did it in a comment block of your code.
def bubbleSort(arr):
for i in range(len(arr)-1):
for j in range(len(arr)-i-1):
if arr[j] > arr[j+1]:
swap( arr[j+1], arr[j] )
The first line of the input will contain N, which is the size of the array. Next line will contain the N number of elements. Output will contain the sorted elements.
P.S: sample input and output may not be the preferred answer choice.
|
Input 1: 5 |
Input 2: |
|
Output 1: 1 234 5 |
Output 2: |
Task 2:
You have a list of elements and their prices. Select your preferred lists from the item based on lowest price. So to complete the task you have a tool called selection sort. In selection sort:
- β Β Select the minimum element from the unsorted part of the given array.
- β Β Move the selected element to a sorted part of the array.
- β Β Repeat this process to make the unsorted array sorted.
Here is pseudo code:
ππππβ0π‘ππ β 1
π = πππππππ (π΄[π], π΄[π + 1], ……. π΄[π β 1])π π€ππ (π΄[π], π΄[π]) πππ πππ
Use the above pseudo code to complete the selection sort.
First line of the input will contain N items and M preferred choices (where M β€ N). The next line will contain the price of each element. Output will contain the price of M number of preferred elements.
|
Input 1: 53 |
Input 2: 74 |
|
5 10 2 1 4 |
10 2 3 4 1 100 1 |
|
Output 1: 1 24 |
Output 2: 1 123 |
Task 3:
Suppose you are given a task to rank the students. You have gotten the marks and id of the students. Now your task is to rank the students based on their marks using only insertion sort.
Here is the pseudocode for insertion sort.
ππππβ0π‘ππ β 1 π‘πππ β π΄[π + 1] ππππβππ‘π0
ππ(π΄[π] > π‘πππ) π΄[π + 1] βπ΄[π]
πππ π πππππ
πππ πππ
π΄[π + 1] β π‘πππ
πππ πππ
Implement this pseudocode to complete your task.
First line will contain an integer N. The next line will contain N number of id of the students.The next line will contain the N number of the marks of corresponding students. Output will be ranking the id based on their marks (descending order).
|
Input 1: |
Input 2: |
|
Output 1: |
Output 2: 3 2 1 6 45 |
Task 4:
Here is the problem, just simply sorting an array. Now, to sort the array you should use efficient sorting techniques. It will have worst-case time complexity better than the above sorting algorithms. The sorting algorithm pseudocode is given below:
ππΈπ πΊπΈ (π΄, π, π, π ) π1βπ β π + 1 π2βπ β π
πΆππππ‘ππππππ¦π πΏ[1. . π1 + 1]ππππ [1. . π2 + 1] πΉππ πβ1π‘ππ1
π·ππΏ[π]βπ΄[π + π β 1] πΉππ πβ1π‘π π2
π·ππ [π]βπ΄[π + π] πΏ[π1 + 1]ββ
π
[π2 + 1]ββ πβ1
πβ1
πΉππ πβππππ π·ππΌπΉπΏ[π]β€π [π]
ππ»πΈπ π΄[π] β πΏ[π] πβπ+1 πΈπΏππΈ π΄[π] β π [π]
πβπ+1
ππΈπ πΊπΈ β πππ π (π΄, π, π) πππ < π
//πΆhπππππππππ ππππ π
π = (π + π)/2
ππΈπ πΊπΈ β πππ π (π΄, π, π) ππΈπ πΊπΈ β πππ π(π΄, π + 1, π) ππΈπ πΊπΈ (π΄, π, π, π)
Take help from pseudocode or use your way to complete the task.
First line will contain N . The next line will contain N number of elements. The output will contain a sorted N number of elements (ascending order).
|
Input 1: |
Input 2: |
|
Output 1: -1 2 5 8 10 |
Output 2: |







