aboutsummaryrefslogtreecommitdiff
path: root/src/sort.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sort.c')
-rw-r--r--src/sort.c174
1 files changed, 0 insertions, 174 deletions
diff --git a/src/sort.c b/src/sort.c
index 4462160..83fd0da 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -245,182 +245,8 @@ int l_bubblesort(lua_State* L) {
return 1;
}
-int l_countingsort(lua_State* L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- size_t len = lua_objlen(L,1);
- int* nums = malloc(sizeof * nums * len);
- int* out = malloc(sizeof * nums * len);
- int max = 0;
- for(int i = 0; i <= len-1; i++){
-
- lua_pushinteger(L,i+1);
- lua_gettable(L,1);
-
- nums[i] = luaL_checknumber(L, -1);
- out[i] = 0;
-
- if(nums[i]<0) p_fatal("array.countingsort(<table>) requires all indices to be >= 0");
- max = max < nums[i] ? nums[i] : max;
-
- lua_pop(L,1);
- }
-
- int* count = calloc(max + 1, sizeof * count);
-
- for(size_t i = 0; i < len; i++){
- count[nums[i]]++;
- }
-
- for(size_t i = 1; i <= max; i++){
- count[i] += count[i - 1];
- }
-
- for(int i = len - 1; i >= 0; i--){
- out[count[nums[i]] - 1] = nums[i];
- count[nums[i]]--;
- }
-
- lua_newtable(L);
- for(size_t i = 0; i != len; i++){
- lua_pushnumber(L,i+1);
- lua_pushnumber(L,out[i]);
- lua_settable(L, -3);
- }
-
- free(count);
- free(nums);
- free(out);
- return 1;
-}
-
int i_sorted(double* arr, size_t len){
for(size_t i = 0; i != len - 1; i++)
if(arr[i] > arr[i+1]) return 0;
return 1;
}
-
-int l_miraclesort(lua_State* L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- size_t len = lua_objlen(L,1);
- double* nums = malloc(sizeof * nums * len);
- for(size_t i = 0; i <= len-1; i++){
-
- lua_pushinteger(L,i+1);
- lua_gettable(L,1);
-
- nums[i] = luaL_checknumber(L, -1);
- lua_pop(L,1);
- }
-
- for(;!i_sorted(nums,len););
-
- lua_newtable(L);
- for(size_t i = 0; i != len; i++){
- lua_pushnumber(L,i+1);
- lua_pushnumber(L,nums[i]);
- lua_settable(L, -3);
- }
-
- free(nums);
- return 1;
-}
-
-int l_stalinsort(lua_State* L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- size_t len = lua_objlen(L,1);
- size_t rlen = 0;
- double* nums = malloc(sizeof * nums * len);
- for(size_t i = 0; i <= len-1; i++){
-
- lua_pushinteger(L,i+1);
- lua_gettable(L,1);
-
- double n = luaL_checknumber(L, -1);
- if(rlen == 0 || nums[rlen - 1] <= n){
- nums[rlen] = n;
- rlen++;
- }
-
- lua_pop(L,1);
- }
-
-
- lua_newtable(L);
- for(size_t i = 0; i != rlen; i++){
- lua_pushnumber(L,i+1);
- lua_pushnumber(L,nums[i]);
- lua_settable(L, -3);
- }
-
- free(nums);
- return 1;
-}
-
-void i_slowsort(double* arr, int i, int j){
- if(i >= j) return;
-
- int m = (i + j) /2;
-
- i_slowsort(arr, i, m);
- i_slowsort(arr, m + 1, j);
-
- if(arr[j] < arr[m]){
- i_swap(arr[j], arr[m]);
- }
-
- i_slowsort(arr, i, j - 1);
-}
-
-int l_slowsort(lua_State* L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- size_t len = lua_objlen(L,1);
- double* nums = malloc(sizeof * nums * len);
- for(int i = 0; i <= len-1; i++){
-
- lua_pushinteger(L,i+1);
- lua_gettable(L,1);
-
- nums[i] = luaL_checknumber(L, -1);
- lua_pop(L,1);
- }
-
- i_slowsort(nums, 0, len - 1);
-
- lua_newtable(L);
- for(size_t i = 0; i != len; i++){
- lua_pushnumber(L,i+1);
- lua_pushnumber(L,nums[i]);
- lua_settable(L, -3);
- }
-
- free(nums);
- return 1;
-}
-
-int l_bogosort(lua_State* L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- size_t len = lua_objlen(L,1);
- double* nums = malloc(sizeof * nums * len);
- for(size_t i = 0; i <= len-1; i++){
-
- lua_pushinteger(L,i+1);
- lua_gettable(L,1);
-
- nums[i] = luaL_checknumber(L, -1);
- lua_pop(L,1);
- }
-
- for(;!i_sorted(nums, len);){
- i_shuffle(nums, len);
- }
-
- lua_newtable(L);
- for(size_t i = 0; i != len; i++){
- lua_pushnumber(L,i+1);
- lua_pushnumber(L,nums[i]);
- lua_settable(L, -3);
- }
-
- free(nums);
- return 1;
-}