add bubble short

This commit is contained in:
Rémi Heredero 2022-03-07 21:48:18 +01:00
parent 454ffa9c34
commit bd2e651b5e
8 changed files with 96 additions and 26 deletions

BIN
bin/tools/Chrono.class Normal file

Binary file not shown.

View 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);
}
}

View File

@ -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);
}
}

View 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
View 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;
}
}