Notes
Notes - notes.io |
import java.util.concurrent.Delayed;
public class lekprojekt {
public static void randomlyFillArray(int[] array, int a, int b) {
for (int i = 0; i < array.length; i++) {
array[i] = randomInt(a, b);
}
}
public static int randomInt(int a, int b) {
return (int) ((b - a + 1) * Math.random() + a); // math.random --> x € R(double) , 0 < x < 1
//förstår inte slumpningen...
}
//______________Försöker fixa in en quicksort_________*
static long QSjämf = 0;
public static void quickSort(int[] array, int low, int high) {
if (array == null || array.length == 0)
return;
if (low >= high)
return;
// pick the pivot
int middle = low + (high - low) /2;
// markerar pivoten till mittersa platsen
int pivot = array[middle];
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
QSjämf++;
}
if (low < j)
quickSort(array, low, j);
if (high > i)
quickSort(array, i, high);
}
//___________________________försöker fixa in en quicksort________*
static long SelSJämf = 0;
public static void SelectionSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
SelSJämf++;
for (int j = i + 1; j < array.length; j++) {
SelSJämf++;
if (array[i] > array[j]) {
// ... Exchange elements
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
static long InsSJämf = 0;
public static void insertionSort(int[] array) {
int i, j, temp;
for (i = 1; i < array.length; i++) {
InsSJämf++;
temp = array[i];
j = i;
while (j > 0 && array[j - 1] > temp) {
InsSJämf++;
array[j] = array[j - 1];
j--;
}
array[j] = temp;
}
}
static long BubSJämf = 0;
public static void bubbleSort(int[] array) {
boolean swapped = true;
int j = 0;
int temp;
while (swapped) {
//BubSJämf++;
swapped = false;
j++;
for (int i = 0; i < array.length - j; i++) {
BubSJämf++;
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
}
}
public static void main(String[] args) {
int antalBrickor = 6;
int topSlump = 4;
int botSlump = 2;
// int[] array500 = new int[500]; //These are my arrays that I should do the process for //each one.
// int[] array1000 = new int[1000];
// int[] array5000 = new int[5000];
// int[] array10000 = new int[10000];
// int[] array50000 = new int[50000];
// int[] array100000 = new int[100000];
// int[] array500000 = new int[500000];
// int[] array1000000 = new int[1000000];
for (int i = 0; i <= 4; i++) {
int[] array = new int[antalBrickor];
if (i == 1) {
randomlyFillArray(array, botSlump, topSlump);
long startTime = System.currentTimeMillis();
//TESTAR BARA QUICKSORT
SelectionSort(array);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
// System.out.println("Sorteringstid i ms för SelectionSort för x integers : "
// + elapsedTime );
System.out.println(" Så här många jämförelser gjorde SelectionSort : " +SelSJämf);
}
else if (i == 2) {
randomlyFillArray(array, botSlump, topSlump);
long startTime = System.currentTimeMillis();
insertionSort(array);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
// System.out.println("Sorteringstid i ms för InsertionSort för x integers : "
// + elapsedTime);
System.out.println(" Så här många jämförelser gjorde InsertionSort : " +InsSJämf);
}
else if (i == 3) {
randomlyFillArray(array, botSlump, topSlump);
long startTime = System.currentTimeMillis();
bubbleSort(array);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
// System.out.println(" Sorteringstid i ms för BubbleSort för x integers : "
// + elapsedTime);
System.out.println(" Så här många jämförelser gjorde BubbleSort : " +BubSJämf);
}
//TESTING BELOW
else if (i == 4) {
randomlyFillArray(array, botSlump, topSlump);
long startTime = System.currentTimeMillis();
quickSort(array,0,antalBrickor-1);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println(" Sorteringstid i ms för Quicksort för x integers : "
+ elapsedTime);
System.out.println(" Så här många jämförelser gjorde quicksort : " +QSjämf);
}
//TESTING ABOVE
}
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team