public class ArrayUtils { final private int[] array; final private int[] sortedArray; long switchCount, compareCount, timeAmount; public ArrayUtils(int[] array) { this.array = array; this.sortedArray = Arrays.copyOf(array, array.length); Arrays.sort(sortedArray); } void results(String text) { System.out.println(String.format("%-35s Compares: %, 15d, Switches: %, 15d, Time: %, 15d", text, compareCount, switchCount, timeAmount)); } }
boolean validate() { if (Arrays.equals(array,sortedArray)) return true; return false; }
public void sortBubbleClassic() { int currentPosition; int maxPosition; int temp; switchCount=0; compareCount=0; time = System.nanoTime(); for (maxPosition=array.length - 1; maxPosition >= 0;maxPosition--) { for (currentPosition = 0; currentPosition < maxPosition; currentPosition++) { compareCount++; if (array[currentPosition] > array[currentPosition+1]) { temp = array[currentPosition]; array[currentPosition] = array[currentPosition+1]; array[currentPosition+1] = temp; switchCount++; } } } time = System.nanoTime() - time; assert(validate()); return; }
public class Run { static public void FillRandom(int[] array) { for (int count=0; count < array.length; count++) { array[count] = (int)(Math.random()*100); } } public static void main(String[] args) { int arraySize= 10000; int random[] = new int[arraySize+1]; FillRandom(random); ArrayUtils bubbleClassic = new ArrayUtils(random); bubbleClassic.sortBubbleClassic(); bubbleClassic.results("Bubble Classic, random array"); } }
Bubble Classic, random array Compares: 50 005 000, Switches: 24 486 908, Time: 117 116 326
public void sortBubbleClassic() { int currentPosition; int maxPosition; int temp; switchCount=0; compareCount=0; timeAmount = System.nanoTime(); Boolean changed; // for (maxPosition=array.length - 1; maxPosition >= 0;maxPosition--) { changed=false; // for (currentPosition = 0; currentPosition < maxPosition; currentPosition++) { compareCount++; if (array[currentPosition] > array[currentPosition+1]){ temp = array[currentPosition]; array[currentPosition] = array[currentPosition+1]; array[currentPosition+1] = temp; switchCount++; changed = true; // } } if (!changed) { // - timeAmount = System.nanoTime() - timeAmount; assert(validate()); return; } } timeAmount = System.nanoTime() - timeAmount; assert(validate()); return; }
public void sortBubbleAdvanced(){ int currentPosition; int maxPosition; // int minPosition = 0; // boolean changed; // , int temp; switchCount = 0; compareCount = 0; timeAmount = System.nanoTime(); for (maxPosition = array.length - 1; maxPosition >= 0; maxPosition--) { changed=false; for (currentPosition = minPosition; currentPosition < maxPosition; currentPosition++) { if (array[currentPosition] > array[currentPosition+1]){ // temp = array[currentPosition+1]; array[currentPosition+1] = array[currentPosition]; array[currentPosition] = temp; switchCount++; changed=true; } if (array[currentPosition] < array[minPosition]){ // temp = array[minPosition]; array[minPosition] = array[currentPosition]; array[currentPosition] = temp; switchCount++; changed=true; } if (array[currentPosition] > array[maxPosition]){ // temp = array[maxPosition]; array[maxPosition] = array[currentPosition]; array[currentPosition] = temp; switchCount++; changed=true; } compareCount+=3; } if (!changed) { timeAmount=System.nanoTime()-timeAmount; assert(validate()); return; } compareCount++; minPosition++ // } timeAmount=System.nanoTime()-timeAmount; assert(validate()); return; }
Bubble Classic, random array Compares: 50 005 000, Switches: 24 758 509, Time: 105 797 881 Bubble Advanced, random array Compares: 112 317 213, Switches: 18 684 909, Time: 87 415 460
static public void fillDecremental(int[] array) { for (int count=0; count < array.length; count++) { array[count] = array.length-count; } } static public void fillIncremental(int[] array) { for (int count=0; count < array.length; count++) { array[count] = count; } }
Bubble Classic, random array Compares: 50 005 000, Switches: 24 678 169, Time: 116 314 053 Bubble Advanced, random array Compares: 111 879 748, Switches: 18 615 013, Time: 77 282 419 Bubble Classic, decremental array Compares: 50 005 000, Switches: 50 005 000, Time: 48 202 818 Bubble Advanced, decremental array Compares: 112 527 500, Switches: 49 985 004, Time: 77 115 071 Bubble Classic, incremental array Compares: 50 005 000, Switches: 0, Time: 24 805 261 Bubble Advanced, incremental array Compares: 30 000, Switches: 0, Time: 35 084
public void sortBubbleAdvanced(){ int currentPosition; int maxPosition; int changedMaxPosition = array.length - 1; // int minPosition = 0; boolean changed; int temp; switchCount = 0; compareCount = 0; timeAmount = System.nanoTime(); for (maxPosition = array.length - 1; maxPosition >= 0; minPosition++) // , minPosition++ { changed=false; for (currentPosition = minPosition; currentPosition < maxPosition; currentPosition++) { if (array[currentPosition] > array[currentPosition+1]){ temp = array[currentPosition+1]; array[currentPosition+1] = array[currentPosition]; array[currentPosition] = temp; switchCount++; changed=true; changedMaxPosition = currentPosition; // } if (array[currentPosition] < array[minPosition]){ temp = array[minPosition]; array[minPosition] = array[currentPosition]; array[currentPosition] = temp; switchCount++; changed=true; } if (array[currentPosition] > array[maxPosition]){ temp = array[maxPosition]; array[maxPosition] = array[currentPosition]; array[currentPosition] = temp; switchCount++; changed=true; } compareCount+=3; } if (!changed) { timeAmount=System.nanoTime()-timeAmount; assert(validate()); return; } compareCount++; maxPosition = changedMaxPosition; // - } timeAmount=System.nanoTime()-timeAmount; assert(validate()); return; }
Bubble Classic, random array Compares: 50 005 000, Switches: 25 020 725, Time: 117 550 372 Bubble Advanced, random array Compares: 45 090 482, Switches: 10 174 100, Time: 70 032 156 Bubble Classic, decremental array Compares: 50 005 000, Switches: 50 005 000, Time: 47 815 033 Bubble Advanced, decremental array Compares: 60 022 000, Switches: 30 003 000, Time: 46 042 519 Bubble Classic, incremental array Compares: 50 005 000, Switches: 0, Time: 25 072 582 Bubble Advanced, incremental array Compares: 30 000, Switches: 0, Time: 34 773
public void quickSort() { timeAmount = System.nanoTime(); switchCount = 0; compareCount = 0; doQuickSort(0, array.length - 1); timeAmount = System.nanoTime() - timeAmount; assert(validate()); } private void doQuickSort(int startPosition, int lastPosition) { if (startPosition >= lastPosition) { compareCount++; return; } int tempStartPosition = startPosition, tempLastPosition = lastPosition; int currentPosition = tempStartPosition - (tempStartPosition - tempLastPosition) / 2; while (tempStartPosition < tempLastPosition) { while (tempStartPosition < currentPosition && (array[tempStartPosition] <= array[currentPosition])) { compareCount++; tempStartPosition++; } while (tempLastPosition > currentPosition && (array[currentPosition] <= array[tempLastPosition])) { compareCount++; tempLastPosition--; } if (tempStartPosition < tempLastPosition) { int temp = array[tempStartPosition]; array[tempStartPosition] = array[tempLastPosition]; array[tempLastPosition] = temp; switchCount++; if (tempStartPosition == currentPosition) currentPosition = tempLastPosition; else if (tempLastPosition == currentPosition){ currentPosition = tempStartPosition; compareCount++; } compareCount++; } compareCount++; } doQuickSort(startPosition, currentPosition); doQuickSort(currentPosition + 1, lastPosition); }
Bubble Classic, random array Compares: 50 005 000, Switches: 24 684 713, Time: 117 338 627 QuickSort, random array Compares: 453 318, Switches: 22 234, Time: 3 000 141 Bubble Advanced, random array Compares: 44 832 715, Switches: 10 048 493, Time: 70 540 407 Bubble Classic, decremental array Compares: 50 005 000, Switches: 50 005 000, Time: 47 269 214 QuickSort, decremental array Compares: 153 632, Switches: 5 000, Time: 179 766 Bubble Advanced, decremental array Compares: 60 022 000, Switches: 30 003 000, Time: 45 579 908 Bubble Classic, incremental array Compares: 50 005 000, Switches: 0, Time: 24 927 899 QuickSort, incremental array Compares: 143 632, Switches: 0, Time: 134 437 Bubble Advanced, incremental array Compares: 30 000, Switches: 0, Time: 35 394
static public void fillIncremental(int[] array) { for (int count=0; count < array.length; count++) { array[count] = count; } for (int count=array.length/2; count < array.length/2+5; count++) // 5 array[count]=(int)Math.random()*100; }
Bubble Classic, incremental array Compares: 50 005 000, Switches: 24 995, Time: 24 751 238 QuickSort, incremental array Compares: 219 964, Switches: 8 426, Time: 249 624 Bubble Advanced, incremental array Compares: 179 740, Switches: 24 981, Time: 125 123
Bubble Classic, incremental array Compares: 5 000 050 000, Switches: 249 995, Time: 2 168 664 535 QuickSort, incremental array Compares: 2 539 530, Switches: 86 062, Time: 11 479 582 Bubble Advanced, incremental array Compares: 1 799 740, Switches: 249 981, Time: 6 411 974
Source: https://habr.com/ru/post/274493/
All Articles