1+ package sorts ;
2+
3+ import templates .Sort ;
4+ import utils .Delays ;
5+ import utils .Highlights ;
6+ import utils .Reads ;
7+ import utils .Writes ;
8+
9+ /*
10+ *
11+ MIT License
12+
13+ Copyright (c) 2019 w0rthy
14+
15+ Permission is hereby granted, free of charge, to any person obtaining a copy
16+ of this software and associated documentation files (the "Software"), to deal
17+ in the Software without restriction, including without limitation the rights
18+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19+ copies of the Software, and to permit persons to whom the Software is
20+ furnished to do so, subject to the following conditions:
21+
22+ The above copyright notice and this permission notice shall be included in all
23+ copies or substantial portions of the Software.
24+
25+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31+ SOFTWARE.
32+ *
33+ */
34+
35+ final public class SmartCocktailSort extends Sort {
36+ public SmartCocktailSort (Delays delayOps , Highlights markOps , Reads readOps , Writes writeOps ) {
37+ super (delayOps , markOps , readOps , writeOps );
38+
39+ this .setSortPromptID ("Smart Cocktail" );
40+ this .setRunAllID ("Optimized Cocktail Shaker Sort" );
41+ this .setReportSortID ("Optimized Cocktail Shaker Sort" );
42+ this .setCategory ("Exchange Sorts" );
43+ this .isComparisonBased (true );
44+ this .isBucketSort (false );
45+ this .isRadixSort (false );
46+ this .isUnreasonablySlow (false );
47+ this .setUnreasonableLimit (0 );
48+ this .isBogoSort (false );
49+ }
50+
51+ private void smartCocktailShaker (int [] array , int start , int end , double sleep ) {
52+ int i = start ;
53+ while (i < ((end / 2 ) + start )) {
54+ boolean sorted = true ;
55+ for (int j = i ; j < end + start - i - 1 ; j ++) {
56+ if (Reads .compare (array [j ], array [j + 1 ]) == 1 ) {
57+ Writes .swap (array , j , j + 1 , sleep , true , false );
58+ sorted = false ;
59+ }
60+
61+ Highlights .markArray (1 , j );
62+ Highlights .markArray (2 , j + 1 );
63+
64+ Delays .sleep (sleep / 2 );
65+ }
66+ for (int j = end + start - i - 1 ; j > i ; j --){
67+ if (Reads .compare (array [j ], array [j - 1 ]) == -1 ) {
68+ Writes .swap (array , j , j - 1 , sleep , true , false );
69+ sorted = false ;
70+ }
71+
72+ Highlights .markArray (1 , j );
73+ Highlights .markArray (2 , j - 1 );
74+
75+ Delays .sleep (sleep / 2 );
76+ }
77+ if (sorted ) break ;
78+ else i ++;
79+ }
80+ }
81+
82+ public void customSort (int [] array , int start , int end ) {
83+ this .smartCocktailShaker (array , start , end , 1 );
84+ }
85+
86+ @ Override
87+ public void runSort (int [] array , int length , int bucketCount ) {
88+ this .smartCocktailShaker (array , 0 , length , 0.1 );
89+ }
90+ }
0 commit comments