Advertisement
Guest User

bubblesort

a guest
Jan 6th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. /****************************************************************************
  2.  * Copyright/Copyleft:
  3.  *
  4.  * For this source the LGPL Lesser General Public License,
  5.  * published by the Free Software Foundation is valid.
  6.  * It means:
  7.  * 1) You can use this source without any restriction for any desired purpose.
  8.  * 2) You can redistribute copies of this source to everybody.
  9.  * 3) Every user of this source, also the user of redistribute copies
  10.  *    with or without payment, must accept this license for further using.
  11.  * 4) But the LPGL is not appropriate for a whole software product,
  12.  *    if this source is only a part of them. It means, the user
  13.  *    must publish this part of source,
  14.  *    but don't need to publish the whole source of the own product.
  15.  * 5) You can study and modify (improve) this source
  16.  *    for own using or for redistribution, but you have to license the
  17.  *    modified sources likewise under this LGPL Lesser General Public License.
  18.  *    You mustn't delete this Copyright/Copyleft inscription in this source file.
  19.  *
  20.  * @author Hartmut Schorrig: hartmut.schorrig@vishia.de, www.vishia.org
  21.  * @version 0.93 2011-01-05  (year-month-day)
  22.  *******************************************************************************/
  23. package org.vishia.util;
  24.  
  25. public class Sort
  26. {
  27.  
  28.   /**Adequate to {@link bubbleSort(long[] data)}
  29.    *
  30.    * @param data
  31.    * @return
  32.    */  
  33.   public static int[] bubbleSort(int[] data)
  34.   {
  35.     int[] idxData = new int[data.length +1];
  36.     int[] idxSrc = new int[data.length];
  37.     int idx;
  38.     for(idx=0; idx < data.length; idx++)
  39.     { idxSrc[idx] = idx;
  40.     }
  41.     int nrofSwappings =0;
  42.     { //bubblesort-Algorithmus aus Wikipedia.
  43.       boolean bSwapped;
  44.       int nrofTests = data.length -1;
  45.       do
  46.       { bSwapped = false;
  47.         for(idx = 0; idx < nrofTests; idx++)
  48.         { if(data[ idx ] > data[ idx + 1 ])
  49.           { int temp = data[ idx ];
  50.             data[ idx ] = data[idx+1];
  51.             data[idx+1] = temp;
  52.             int tempIdx = idxSrc[ idx ];
  53.             idxSrc[ idx ] = idxSrc[idx+1];
  54.             idxSrc[idx+1] = tempIdx;
  55.             bSwapped = true;
  56.             nrofSwappings +=1;
  57.           }
  58.         }
  59.         nrofTests -= 1;
  60.       }while(bSwapped);
  61.     }  
  62.     //The idxSrc contains the indices in sorted order,
  63.     //but the result should contain the dst indices to insort the src elements.
  64.     //Therefore the order of idxSrc must place in the element idxSrc[]
  65.     for(idx= 0; idx < idxSrc.length; idx++)
  66.     { idxData[idxSrc[idx]] = idx;
  67.     }
  68.     idxData[data.length] = nrofSwappings;
  69.     return idxData;
  70.   }
  71.  
  72.   /**Sorts the input array, returns the index in a new output array.
  73.    * The input array may be only a copy of the relevant sorting informations of a larger information set.
  74.    * The algorithm returns an array of indices to sort the whole information.
  75.    * <br>
  76.    * At example, an input set contains the values <code>{{ 5, "e"}, { 2, "b"}, { 7, "g"}, { 1, "a"}}</code><br>
  77.    * The input array for this method should contain the sorting relevant information: <code>{5, 2, 7, 1 }</code><br>
  78.    * A temporary result will contain the indices of the information in sorted order<code>{3, 1, 0, 2 }</code><br>
  79.    * But the result will contain the indices to copy to a dst array if the src information is taken in its given order<code>{2, 1, 3, 0 }</code><br>
  80.    * To create a sorted whole data set use the indices and copy the source elements in this order:<br>
  81.    * by reading in given order and copy to index 2, 1, 3, 0, The result will be:<code>{{ 1, "a"}, { 2, "b"}, { 5, "e"}, { 7, "g"}}</code>
  82.    *
  83.    * @param data Array with some long numbers unsorted.
  84.    * @return Array with the indices to write the data in a dst container.
  85.    *         The length of the return array is equal the length of the data array + 1.
  86.    *         The last element contains the number of sorting steps.
  87.    */
  88.   public static int[] bubbleSort(long[] data)
  89.   {
  90.     int[] idxData = new int[data.length +1];
  91.     int[] idxSrc = new int[data.length];
  92.     int idx;
  93.     for(idx=0; idx < data.length; idx++)
  94.     { idxSrc[idx] = idx;
  95.     }
  96.     int nrofSwappings =0;
  97.     { //bubblesort-Algorithmus aus Wikipedia.
  98.       boolean bSwapped;
  99.       int nrofTests = data.length -1;
  100.       do
  101.       { bSwapped = false;
  102.         for(idx = 0; idx < nrofTests; idx++)
  103.         { if(data[ idx ] > data[ idx + 1 ])
  104.           { long temp = data[ idx ];
  105.             data[ idx ] = data[idx+1];
  106.             data[idx+1] = temp;
  107.             int tempIdx = idxSrc[ idx ];
  108.             idxSrc[ idx ] = idxSrc[idx+1];
  109.             idxSrc[idx+1] = tempIdx;
  110.             bSwapped = true;
  111.             nrofSwappings +=1;
  112.           }
  113.         }
  114.         nrofTests -= 1;
  115.       }while(bSwapped);
  116.     }  
  117.     //The idxSrc contains the indices in sorted order,
  118.     //but the result should contain the dst indices to insort the src elements.
  119.     //Therefore the order of idxSrc must place in the element idxSrc[]
  120.     for(idx= 0; idx < idxSrc.length; idx++)
  121.     { idxData[idxSrc[idx]] = idx;
  122.     }
  123.     idxData[data.length] = nrofSwappings;
  124.     return idxData;
  125.   }
  126.  
  127.   /**Only use in debugging (eclipse or so) to test the algorithm.*/
  128.   public static int[] testBubbleSort()
  129.   { long[] src = { 5,2,7,1};
  130.     int[] dst = bubbleSort(src);
  131.     return dst;
  132.   }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement