add bubble short
This commit is contained in:
parent
454ffa9c34
commit
bd2e651b5e
BIN
bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.class
Normal file
BIN
bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.class
Normal file
BIN
bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.class
Normal file
Binary file not shown.
BIN
bin/tools/Chrono.class
Normal file
BIN
bin/tools/Chrono.class
Normal file
Binary file not shown.
23
src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.java
Normal file
23
src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.java
Normal file
@ -0,0 +1,23 @@
|
||||
package C10_Tri_et_complexite.C103_Algorithme_de_tri;
|
||||
|
||||
public class Bubble {
|
||||
|
||||
Bubble(int[] a){
|
||||
Short.printList(a);
|
||||
|
||||
boolean shorted = false;
|
||||
while (shorted == false) {
|
||||
shorted = true;
|
||||
for (int i = 0; i < a.length-1; i++) {
|
||||
if (a[i] > a[i+1]) {
|
||||
int tmp = a[i];
|
||||
a[i] = a[i+1];
|
||||
a[i+1] = tmp;
|
||||
shorted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Short.printList(a);
|
||||
}
|
||||
}
|
@ -1,18 +1,10 @@
|
||||
package C10_Tri_et_complexite.C103_Algorithme_de_tri;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
// Tri d'un tableau;
|
||||
|
||||
public class Selection {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new Selection(100);
|
||||
}
|
||||
Selection(int[] a){
|
||||
Short.printList(a);
|
||||
|
||||
Selection(int n){
|
||||
int[] a = newList(n);
|
||||
printList(a);
|
||||
for (int i = 0; i < a.length-1; i++) {
|
||||
int min = a[i];
|
||||
int iMin = i;
|
||||
@ -26,22 +18,7 @@ public class Selection {
|
||||
a[i] = a[iMin];
|
||||
a[iMin] = tmp;
|
||||
}
|
||||
printList(a);
|
||||
}
|
||||
|
||||
int[] newList(int size){
|
||||
int[] a = new int[size];
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = r.nextInt(1000);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
static void printList(int[] a){
|
||||
for (int i : a) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
System.out.println("\n");
|
||||
Short.printList(a);
|
||||
}
|
||||
}
|
||||
|
46
src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.java
Normal file
46
src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.java
Normal file
@ -0,0 +1,46 @@
|
||||
package C10_Tri_et_complexite.C103_Algorithme_de_tri;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import tools.Chrono;
|
||||
|
||||
public class Short {
|
||||
private static boolean print = false;
|
||||
public static void main(String[] args) {
|
||||
int[] a = newList(1000);
|
||||
|
||||
|
||||
Chrono selection = new Chrono("Selection");
|
||||
new Selection(a.clone());
|
||||
selection.stop();
|
||||
|
||||
if(print) System.out.println("-----------\n");
|
||||
|
||||
Chrono bubble = new Chrono("Bubble");
|
||||
new Bubble(a.clone());
|
||||
bubble.stop();
|
||||
|
||||
System.out.println(selection);
|
||||
System.out.println(bubble);
|
||||
}
|
||||
|
||||
private static int[] newList(int size){
|
||||
int[] a = new int[size];
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = r.nextInt(1000);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
static void printList(int[] a){
|
||||
if (print) {
|
||||
for (int i : a) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
System.out.println("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
24
src/tools/Chrono.java
Normal file
24
src/tools/Chrono.java
Normal file
@ -0,0 +1,24 @@
|
||||
package tools;
|
||||
|
||||
public class Chrono {
|
||||
private long start, end, total;
|
||||
private String name;
|
||||
|
||||
public Chrono(String name){
|
||||
this.name = name;
|
||||
start = System.nanoTime();
|
||||
}
|
||||
|
||||
public void stop(){
|
||||
end = System.nanoTime();
|
||||
total = (long) ((end- start)/1000.0);
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
String s = name;
|
||||
s += " : \t";
|
||||
s += total;
|
||||
s += " uS";
|
||||
return s;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user