identaçao
This commit is contained in:
parent
944f54627c
commit
b3a2fd6788
|
|
@ -7,27 +7,27 @@
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
class Geracao {
|
class Geracao {
|
||||||
protected int[] array;
|
protected int[] array;
|
||||||
protected int n;
|
protected int n;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construtor.
|
* Construtor.
|
||||||
*/
|
*/
|
||||||
public Geracao(){
|
public Geracao(){
|
||||||
array = new int[100];
|
array = new int[100];
|
||||||
n = array.length;
|
n = array.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construtor.
|
* Construtor.
|
||||||
* @param int tamanho do array de numeros inteiros.
|
* @param int tamanho do array de numeros inteiros.
|
||||||
*/
|
*/
|
||||||
public Geracao(int tamanho){
|
public Geracao(int tamanho){
|
||||||
array = new int[tamanho];
|
array = new int[tamanho];
|
||||||
n = array.length;
|
n = array.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -55,9 +55,9 @@ class Geracao {
|
||||||
*/
|
*/
|
||||||
public void aleatorio() {
|
public void aleatorio() {
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
crescente();
|
crescente();
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
swap(i, Math.abs(rand.nextInt()) % n);
|
swap(i, Math.abs(rand.nextInt()) % n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,8 +66,8 @@ class Geracao {
|
||||||
* Efetua a leitura dos elementos via entrada padrao.
|
* Efetua a leitura dos elementos via entrada padrao.
|
||||||
*/
|
*/
|
||||||
public void entradaPadrao() {
|
public void entradaPadrao() {
|
||||||
n = MyIO.readInt();
|
n = MyIO.readInt();
|
||||||
array = new int[n];
|
array = new int[n];
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
array[i] = MyIO.readInt();
|
array[i] = MyIO.readInt();
|
||||||
|
|
@ -77,13 +77,13 @@ class Geracao {
|
||||||
/**
|
/**
|
||||||
* Recebe um Efetua a leitura dos elementos via entrada padrao.
|
* Recebe um Efetua a leitura dos elementos via entrada padrao.
|
||||||
*/
|
*/
|
||||||
public void entrada(int[] vet){
|
public void entrada(int[] vet){
|
||||||
n = vet.length;
|
n = vet.length;
|
||||||
array = new int[n];
|
array = new int[n];
|
||||||
for(int i = 0; i < n; i++){
|
for(int i = 0; i < n; i++){
|
||||||
array[i] = vet[i];
|
array[i] = vet[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -94,7 +94,7 @@ class Geracao {
|
||||||
System.out.print("[");
|
System.out.print("[");
|
||||||
|
|
||||||
for (int i = 0; i < k; i++) {
|
for (int i = 0; i < k; i++) {
|
||||||
System.out.print(" ("+i+")" + array[i]);
|
System.out.print(" ("+i+")" + array[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("]");
|
System.out.println("]");
|
||||||
|
|
@ -104,57 +104,57 @@ class Geracao {
|
||||||
/**
|
/**
|
||||||
* Mostra os elementos do array.
|
* Mostra os elementos do array.
|
||||||
*/
|
*/
|
||||||
public void mostrar() {
|
public void mostrar() {
|
||||||
System.out.print("[");
|
System.out.print("[");
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
System.out.print(" ("+i+")" + array[i]);
|
System.out.print(" ("+i+")" + array[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("]");
|
System.out.println("]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Troca o conteudo de duas posicoes do array
|
* Troca o conteudo de duas posicoes do array
|
||||||
* @param i int primeira posicao
|
* @param i int primeira posicao
|
||||||
* @param j int segunda posicao
|
* @param j int segunda posicao
|
||||||
*/
|
*/
|
||||||
public void swap(int i, int j) {
|
public void swap(int i, int j) {
|
||||||
int temp = array[i];
|
int temp = array[i];
|
||||||
array[i] = array[j];
|
array[i] = array[j];
|
||||||
array[j] = temp;
|
array[j] = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retorna o timestamp atual
|
* Retorna o timestamp atual
|
||||||
* @return timestamp atual
|
* @return timestamp atual
|
||||||
*/
|
*/
|
||||||
public long now(){
|
public long now(){
|
||||||
return new Date().getTime();
|
return new Date().getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retorna verdadeiro/falso indicando se o array esta ordenado
|
* Retorna verdadeiro/falso indicando se o array esta ordenado
|
||||||
* @return boolean indicando se o array esta ordenado
|
* @return boolean indicando se o array esta ordenado
|
||||||
*/
|
*/
|
||||||
public boolean isOrdenado(){
|
public boolean isOrdenado(){
|
||||||
boolean resp = true;
|
boolean resp = true;
|
||||||
for(int i = 1; i < n; i++){
|
for(int i = 1; i < n; i++){
|
||||||
if(array[i] < array[i-1]){
|
if(array[i] < array[i-1]){
|
||||||
i = n;
|
i = n;
|
||||||
resp = false;
|
resp = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Metodo a ser implementado nas subclasses
|
* Metodo a ser implementado nas subclasses
|
||||||
*/
|
*/
|
||||||
public void sort(){
|
public void sort(){
|
||||||
System.out.println("Método a ser implementado nas subclasses.");
|
System.out.println("Método a ser implementado nas subclasses.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,36 +6,36 @@
|
||||||
|
|
||||||
class Selecao extends Geracao {
|
class Selecao extends Geracao {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construtor.
|
* Construtor.
|
||||||
*/
|
*/
|
||||||
public Selecao(){
|
public Selecao(){
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construtor.
|
* Construtor.
|
||||||
* @param int tamanho do array de numeros inteiros.
|
* @param int tamanho do array de numeros inteiros.
|
||||||
*/
|
*/
|
||||||
public Selecao(int tamanho){
|
public Selecao(int tamanho){
|
||||||
super(tamanho);
|
super(tamanho);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Algoritmo de ordenacao por selecao.
|
* Algoritmo de ordenacao por selecao.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sort() {
|
public void sort() {
|
||||||
for (int i = 0; i < (n - 1); i++) {
|
for (int i = 0; i < (n - 1); i++) {
|
||||||
int menor = i;
|
int menor = i;
|
||||||
for (int j = (i + 1); j < n; j++){
|
for (int j = (i + 1); j < n; j++){
|
||||||
if (array[menor] > array[j]){
|
if (array[menor] > array[j]){
|
||||||
menor = j;
|
menor = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
swap(menor, i);
|
swap(menor, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue