Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * examples.java
- *
- * Copyright 2011 phra <phra@phra-b0x>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- *
- */
- //import java.util.Iterator;
- import java.util.Arrays;
- public class examples {
- /**
- * @param args
- */
- public static void main(String args[]){
- miovettore numeri = new miovettore();
- try {if (args[0].equals("quicksort")) {
- System.out.println("vettore prima del quicksorting");
- System.out.println(Arrays.toString(numeri.a));
- numeri.quicksort(0,numeri.a.length-1);
- System.out.println("vettore dopo il quicksorting");
- System.out.println(Arrays.toString(numeri.a));
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- }
- try {if (args[0].equals("mergesort")) {
- System.out.println("vettore prima del mergesorting");
- System.out.println(Arrays.toString(numeri.a));
- numeri.mergesort(0,numeri.a.length-1);
- System.out.println("vettore dopo il mergesorting");
- System.out.println(Arrays.toString(numeri.a));
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("HELP:");
- System.out.println("args[] vuoto.");
- System.out.println("> java examples mergesort");
- System.out.println("> java examples quicksort");
- }
- }
- }
- class miovettore {
- int a[] = new int[]{20,10,15,12,12,4,3,2,34,654,56,34,2,34,18};
- int b[] = new int[100];
- void mergesort(int primo, int ultimo){
- if (primo<ultimo){
- int mezzo = (int)(primo+ultimo)/2;
- mergesort(primo,mezzo);
- mergesort(mezzo+1,ultimo);
- merge(primo,ultimo,mezzo);
- }
- }
- private void merge(int primo, int ultimo, int mezzo){
- int i,j,k,h;
- i = k = primo;j = mezzo+1;
- while ((i<=mezzo) && (j<=ultimo)) {
- if (this.a[i] <= this.a[j]){
- this.b[k] = this.a[i];
- i++;
- }
- else {
- this.b[k] = this.a[j];
- j++;
- }
- k++;
- }
- j = ultimo;
- for (h=mezzo;h>=i;h--){
- this.a[j] = this.a[h];
- j--;
- }
- for (j=primo;j<=k-1;j++){
- this.a[j] = this.b[j];
- }
- }
- void quicksort(int primo, int ultimo) {
- if (primo<ultimo) {
- int perno = perno(primo, ultimo);
- quicksort(primo,perno);
- quicksort(perno+1,ultimo);
- }
- }
- private int perno(int primo, int ultimo) {
- int x = this.a[primo];
- int j = primo;
- int i;
- for (i = primo;i <= ultimo;i++) {
- if (this.a[i] < x) {
- j++;
- swap(i,j);
- }
- }
- this.a[primo] = this.a[j];
- this.a[j] = x;
- return j;
- }
- private void swap(int uno, int due) {
- int temp = this.a[uno];
- this.a[uno] = this.a[due];
- this.a[due] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement