-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveInsertionSort.qs
More file actions
40 lines (34 loc) · 1.63 KB
/
RecursiveInsertionSort.qs
File metadata and controls
40 lines (34 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace RecursiveInsertionSort {
// Define a mutable array type to store elements in Q#.
mutable array = Microsoft.Quantum.Arrays.Array;
// Function to insert an element into a sorted sublist.
operation InsertElement(sortedList : array, idx : Int, element : Int) : array {
mutable sublist = sortedList;
// Base case: If we reach the beginning of the list or the current element is greater than the element to insert,
// insert the element at the current index and return the modified sublist.
if (idx == 0 or sortedList[idx - 1] <= element) {
set sublist = InsertAt(sortedList, idx, element);
return sublist;
}
// Recursive case: Recursively move the element to the left until the correct position is found.
else {
set sublist = InsertElement(sortedList, idx - 1, element);
return sublist;
}
}
// Function to sort the entire array using recursive insertion sort.
operation RecursiveInsertionSort(inputList : array) : array {
mutable sortedList = inputList;
// Base case: If the list is empty or contains only one element, it is already sorted.
if (Length(inputList) <= 1) {
return sortedList;
}
// Recursive case: Sort the sublist from the second element to the end.
else {
set sortedList = RecursiveInsertionSort(Tail(inputList));
// Insert the first element (Head) into the sorted sublist.
set sortedList = InsertElement(sortedList, Length(sortedList), Head(inputList));
return sortedList;
}
}
}