Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.27 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Kyle Kemp
  4.  *
  5.  */
  6. public class Sorting {
  7.  
  8.     private static class Smallest {
  9.    
  10.         public Smallest(int first, int second){
  11.             this.first = first;
  12.             this.second = second;
  13.         }
  14.        
  15.         public Smallest() {}
  16.    
  17.         private int first, second;
  18.        
  19.         public void setSmallest(int[] arr, int pos){
  20.             if(arr[pos] < arr[first]) { first = pos; organize(arr); return; }
  21.             if(arr[pos] < arr[second]) { second = pos; organize(arr); return; }
  22.         }
  23.        
  24.         private void organize(int[] arr) {
  25.             int temp = first;
  26.             if(arr[second] < arr[first]) { first = second; second = temp; }
  27.         }
  28.        
  29.         public int getFirst() { return first; }
  30.         public int getSecond() { return second; }
  31.         public void setFirst(int first) { this.first = first; }
  32.         public void setSecond(int second) { this.second = second; }
  33.     }
  34.  
  35.     public static void main(String[] args){
  36.        
  37.         int[][] arr = { { 1,2,3,4}, {123, 5, 0 }, {123, 1, 0} };
  38.        
  39.         int[] array = {1,2,3,4,7,12,0,6,69, 70};
  40.        
  41.         mySelectionSort(array);
  42.         printArray(array);
  43.        
  44.     }
  45.    
  46.     public static int[] reverse(int[] arr) {
  47.         int[] reversed = new int[arr.length];
  48.        
  49.         for(int x=0; x<arr.length; x++) {
  50.             reversed[x] = arr[arr.length-x-1];
  51.         }
  52.        
  53.         return reversed;
  54.     }
  55.    
  56.     public static void sortTwoD(int[][] sort){
  57.    
  58.         //calculate the length of the 1d array to sort
  59.         int maxSize = 0;
  60.         for(int x=0; x<sort.length; x++){
  61.             maxSize += sort[x].length;
  62.         }
  63.        
  64.         int[] sortMe = new int[maxSize];
  65.        
  66.         int curPos = 0;
  67.        
  68.         //compress the array down to one dimenson
  69.         for(int x=0; x<sort.length; x++){
  70.             System.arraycopy(sort[x], 0, sortMe, curPos, sort[x].length);
  71.             curPos += sort[x].length;
  72.         }
  73.        
  74.         mySelectionSort(sortMe);
  75.        
  76.         curPos = 0;
  77.        
  78.         //rewrite the array to contain all of the sorted numbers
  79.         for(int x=0; x<sort.length; x++){
  80.             System.arraycopy(sortMe, curPos, sort[x], 0, sort[x].length);
  81.             curPos += sort[x].length;
  82.         }
  83.        
  84.     }
  85.    
  86.     private static void swap(int[] arr, int oldpos, int newpos){
  87.         int temp = arr[oldpos];
  88.         arr[oldpos] = arr[newpos];
  89.         arr[newpos] = temp;
  90.     }
  91.  
  92.     private static int findHighestPos(int[] arr) {
  93.         int r = 0;
  94.         for(int i=0; i<arr.length; i++) {
  95.             if(arr[i] > arr[r]) {
  96.                 r = i;
  97.             }
  98.         }
  99.         return r;
  100.     }
  101.    
  102.     public static void mySelectionSort(int[] sort){
  103.    
  104.         int max = findHighestPos(sort);
  105.        
  106.         Smallest min = new Smallest(max,max);
  107.        
  108.         for(int pos=0; pos<sort.length; pos++) {
  109.             for(int i=0; i<sort.length; i++) {
  110.                 min.setSmallest(sort, pos);
  111.             }
  112.             System.out.println("smallest: "+min.first+ " "+min.second);
  113.             for(int i=0; i<sort.length; i++) {
  114.                 if(sort[min.first] < sort[i]) {
  115.                     swap(sort, min.first, i);
  116.                     break;
  117.                 }
  118.             }
  119.             for(int i=min.first; i<sort.length; i++) {
  120.                 if(sort[min.second] < sort[i]) {
  121.                     swap(sort, min.second, i);
  122.                     break;
  123.                 }
  124.             }
  125.         }
  126.         printArray(sort);
  127.     }
  128.    
  129.     public static void printArray(int[] arr){
  130.        
  131.         for(int i : arr){
  132.             System.out.print(i + " ");
  133.         }
  134.         System.out.println();
  135.        
  136.     }
  137.    
  138.     public static void print2DArray(int[][] arr){
  139.        
  140.         // get the maximum size of a number, so it can be dynamically pretty
  141.         int maxSize=0;
  142.         for(int x=0; x<arr.length; x++){
  143.             for(int y=0; y<arr[x].length; y++){
  144.                 maxSize = Math.max((""+arr[x][y]).length(), maxSize);
  145.             }
  146.         }
  147.        
  148.         for(int x=0; x<arr.length; x++){
  149.             for(int y=0; y<arr[x].length; y++){
  150.                 System.out.format("%"+maxSize+"d,", arr[x][y]);
  151.             }
  152.             System.out.println();
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement