Skip to content

Instantly share code, notes, and snippets.

@flyq
Created January 5, 2022 12:44
Show Gist options
  • Save flyq/a21f84d34367eeffac04f93b4e6c649d to your computer and use it in GitHub Desktop.
Save flyq/a21f84d34367eeffac04f93b4e6c649d to your computer and use it in GitHub Desktop.
import Debug "mo:base/Debug";
import Nat "mo:base/Nat";
import Array "mo:base/Array";
actor {
func swap(arr: [var Int], low: Nat, high: Nat) : [var Int] {
var tmp = arr[low];
arr[low] := arr[high];
arr[high] := tmp;
return arr;
};
func quickSort(arr: [var Int], low: Nat, high: Nat): [var Int]{
var arr1 = arr;
var key = arr[low];
var start : Nat = low;
var end : Nat = high;
while(end > start){
while(end > start and arr[end] >= key) {end := end - 1};
arr1 := swap(arr, start, end);
while(end > start and arr[start] low) arr1 := quickSort(arr1, low, start - 1);
if (end < high) arr1 := quickSort(arr1, end + 1, high);
return arr1;
};
func sort(arr: [var Int]): [var Int]{
let low = 0;
let high = arr.size();
return quickSort(arr, low, high);
};
private func qsort(arr: [var Int]): async Int{
// var arr_tmp: [var Int] = [var];
// for (x in arr.vals()){
// arr_tmp := Array.append(arr_tmp, [var x]);
// };
var arr1 = sort(arr);
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment