0% found this document useful (0 votes)
5 views9 pages

Selection Sort Programming Tasks

soting question
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)
5 views9 pages

Selection Sort Programming Tasks

soting question
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

Selection Sort Assignment

Que 1. Write a program to sort an array in non


decreasing(increasing) order using Selection Sort.
Example
Input: arr = [45, 12, 89, 23, 5, 67]
Output: [5, 12, 23, 45, 67, 89]
Explanation: The algorithm repeatedly finds the
smallest element from the unsorted portion of the
array and swaps it with the first unsorted element. This
continues until the entire array is sorted in non
decreasing (ascending) order.

Que 2. Write a program to sort an array in decreasing


order using Selection Sort.
Example
Input: [10, 50, 1, 29, 32, 12, 43]
Output: [50, 43, 32, 29, 12, 10, 1]

Explanation: In this case, Selection Sort repeatedly


finds the largest element from the unsorted part of the
array and swaps it with the first unsorted element,
continuing the process until the entire array is sorted in
decreasing order.

Que 3. You are given an array of integers and a positive


integer k. Use the Selection Sort algorithm to find the
Kth smallest element in the array.
Input: arr = [7, 10, 4, 3, 20, 15], k = 3
Output: 7
Explanation: After sorting the array in ascending order
using Selection Sort, the array becomes [3, 4, 7, 10, 15,
20].
The 3rd smallest element is 7.
Que 4. Given two arrays: one with student names and
another with their scores, sort the students by their
scores using Selection Sort.

Example
Input: names = ["Alice", "Bob", "Charlie"], scores = [50,
80, 70]
Output: ["Alice", "Charlie", "Bob"]
Explanation: The students are sorted by their scores in
descending order, resulting in ["Bob", "Charlie",
"Alice"] based on the association between scores and
names.

Que 5. Sort only the elements at even indices of the


array in ascending order using Selection Sort, while
keeping the odd-indexed elements unchanged.
Example
Input: arr = [4, 1, 7, 2, 3, 5]
Output: [3, 1, 4, 2, 7, 5]
Explanation: The even-indexed elements [4, 7, 3] are
sorted as [3, 4, 7], while the odd-indexed elements [1,
2, 5] remain unchanged.

Que 6. Sort Even and Odd Indices Independently


You are given a 0-indexed integer array nums.
Rearrange the values of nums according to the
following rules:
1. Sort the values at odd indices of nums in non
increasing order.
• For example, if nums = [4,1,2,3] before this
step, it becomes [4,3,2,1] after. The values at
odd indices 1 and 3 are sorted in non
increasing order.
2. Sort the values at even indices of nums in non
decreasing order.
• For example, if nums = [4,1,2,3] before this
step, it becomes [2,1,4,3] after. The values at
even indices 0 and 2 are sorted in non
decreasing order.
Return the array formed after rearranging the values
of nums.
Example
Input: nums = [4,1,2,3]
Output: [2,3,4,1]

Explanation:
First, we sort the values present at odd indices (1 and
3) in non-increasing order.
So, nums changes from [4,1,2,3] to [4,3,2,1].
Next, we sort the values present at even indices (0 and
2) in non-decreasing order.
So, nums changes from [4,1,2,3] to [2,3,4,1].
Thus, the array formed after rearranging the values is
[2,3,4,1].
Que 7. Given an array of integers nums, sort the array
in increasing order based on the frequency of the
values. If multiple values have the same frequency,
sort them in decreasing order.
Return the sorted array.
Example
Input: nums = [1,1,2,2,2,3]
Output: [3,1,1,2,2,2]
Explanation: '3' has a frequency of 1, '1' has a
frequency of 2, and '2' has a frequency of 3.

Input: nums = [2,3,1,3,2]


Output: [1,3,3,2,2]
Explanation: '2' and '3' both have a frequency of 2, so
they are sorted in decreasing order.
Input: nums = [-1,1,-6,4,5,-6,1,4,1]
Output: [5,-1,4,4,-6,-6,1,1,1]
Explanation: 1 has the highest frequency of 3, 4 and -6
also appears twice whereas 5 and -1 appears once. The
result is [5, -1, 4, 4, -6, -6, 1, 1, 1] where numbers are
sorted based on frequency and in descending order for
ties.

Que 8. Sort the array based on the units digit of each


number in ascending order using Selection Sort.
Example:
Input: arr = [42, 31, 63, 74, 25]
Output: [31, 42, 63, 74, 25]
Explanation: Sorting is based on units digit: [1, 2, 3, 4,
5].

Que 9. Sort the array in ascending order based on the


sum of the digits of each number using Selection Sort.
Example
Input: arr = [23, 45, 12, 56, 34]
Output: [12, 23, 34, 45, 56]
Explanation:
• Input Array: [23, 45, 12, 56, 34]
• Sum of digits of each element: [5, 9, 3, 11, 7]
• Sorted sums in ascending order: [3, 5, 7, 9, 11]
• Rearranged array based on sorted sums: [12, 23,
34, 45, 56]

Que 10. Sort the array in ascending order based on the


number of digits in each number. If multiple numbers
have the same number of digits, sort them in
ascending order.
Example
Input: arr = [123, 45, 6, 7890, 12]

Output: [6, 12, 45, 123, 7890]


Explanation:
• Number of digits: [3, 2, 1, 4, 2].
Sorting first by the number of digits gives [1, 2, 2,

3, 4].
For numbers with the same number of digits (like

12 and 45), they are sorted in ascending order.


• The final sorted array is: [6, 12, 45, 123, 7890].

You might also like