Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cat /home/djszapi/Downloads/Shellsort.java
- package uebung04;
- import static gdi.MakeItSimple.*;
- public class Shellsort{
- public static int numberOfSwaps = 0;
- public static int numberOfChecks = 0;
- public static int numberOfRuns = 0;
- /**
- * The Shell Sort, which sorted the Array with the given h-Sort Chain.
- * @param data contains the array which shall be sorted.
- * @param hSortChains the given h-Sort Chain
- */
- public static void shellsort(int[] data, int[] hSortChains) {
- //Select the h-Sort Chain, which it should sort with.
- for(int stepInH = 0; stepInH < hSortChains.length; stepInH++) {
- int h = hSortChains[stepInH];
- numberOfRuns++;
- /* Get hold of the element, which should be place on the right spot.
- * Both Indexpointer points at the same element.
- * IndexPointer1 is the pointer which, tells Indexpointer2 to point
- * at the same element.
- * IndexPointer2 is the pointer which checks the element.
- */
- for(int indexPointer1 = h; indexPointer1 < data.length; indexPointer1++) {
- int tmp = indexPointer1;
- int indexPointer2 = indexPointer1;
- numberOfChecks++;
- // This loop starts to swap the elements.
- while(indexPointer2 >= h && data[indexPointer2-h] > tmp){
- /* First swap, if the conditions are true.
- */
- numberOfChecks++;
- if (data[indexPointer2 - h] > tmp){
- data[indexPointer2] = data[indexPointer2 - h];
- indexPointer2 = indexPointer2 - h;
- }
- // Second Swap, this is always Swapping.
- data[indexPointer2] = tmp;
- numberOfSwaps++;
- //Prints the swap of two selected elements.
- println("After swap No." + numberOfSwaps);
- for(int pos=0; pos < data.length ;pos++) {
- if(pos == indexPointer1 || pos == indexPointer2)
- print("("+data[pos]+") ");
- else
- print(data[pos]+" ");
- }
- println();
- }
- }
- }
- }
- /** Print the content of the given array.
- * @param data contains the array which content shall be printed.
- * @param opt contains optional indices, that shall be marked
- */
- public static void printArrayContent(int[] data, int... opt) {
- // Array showing which field shall be marked
- boolean[] markMap = new boolean[data.length];
- if (opt.length > 0) {
- for (int i=0; i < opt.length; i++) {
- markMap[opt[i]] = true;
- }
- }
- for (int i=0; i<data.length; i++) {
- if (markMap[i]) {
- print("(" + data[i] +") ");
- } else {
- print(data[i]+" ");
- }
- }
- println();
- }
- //main method which calls the shellsort method and prints the sorted array.
- public static void main (String[] args){
- // Unsorted Array
- int[] data ={11, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- // The given h-Sort sequences.
- int[] hSortChains = {9, 7, 4, 1};
- shellsort(data, hSortChains);
- println();
- print("The sorted Array: ");
- printArrayContent(data);
- println();
- println("numberOfRuns:\t" + numberOfRuns);
- println("numberOfChecks:\t" + numberOfChecks);
- println("numberOfSwaps:\t" + numberOfSwaps);
- }
- }
Add Comment
Please, Sign In to add comment