Advertisement
phr3ncj

examples.java

May 25th, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. /*
  2.  *      examples.java
  3.  *      
  4.  *      Copyright 2011 phra <phra@phra-b0x>
  5.  *      
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *      
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *      
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  *      MA 02110-1301, USA.
  20.  *      
  21.  *      
  22.  */
  23.  
  24.  
  25. //import java.util.Iterator;
  26. import java.util.Arrays;
  27.  
  28. public class examples {
  29.     /**
  30.      * @param args
  31.      */
  32.     public static void main(String args[]){
  33.        
  34.         miovettore numeri = new miovettore();
  35.                
  36.         try {if (args[0].equals("quicksort")) {
  37.            
  38.             System.out.println("vettore prima del quicksorting");
  39.             System.out.println(Arrays.toString(numeri.a));
  40.             numeri.quicksort(0,numeri.a.length-1);
  41.             System.out.println("vettore dopo il quicksorting");
  42.             System.out.println(Arrays.toString(numeri.a));
  43.            
  44.             }
  45.            
  46.         }   catch (ArrayIndexOutOfBoundsException e) {
  47.            
  48.             }
  49.         try {if (args[0].equals("mergesort")) {
  50.            
  51.             System.out.println("vettore prima del mergesorting");
  52.             System.out.println(Arrays.toString(numeri.a));
  53.             numeri.mergesort(0,numeri.a.length-1);
  54.             System.out.println("vettore dopo il mergesorting");
  55.             System.out.println(Arrays.toString(numeri.a));
  56.            
  57.             }
  58.         } catch (ArrayIndexOutOfBoundsException e) {
  59.            
  60.             System.out.println("HELP:");
  61.             System.out.println("args[] vuoto.");
  62.             System.out.println("> java examples mergesort");
  63.             System.out.println("> java examples quicksort");
  64.            
  65.         }
  66.        
  67.        
  68.        
  69.            
  70.     }
  71. }
  72.  
  73.     class miovettore {
  74.        
  75.         int a[] = new int[]{20,10,15,12,12,4,3,2,34,654,56,34,2,34,18};
  76.         int b[] = new int[100];
  77.        
  78.     void mergesort(int primo, int ultimo){
  79.        
  80.         if (primo<ultimo){
  81.            
  82.             int mezzo = (int)(primo+ultimo)/2;
  83.             mergesort(primo,mezzo);
  84.             mergesort(mezzo+1,ultimo);
  85.             merge(primo,ultimo,mezzo);
  86.            
  87.         }
  88.     }
  89.  
  90.     private void merge(int primo, int ultimo, int mezzo){
  91.  
  92.         int i,j,k,h;
  93.         i = k = primo;j = mezzo+1;
  94.         while ((i<=mezzo) && (j<=ultimo)) {
  95.            
  96.             if (this.a[i] <= this.a[j]){
  97.                
  98.                 this.b[k] = this.a[i];
  99.                 i++;
  100.                
  101.             }
  102.             else {
  103.                
  104.                 this.b[k] = this.a[j];
  105.                 j++;
  106.                
  107.             }
  108.             k++;
  109.         }
  110.        
  111.         j = ultimo;
  112.         for (h=mezzo;h>=i;h--){
  113.            
  114.             this.a[j] = this.a[h];
  115.                
  116.             j--;
  117.         }
  118.        
  119.         for (j=primo;j<=k-1;j++){
  120.            
  121.             this.a[j] = this.b[j]; 
  122.         }
  123.     }
  124.  
  125.     void quicksort(int primo, int ultimo) {
  126.        
  127.         if (primo<ultimo) {
  128.            
  129.             int perno = perno(primo, ultimo);
  130.             quicksort(primo,perno);
  131.             quicksort(perno+1,ultimo);
  132.        
  133.         }
  134.     }
  135.  
  136.     private int perno(int primo, int ultimo) {
  137.        
  138.         int x = this.a[primo];
  139.         int j = primo;
  140.         int i;
  141.         for (i = primo;i <= ultimo;i++) {
  142.            
  143.             if (this.a[i] < x) {
  144.                
  145.                 j++;
  146.                 swap(i,j);
  147.            
  148.             }
  149.         }
  150.         this.a[primo] = this.a[j];
  151.         this.a[j] = x;
  152.         return j;
  153.        
  154.     }
  155.    
  156.     private void swap(int uno, int due) {
  157.        
  158.         int temp = this.a[uno];
  159.         this.a[uno] = this.a[due];
  160.         this.a[due] = temp;
  161.        
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement