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

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

CQL: Implement Merge Sort

Cassandra CQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE OR REPLACE FUNCTION merge_sort(
2arr list<int>
3)
4RETURNS list<int>
5AS $$
6DECLARE
7n int;
8mid int;
9left_half list<int>;
10right_half list<int>;
11merged_list list<int>;
12BEGIN
13n = list_size(arr);
14
15-- Base case: if the list has 0 or 1 element, it's already sorted
16IF n <= 1 THEN
17RETURN arr;
18END IF;
19
20-- Divide the list into two halves
21mid = n / 2;
22left_half = list(arr, 0, mid - 1);
23right_half = list(arr, mid, n - 1);
24
25-- Recursively sort both halves
26left_half = merge_sort(left_half);
27right_half = merge_sort(right_half);
28
29-- Merge the sorted halves
30merged_list = merge(left_half, right_half);
31
32RETURN merged_list;
33END; $$
34
35CREATE OR REPLACE FUNCTION merge(
36left list<int>,
37right list<int>
38)
39RETURNS list<int>
40AS $$
41DECLARE
42merged list<int>;
43i int;
44j int;
45n_left int;
46n_right int;
47BEGIN
48merged = [];
49i = 0;
50j = 0;
51n_left = list_size(left);
52n_right = list_size(right);
53
54-- Compare elements from left and right lists and add the smaller one to merged list
55WHILE i < n_left AND j < n_right DO
56IF left[i] <= right[j] THEN
57merged = merged + [left[i]];
58i = i + 1;
59ELSE
60merged = merged + [right[j]];
61j = j + 1;
62END IF;
63END WHILE;
64
65-- Add any remaining elements from the left list
66WHILE i < n_left DO
67merged = merged + [left[i]];
68i = i + 1;
69END WHILE;
70
71-- Add any remaining elements from the right list
72WHILE j < n_right DO
73merged = merged + [right[j]];
74j = j + 1;
75END WHILE;
76
77RETURN merged;
78END; $$
79
80-- Helper function to extract a sublist (simulating slicing)
81-- CQL's list slicing syntax is `list(original_list, start_index, end_index)`
82-- where end_index is inclusive.
83-- Example: list(my_list, 0, 2) gets elements at index 0, 1, 2.
84
85-- Example Usage:
86-- SELECT merge_sort([38, 27, 43, 3, 9, 82, 10]);
87-- SELECT merge_sort([1]);
88-- SELECT merge_sort([]);
Algorithm description viewbox

CQL: Implement Merge Sort

Algorithm description:

This Cassandra CQL implementation of Merge Sort recursively divides a list of integers into halves, sorts each half, and then merges the sorted halves back together. The `merge` helper function is crucial for combining two sorted lists into a single sorted list. This algorithm is stable and has a guaranteed time complexity, making it suitable for sorting large datasets in Cassandra.

Algorithm explanation:

Merge Sort is a divide-and-conquer sorting algorithm. The `merge_sort` function recursively splits the input list `arr` into `left_half` and `right_half` until the sub-lists contain at most one element (the base case). It then calls the `merge` function to combine these sorted sub-lists. The `merge` function takes two sorted lists, `left` and `right`, and iterates through them, comparing elements and appending the smaller one to a new `merged` list. It continues until one list is exhausted, then appends the remaining elements of the other list. Time complexity is O(N log N) for both best, average, and worst cases, due to the consistent division and merging steps. Space complexity is O(N) because of the auxiliary space required for merging. Edge cases handled: empty lists and single-element lists are base cases for recursion.

Pseudocode:

FUNCTION merge_sort(arr):
  n = size of arr

  IF n <= 1 THEN
    RETURN arr
  END IF

  mid = n / 2
  left_half = arr from index 0 to mid-1
  right_half = arr from index mid to n-1

  sorted_left = merge_sort(left_half)
  sorted_right = merge_sort(right_half)

  RETURN merge(sorted_left, sorted_right)
END FUNCTION

FUNCTION merge(left, right):
  merged_list = empty list
  i = 0
  j = 0

  WHILE i < size of left AND j < size of right:
    IF left[i] <= right[j] THEN
      append left[i] to merged_list
      i = i + 1
    ELSE
      append right[j] to merged_list
      j = j + 1
    END IF
  END WHILE

  APPEND remaining elements of left to merged_list
  APPEND remaining elements of right to merged_list

  RETURN merged_list
END FUNCTION