Commit b03fe092 authored by Jonas Skogtrø Olsen's avatar Jonas Skogtrø Olsen
Browse files

commit

parent 327b8737
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -2,6 +2,8 @@ import java.awt.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.stream.IntStream;
public class QuickSortAlgo {
......@@ -63,8 +65,8 @@ public class QuickSortAlgo {
}
public static void quicksortBubble(int[]t,int v, int h){
//#TODO finn ut hva" h-v > ?" best for spørsmålstegnet
if(h-v>t.length/2){
//#TODO finn ut " h-v > ?" best verdi for spørsmålstegnet
if(h-v>50000){
int delpos = splitt(t,v,h);
quicksort(t,v,delpos-1);
quicksort(t,delpos+1,h);
......@@ -72,17 +74,53 @@ public class QuickSortAlgo {
bubbleSort(t);
}
}
//Sjekker om liste sortert riktig
public static boolean feilSortering(int[]liste){
for(int i = 1; i < liste.length;i++){
if(liste[i]<liste[i-1]){
return false;
}
}
return true;
}
//sjekker sum av elementene i listen
public static int sjekksum(int [] liste){
int sum = IntStream.of(liste).sum();
return sum;
}
public static void removeAndAddRandom(int[]liste){
Random random = new Random();
liste = null;
liste = random.ints(100000, 10,100000).toArray();
}
public static void main(String[]args){
//#todo lage liste med 100000 tall
//#TODO ta tid bå begge funksjonene
//#TODo lage sjekksum og korekthetstest
int liste[] ={5,6,7,8,22,12,1,24,36,347,3,7,36,73,4,234,5,3657,2,45,4};
System.out.println(Arrays.toString(liste));
bubbleSort(liste);
//quicksortBubble(liste,0,n-1);
System.out.println(Arrays.toString(liste));
//liste med 100000 tall
Random random = new Random();
int[] storListe = random.ints(100000, 10,100000).toArray();
System.out.println("Antall før sortering: "+sjekksum(storListe));
long startTime = System.nanoTime();
quicksortBubble(storListe,0,storListe.length-1);
System.out.println("Antall etter sortering: "+sjekksum(storListe));
quicksortBubble(storListe,0,storListe.length-1);
long endTime = System.nanoTime();
long duration = (endTime - startTime)/1000000; //divide by 1000000 to get milliseconds.
System.out.println(duration+" duration");
System.out.println("Antall etter sortering: "+sjekksum(storListe));
System.out.println("er lista sortert riktig?: "+feilSortering(storListe));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment