-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/12 18:33:50 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/12 20:54:48 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -45,7 +45,7 @@ void large_sort(t_stack **p_a, t_stack **p_b) | |
if (size_a < 200) | ||
_push_b_n_times(p_a, p_b, size_b); | ||
else | ||
push_b_segmented(p_a, p_b, size_b, 5); | ||
push_b_segmented(p_a, p_b, size_b, 7); | ||
nano_sort(p_a); | ||
while (size_b--) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/02/11 22:57:28 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/12 18:32:39 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/12 23:28:29 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -28,56 +28,65 @@ | |
// [72, 96) -> [seg_size * 3, seg_size * 4) | ||
// [96, 123) -> [seg_size * 4, N) | ||
|
||
static int _calc_segment_id(int pushed, int n, int num_seg) | ||
static int _calc_segment_id(int target, int n, int segs) | ||
{ | ||
int seg_size; | ||
int id; | ||
|
||
if (num_seg <= 0) | ||
if (segs <= 0) | ||
return (-1); | ||
seg_size = n / num_seg; | ||
seg_size = n / segs; | ||
id = 0; | ||
while (id < num_seg) | ||
while (id < segs) | ||
{ | ||
if (id * seg_size <= pushed && pushed < (id + 1) * seg_size) | ||
if (id * seg_size <= target && target < (id + 1) * seg_size) | ||
return (id); | ||
id++; | ||
} | ||
if (pushed >= seg_size * num_seg) | ||
return (num_seg - 1); | ||
if (target >= seg_size * segs) | ||
return (segs - 1); | ||
return (-1); | ||
} | ||
|
||
// ft_printf("ns: %d, index: %d, pushed: %d, n: %d\n", num_segment, index, | ||
// pushed, n); | ||
static bool _should_push_b(int index, int pushed, int n, int num_seg) | ||
static bool _should_push_b(int index, int pushed, int n, int segs) | ||
{ | ||
int seg_size; | ||
int cur_id; | ||
int inf; | ||
int sup; | ||
|
||
seg_size = n / num_seg; | ||
cur_id = _calc_segment_id(pushed, n, num_seg); | ||
inf = seg_size * cur_id; | ||
sup = seg_size * (cur_id + 1); | ||
if (cur_id == num_seg - 1) | ||
sup = n; | ||
seg_size = n / segs; | ||
cur_id = _calc_segment_id(pushed, n, segs); | ||
if (cur_id % 2 == 0) | ||
{ | ||
inf = seg_size * cur_id; | ||
sup = seg_size * (cur_id + 2); | ||
} | ||
else | ||
{ | ||
inf = seg_size * (cur_id - 1); | ||
sup = seg_size * (cur_id + 1); | ||
} | ||
if (cur_id == segs - 1) | ||
return (inf < index && index <= n); | ||
return (inf < index && index <= sup); | ||
} | ||
|
||
void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int num_seg) | ||
void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs) | ||
{ | ||
int pushed; | ||
int index; | ||
|
||
pushed = 0; | ||
num_seg = 5; | ||
while (pushed < n) | ||
{ | ||
if (_should_push_b(get_content(*p_a)->index, pushed, n, num_seg)) | ||
index = get_content(*p_a)->index; | ||
if (_should_push_b(index, pushed, n, segs)) | ||
{ | ||
operate_pb(p_a, p_b); | ||
if (_calc_segment_id(pushed, n, num_seg) % 2 == 1) | ||
if (_calc_segment_id(index - 1, n, segs) % 2 == 1) | ||
operate_rb(p_b); | ||
pushed++; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters