Problemset: Sorting Algorithms
1. Find the kth smallest element from an array.
Input Output
a = [6,5,1,3,4], k = 2 3
a = [7,9,1,8,2] , k = 4 8
2. Find the median from an array.
Input Output
a = [5,2,1,4,7] 4
a = [11,9,17,15] 13
3. Find the smallest difference of elements from an array.
Input Output
a = [15,7,2,12,13] 1
a = [2,13,11,19,5] 2
4. Sort an array by absolute value in ascending order.
Input Output
a = [-10, 5, -3, 7, -2] [-2, -3, 5, 7, -10]
5. Sort an Array of Strings according to length.
Input Output
a = ["apple", "bat", "carrot", "dog"] ["bat", "dog", "apple", "carrot"]
6. Sort odd indexed elements of an array in descending order and
even indexed elements in ascending order.
Input Output
a = [16,17,4,18,1,20,5,12] [1,20,4,18,5,17,16,12]
a = [4,3,2,1] [2,3 ,4,1]
7. Find the Longest Consecutive Subsequence after sorting an array.
Input Output
a = [1, 9, 3, 10, 4, 20, 2] 4
a = [3,1] 1
8. Sort an array based on the frequency of its elements. If two
elements have the same frequency, sort them in ascending order.
Input Output
a = [4, 3, 1, 6, 1, 3, 4, 4] [4, 4, 4, 1, 1, 3, 3, 6]
9. You are given two arrays a and b. Merge a and b into a single array
sorted in non-decreasing order.
Input Output
a = [1,16,13,14,4] [1,3,4,5,7,12,13,14,15,16]
b = [15,12,5,3,7]
10. You are given a list of tasks, where each task is represented as
(StartTime, FinishTime). The goal is to sort the tasks based on their
durations (calculated as FinishTime - StartTime) in ascending
order.
Input Output
[(2, 5), (1, 2), (4, 6)] [(1, 3), (4, 6), (2, 5)]