Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: Using bubbleset and Quicksort at the same time with the same numbers

  1. #1
    sancho is offline Member
    Join Date
    Apr 2016
    Posts
    25
    Rep Power
    0

    Arrow Using bubbleset and Quicksort at the same time with the same numbers

    Could someone help me with this? I want to use both of them and show to the user which one is the best to that occasion. I got an error too..

    Using bubbleset and Quicksort at the same time with the same numbers-sem-t-tulo.jpg

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class aps {
    public static Scanner ler;
    public static void main(String[] args) {
    int op1, op, i;
    
    ler = new Scanner(System.in);
    
    Random n = new Random();
    
    int v, ret;
    do{
    
    System.out.println("1 - Do you want to read an outside file?");
    System.out.println("2 - Type your own vector?");
    op1 = ler.nextInt();
    switch(op1){
    case 1:
    
    Reader ae = new Reader();
    
    ae.read();
    break;
    
    case 2:
    
    System.out.println("How many spaces do you wannna Type it? ");
    v = ler.nextInt();
    int vetor[] = new int[v];
    
    
    System.out.println("1 - Do you want to type it?");
    System.out.println("2 - Do you want random numbers?");
    op = ler.nextInt();
    switch(op){
    case 1:
    for (i = 0; i < vetor.length; i++) {
    System.out.print("Type it " + (i + 1) + " number = ");
    vetor[i] = ler.nextInt();
    }
    bubblesort(vetor);
    Quicksort(vetor, 0, vetor.length);
    break;
    case 2:
    for (i = 0; i < vetor.length; i++) {
    vetor[i] = n.nextInt();
    }
    bubblesort(vetor);
    Quicksort(vetor, 0, vetor.length);
    break;
    default:
    System.out.print("option missing");
    break;
    }
    break;
    }
    System.out.println("Retorn to the menu");
    ret = ler.nextInt();
    }
    while(ret == 1);
    }
    
    public static void bubblesort(int vet[]){
    int tam = vet.length;
    
    int i, j, aux;
    long tinicial = System.nanoTime();
    for(i = 0; i < (tam - 1); i++){
    
    for(j = 1; j < tam; j++){
    
    if(vet[j] < vet[j - 1]){
    
    aux = vet[j];
    
    vet[j] = vet[j - 1];
    
    vet[j - 1] = aux;
    }
    
    }
    
    }
    long tfinal = System.nanoTime();
    
    System.out.println("Time = " + ((tfinal - tinicial) / 1000000.0) + " ms");
    
    for(i = 0; i < vet.length; i++){
    System.out.print(vet[i] + " ");
    }
    }
    
    public static void Quicksort(int v[], int esq, int dir){
    long tinq = System.nanoTime();
    int pivo;
    if(esq < dir){
    pivo = Divide(v, esq, dir);
    Quicksort(v, esq, pivo);
    Quicksort(v, pivo + 1, dir);
    }
    long tfiq = System.nanoTime();
    System.out.println("Time = " + ((tfiq - tinq) / 1000000.0) + " ms");
    
    for(int i = 0; i < v.length; i++){
    System.out.print(v[i] + " ");
    }
    
    }
    
    public static int Divide(int v[], int left, int right){
    int c = v[left], i = left - 1, j = right, change;
    while(i <= j){
    if(v[i] <= c)
    i++;
    else if(c < v[j])
    --j;
    else{
    change = v[i];
    v[i] = v[j];
    v[j] = change;
    }
    }
    v[left] = v[j];
    v[j] = c;
    return j;
    }
    
    }
    
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    public class Reader {
    
    public void read() {
    
    Path pasta = Paths.get("C:/Users/BRO/Documents/arq.txt");
    
    try{
    
    byte[] text = Files.readAllBytes(pasta);
    String read = new String(text);
    
    System.out.println(read);
    
    }
    catch(Exception erro){
    }
    }
    }
    Last edited by sancho; 04-08-2016 at 04:44 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Using bubbleset and Quicksort at the same time with the same numbers

    Please place code between code tags. See BB Code List - Java Programming Forum - Learn Java Programming for help. If you code really looks
    like that I recommend you organize it better. Align the {} and use indentation. You also need to use some print statements to
    print out key values. Somewhere one of your indices is getting to be a -1. You need to print that (and perhaps other values) to
    help find the reason.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    sancho is offline Member
    Join Date
    Apr 2016
    Posts
    25
    Rep Power
    0

    Default Re: Using bubbleset and Quicksort at the same time with the same numbers

    Well, i fixed the topic, thanks for the tip i'll try here.

  4. #4
    sancho is offline Member
    Join Date
    Apr 2016
    Posts
    25
    Rep Power
    0

    Default Re: Using bubbleset and Quicksort at the same time with the same numbers

    Ok, i got it! But another problem came, now i have 3 vectors and i need to order some numbers, the first method is fine but the others they do not do their job..

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class Aps {
    	public static Scanner ler;
    	static long tinq;
    	static long tfiq;
    	public static void main(String[] args) {
    		int op1, op, i;
    		
    		
    		ler = new Scanner(System.in);
    		
    		
    		Random n = new Random();
    		
    		
    		Orden ord1 = new Orden();
    		Orden ord2 = new Orden();
    		Orden ord3 = new Orden();
    		
    		
    		int v, ret;
    		do{
    		//Menu
    		System.out.println("1 - Want to read a file?");
    		System.out.println("2 - Order a sequence of numbers?");
    		op1 = ler.nextInt();
    		switch(op1){
    		case 1:	
    		
    		Reader ae = new Reader();
    		
    		ae.leitura();
    		break;
    		
    		case 2:
    		
    		System.out.println("How many numbers do you want?");
    	    v = ler.nextInt();
    		int vetor[] = new int[v];
    		
    	    
    		System.out.println("1 - I want to choose the numbers");
    		System.out.println("2 - I want a random selected numbers");
    		op = ler.nextInt();
    		switch(op){
    		case 1:
    	    for (i = 0; i < vetor.length; i++) {
    	        System.out.print("Type it the " + (i + 1) + "first value = ");
    	        vetor[i] = ler.nextInt();
    	    }
    	    
    		ord1.bubblesort(vetor);
    
    		ord2.apresentaQuick(vetor);
    		
    		ord3.Cocktail(vetor);
    		
    	    break;
    		case 2: 
    			for (i = 0; i < vetor.length; i++) { 
    		        vetor[i] = n.nextInt();
    			}
    			ord1.bubblesort(vetor);
    
    			ord2.apresentaQuick(vetor);
    			
    			ord3.Cocktail(vetor);
    			
    			break;
    		default:
    			 System.out.print("Doesn't match");
    			 break;
    		}
    		break;
    		}
    		System.out.println("Retorn to the main menu?");
    		ret = ler.nextInt();
    		}
    		while(ret == 1);
    	}
    	
    	
    }
     
    
    
    public class Orden {
    	long tinicio, tfim;
    	public void bubblesort(int vet[]){
    		
    		for(int i = 0; i < vet.length; i++){
    			System.out.print(vet[i] + " ");
    			}
    		int tam = vet.length;
    
    		int i, j, aux;
    		tinicio = System.nanoTime();			
    			for(i = 0; i < (tam - 1); i++){
    
    				for(j = 1; j < tam; j++){
    
    					if(vet[j] < vet[j - 1]){
    
    						aux = vet[j];
    
    						vet[j] = vet[j - 1];
    
    						vet[j - 1] = aux;
    				}
    
    			}
    
    		}
    			tfim = System.nanoTime();
    			
    			System.out.println("Tempo = " + ((tfim - tinicio) / 1000000.0) + " ms");
    
    			for(i = 0; i < vet.length; i++){
    				System.out.print(vet[i] + " ");
    			}
    			System.out.println("\n");
    	}
    	 
    	public void apresentaQuick(int vet[]){
    		
    		for(int i = 0; i < vet.length; i++){
    			System.out.print(vet[i] + " ");
    			}
    		
    		tinicio = System.nanoTime();
    		Quicksort(vet, 0, vet.length - 1);
    		tfim = System.nanoTime();
    		System.out.println("Tempo = " + ((tfim - tinicio) / 1000000.0) + " ms ");
    		
    		for(int i = 0; i < vet.length; i++){
    			System.out.print(vet[i] + " ");
    		}
    		
    		System.out.println("\n");
    	}
    	
    	public void Quicksort(int v[], int esq, int dir){
    		int pivo;
    		if(esq < dir){
    			pivo = Separar(v, esq, dir);
    			Quicksort(v, esq, pivo - 1);
    			Quicksort(v, pivo + 1, dir);
    		}
    	}
    	
    	public static int Separar(int vet[], int esq, int dir){
    		int c = vet[esq], i = esq +  1, j = dir, troca;
    		while(i <= j){
    			if(vet[i] <= c)
    			i++;
    			else if(c < vet[j])
    			j--;
    		else{
    			troca = vet[i];
    			vet[i] = vet[j];
    			vet[j] = troca;
    		}
    		}
    		i = j + 1;
    		vet[esq] = vet[j];
    		vet[j] = c;
    		return j;
    	}
    	
    	public void Cocktail(int vetor[]){
    		tinicio = System.nanoTime();
    		int esq = 0, dir = vetor.length - 1, cond = 0, troca, i;
    		while(cond == 0 && esq < dir){
    			cond++;
    			for(i = esq; i < dir; i++){
    				if(vetor[i] > vetor[i + 1]){
    					troca = vetor[i];
    					vetor[i] = vetor[i + 1];
    					vetor[i + 1] = troca;
    					cond = 0;
    				}
    			}
    			dir = dir - 1;
    			for(i = dir; i > esq; i--){
    				if(vetor[i] < vetor[i - 1]){
    					troca = vetor[i];
    					vetor[i] = vetor[i - 1];
    					vetor[i - 1] = troca;
    					cond = 0;
    				}
    			}
    			esq = esq + 1;
    		}
    		tfim = System.nanoTime();
    		System.out.println("Tempo = " + ((tfim - tinicio) / 1000000.0) + " ms");
    		
    		for(i = 0; i < vetor.length; i++){
    			System.out.print(vetor[i] + " ");
    		}
    	}
    
    }
    
     
    
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    public class Reader {
    
    	public void leitura() {
    		
    		Path pasta = Paths.get("C:/Users/WRJ/Documents/arq.txt");
    		
    		try{
    			
    			byte[] text = Files.readAllBytes(pasta);
    			String ler = new String(text);
    			
    
    			System.out.println(ler);
    			
    		}
    		catch(Exception erro){
    		}
    		}
    	}
    Last edited by sancho; 04-19-2016 at 05:33 AM. Reason: problems

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Using bubbleset and Quicksort at the same time with the same numbers

    I am not certain what you mean by not doing their job. I do notice that once you sort your array you use the same array for the other methods. So you are sorting an already sorted array. And you only need one instance of Orden, not three.

    You may also want to clean up your print statements by appropriately using System.out.println().

    And here is a programming tip. When you have some code that is typically grouped together to perform a common function, turn
    it into a method. It makes the overall program cleaner and less prone to errors. For example:

    You have the following code in quite a few places:

    Java Code:
    for (int i = 0; i < vet.length; i++) {
       System.out.print(vet[i] + " ");
    }
    System.out.println();
    Turn it into a method and then just call the method when you want to display the contents of the array.

    Java Code:
    display(vet);
    
    public static void display(int[] data) {
       for (int i = 0; i < data.length; i++) {
           System.out.print(data[i] + " ");
       }
       System.out.println();
    }
    Note also that I declared the method static since it does not rely on any instance fields. This is not really necessary though.

    Regards,
    Jim
    Last edited by jim829; 04-19-2016 at 05:26 PM.
    sancho likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 26
    Last Post: 12-03-2013, 02:55 PM
  2. Replies: 10
    Last Post: 04-01-2013, 01:50 PM
  3. Replies: 3
    Last Post: 10-02-2012, 06:07 AM
  4. Replies: 9
    Last Post: 01-26-2012, 01:04 PM
  5. Quicksort
    By little_polarbear in forum New To Java
    Replies: 12
    Last Post: 07-12-2008, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •