ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

CQL: Find Kth Smallest Element

Cassandra CQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE OR REPLACE FUNCTION find_kth_smallest(
2arr list<int>,
3k int -- k is 1-based index
4)
5RETURNS int
6AS $$
7DECLARE
8n int;
9pivot_index int;
10sorted_arr list<int>;
11BEGIN
12n = list_size(arr);
13
14-- Handle edge cases: empty list, invalid k
15IF n = 0 THEN
16RETURN NULL;
17END IF;
18IF k < 1 OR k > n THEN
19RETURN NULL; -- Invalid k
20END IF;
21
22-- QuickSelect algorithm
23-- We need a mutable copy to perform in-place partitioning
24sorted_arr = arr;
25RETURN quick_select(sorted_arr, 0, n - 1, k - 1); -- k-1 for 0-based index
26END; $$
27
28CREATE OR REPLACE FUNCTION quick_select(
29arr list<int>,
30low int,
31high int,
32k_idx int -- k_idx is 0-based index
33)
34RETURNS int
35AS $$
36DECLARE
37pivot_index int;
38temp int;
39BEGIN
40-- If the list contains only one element, return that element
41IF low = high THEN
42RETURN arr[low];
43END IF;
44
45-- Partition the array around a pivot
46pivot_index = partition_for_select(arr, low, high);
47
48-- If pivot is the k-th smallest element
49IF k_idx = pivot_index THEN
50RETURN arr[k_idx];
51-- If k-th smallest element is in the left sub-array
52ELSE IF k_idx < pivot_index THEN
53RETURN quick_select(arr, low, pivot_index - 1, k_idx);
54-- If k-th smallest element is in the right sub-array
55ELSE
56RETURN quick_select(arr, pivot_index + 1, high, k_idx);
57END IF;
58END; $$
59
60CREATE OR REPLACE FUNCTION partition_for_select(
61arr list<int>,
62low int,
63high int
64)
65RETURNS int
66AS $$
67DECLARE
68pivot_value int;
69i int;
70j int;
71temp int;
72BEGIN
73-- Choose the pivot (last element in this implementation)
74pivot_value = arr[high];
75i = low - 1; -- Index of smaller element
76
77FOR j = low TO high - 1 DO
78-- If current element is smaller than or equal to pivot
79IF arr[j] <= pivot_value THEN
80i = i + 1;
81-- Swap arr[i] and arr[j]
82temp = arr[i];
83arr[i] = arr[j];
84arr[j] = temp;
85END IF;
86END FOR;
87
88-- Swap arr[i+1] and arr[high] (put pivot in its correct place)
89temp = arr[i + 1];
90arr[i + 1] = arr[high];
91arr[high] = temp;
92
93RETURN i + 1; -- Return the partitioning index
94END; $$
95
96-- Example Usage:
97-- SELECT find_kth_smallest([3, 2, 1, 5, 6, 4], 2); -- Expected: 2
98-- SELECT find_kth_smallest([3, 2, 3, 1, 2, 4, 5, 5, 6], 4); -- Expected: 3
99-- SELECT find_kth_smallest([1], 1); -- Expected: 1
100-- SELECT find_kth_smallest([], 1); -- Expected: NULL
101-- SELECT find_kth_smallest([1, 2, 3], 4); -- Expected: NULL
Algorithm description viewbox

CQL: Find Kth Smallest Element

Algorithm description:

This Cassandra CQL function implements the QuickSelect algorithm to find the Kth smallest element in a list of integers. It's a selection algorithm derived from QuickSort. By partitioning the list and recursively searching only the partition that contains the Kth element, it achieves better average time complexity than sorting the entire list. This is useful for finding order statistics efficiently.

Algorithm explanation:

QuickSelect is an efficient algorithm for finding the Kth smallest element. It's based on the partitioning logic of QuickSort. The `find_kth_smallest` function initializes the process, handling edge cases like empty lists or invalid `k` values. The core logic is in `quick_select`, which partitions the array using `partition_for_select`. If the pivot's index is `k-1` (0-based), the pivot is the Kth smallest element. If `k-1` is less than the pivot index, the search continues in the left partition; otherwise, it continues in the right partition. The average time complexity is O(N), significantly better than O(N log N) for sorting. The worst-case time complexity is O(N^2), similar to QuickSort, occurring with poor pivot choices. Space complexity is O(log N) on average due to recursion, and O(N) in the worst case. Edge cases: empty list, invalid `k`, single-element list.

Pseudocode:

FUNCTION find_kth_smallest(arr, k):
  n = size of arr
  IF n is 0 OR k < 1 OR k > n THEN
    RETURN NULL
  END IF
  RETURN quick_select(arr, 0, n - 1, k - 1) -- k-1 for 0-based index
END FUNCTION

FUNCTION quick_select(arr, low, high, k_idx):
  IF low = high THEN
    RETURN arr[low]
  END IF

  pivot_index = partition_for_select(arr, low, high)

  IF k_idx = pivot_index THEN
    RETURN arr[k_idx]
  ELSE IF k_idx < pivot_index THEN
    RETURN quick_select(arr, low, pivot_index - 1, k_idx)
  ELSE
    RETURN quick_select(arr, pivot_index + 1, high, k_idx)
  END IF
END FUNCTION

FUNCTION partition_for_select(arr, low, high):
  -- (Same partition logic as QuickSort)
  pivot_value = arr[high]
  i = low - 1
  FOR j from low to high - 1:
    IF arr[j] <= pivot_value THEN
      i = i + 1
      SWAP arr[i] and arr[j]
    END IF
  END FOR
  SWAP arr[i + 1] and arr[high]
  RETURN i + 1
END FUNCTION