Skip to content

Commit df875ee

Browse files
committed
Update: added Object and reverse ordering
1 parent ba4123e commit df875ee

1 file changed

Lines changed: 257 additions & 0 deletions

File tree

SortingAlgorithm/Java/BinaryInsertionSort/BinaryInsertionSort.java

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package BinaryInsertionSort;
22

3+
import java.util.Comparator;
4+
5+
import Utils.Convert;
6+
import Utils.Order;
7+
38
/**
49
*
510
* @author kdgyun
@@ -625,5 +630,257 @@ private static void reversing(double[] a, int lo, int hi) {
625630
a[hi--] = temp;
626631
}
627632
}
633+
634+
635+
636+
637+
/*==========================
638+
* sorting Object type array
639+
*==========================*/
640+
641+
public static <T> void sort(T[] a, Comparator<? super T> c) {
642+
643+
if(c == null) {
644+
sort(a);
645+
}
646+
else {
647+
if(a.length < 2) {
648+
return;
649+
}
650+
int incLength = getAscending(a, 0, a.length, c);
651+
ComparatorSort(a, 0, a.length, incLength, c);
652+
653+
}
654+
}
655+
656+
// Comparable sort
657+
public static void sort(Object[] a) {
658+
if(a.length < 2) {
659+
return;
660+
}
661+
int incLength = getAscending(a, 0, a.length);
662+
ComparableSort(a, 0, a.length, incLength);
663+
}
664+
665+
private static void ComparableSort(Object[] a, int lo, int hi, int start) {
666+
if(lo == start) {
667+
start++;
668+
}
669+
670+
for (; start < hi; start++) {
671+
@SuppressWarnings("rawtypes")
672+
Comparable target = (Comparable)a[start];
673+
674+
int loc = binarySearch(a, target, 0, start);
675+
676+
int j = start - 1;
677+
678+
while (j >= loc) {
679+
a[j + 1] = a[j];
680+
j--;
681+
}
682+
683+
a[loc] = target;
684+
}
685+
}
686+
687+
@SuppressWarnings({ "rawtypes", "unchecked" })
688+
private static int binarySearch(Object[] a, Comparable key, int lo, int hi) {
689+
690+
int mid;
691+
while (lo < hi) {
692+
mid = (lo + hi) >>> 1;
693+
if (key.compareTo(a[mid]) < 0) {
694+
hi = mid;
695+
}
696+
else {
697+
lo = mid + 1;
698+
}
699+
}
700+
return lo;
701+
}
702+
703+
@SuppressWarnings({ "unchecked", "rawtypes" })
704+
private static int getAscending(Object[] a, int lo, int hi) {
705+
706+
int limit = lo + 1;
707+
if (limit == hi) {
708+
return 1;
709+
}
710+
711+
if (((Comparable)a[lo]).compareTo(a[limit]) <= 0) {
712+
while (limit < hi && ((Comparable)a[limit - 1]).compareTo(a[limit]) <= 0) {
713+
limit++;
714+
}
715+
}
716+
else {
717+
while (limit < hi && ((Comparable)a[limit - 1]).compareTo(a[limit]) > 0) {
718+
limit++;
719+
}
720+
reversing(a, lo, limit);
721+
}
722+
723+
return limit - lo;
724+
}
725+
726+
727+
728+
729+
// Comparator sort
730+
private static <T> void ComparatorSort(T[] a, int lo, int hi, int start, Comparator<? super T> c) {
731+
if(lo == start) {
732+
start++;
733+
}
734+
735+
for (; start < hi; start++) {
736+
T target = a[start];
737+
738+
int loc = binarySearch(a, target, 0, start, c);
739+
740+
int j = start - 1;
741+
742+
while (j >= loc) {
743+
a[j + 1] = a[j];
744+
j--;
745+
}
746+
747+
a[loc] = target;
748+
}
749+
}
750+
751+
private static <T> int binarySearch(T[] a, T key, int lo, int hi, Comparator<? super T> c) {
752+
753+
int mid;
754+
while (lo < hi) {
755+
mid = (lo + hi) >>> 1;
756+
if (c.compare(key, a[mid]) < 0) {
757+
hi = mid;
758+
}
759+
else {
760+
lo = mid + 1;
761+
}
762+
}
763+
return lo;
764+
}
765+
766+
private static <T> int getAscending(T[] a, int lo, int hi, Comparator<? super T> c) {
767+
768+
int limit = lo + 1;
769+
if (limit == hi) {
770+
return 1;
771+
}
772+
773+
if (c.compare(a[lo], a[limit]) <= 0) {
774+
while (limit < hi && c.compare(a[limit - 1], a[limit]) <= 0) {
775+
limit++;
776+
}
777+
}
778+
else {
779+
while (limit < hi && c.compare(a[limit - 1], a[limit]) > 0) {
780+
limit++;
781+
}
782+
reversing(a, lo, limit);
783+
}
784+
785+
return limit - lo;
786+
}
787+
788+
789+
private static <T> void reversing(T[] a, int lo, int hi) {
790+
hi--;
791+
while (lo < hi) {
792+
T temp = a[lo];
793+
a[lo++] = a[hi];
794+
a[hi--] = temp;
795+
}
796+
}
797+
798+
799+
800+
/*
801+
* ==========================
802+
* reverse ordering
803+
* ==========================
804+
*/
805+
806+
807+
public static void sort(byte[] a, boolean isReverse) {
808+
if(isReverse) {
809+
Byte[] b = Convert.toByteArray(a);
810+
sort(b, Order.reverseOrder());
811+
Convert.tobyteArray(b, a);
812+
}
813+
else {
814+
sort(a);
815+
}
816+
}
817+
818+
public static void sort(char[] a, boolean isReverse) {
819+
if(isReverse) {
820+
Character[] b = Convert.toCharacterArray(a);
821+
sort(b, Order.reverseOrder());
822+
Convert.tocharArray(b, a);
823+
}
824+
else {
825+
sort(a);
826+
}
827+
}
828+
829+
public static void sort(short[] a, boolean isReverse) {
830+
if(isReverse) {
831+
Short[] b = Convert.toShortArray(a);
832+
sort(b, Order.reverseOrder());
833+
Convert.toshortArray(b, a);
834+
}
835+
else {
836+
sort(a);
837+
}
838+
}
839+
840+
public static void sort(int[] a, boolean isReverse) {
841+
if(isReverse) {
842+
Integer[] b = Convert.toIntegerArray(a);
843+
sort(b, Order.reverseOrder());
844+
Convert.tointtArray(b, a);
845+
}
846+
else {
847+
sort(a);
848+
}
849+
}
850+
851+
852+
public static void sort(long[] a, boolean isReverse) {
853+
if(isReverse) {
854+
Long[] b = Convert.toLongArray(a);
855+
sort(b, Order.reverseOrder());
856+
Convert.tolongArray(b, a);
857+
}
858+
else {
859+
sort(a);
860+
}
861+
}
862+
863+
public static void sort(float[] a, boolean isReverse) {
864+
if(isReverse) {
865+
Float[] b = Convert.toFloatArray(a);
866+
sort(b, Order.reverseOrder());
867+
Convert.toflostArray(b, a);
868+
}
869+
else {
870+
sort(a);
871+
}
872+
}
873+
874+
public static void sort(double[] a, boolean isReverse) {
875+
if(isReverse) {
876+
Double[] b = Convert.toDoubleArray(a);
877+
sort(b, Order.reverseOrder());
878+
Convert.todoubleArray(b, a);
879+
}
880+
else {
881+
sort(a);
882+
}
883+
}
884+
628885
}
629886

0 commit comments

Comments
 (0)