Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sorted;
- import java.util.Random;
- public class Main
- {
- static int[] firstArray = new int[20001];
- static int[] secondArray = new int[20001];
- static int i, j, k, temporary, comparison, interchange, t;
- static void selection_sort()
- {
- for(i=1; i<=20000; i++)
- {
- secondArray[i]=firstArray[i];
- }
- secondArray[0]=Integer.MIN_VALUE;
- interchange=0;
- comparison=0;
- long started = System.nanoTime();
- for(j=20000; j>0; j--)
- {
- t=0;
- for(k=2; k<=j; k++)
- {
- comparison=comparison+1;
- if(secondArray[t]<secondArray[k])
- {
- t=k;
- }
- }
- temporary=secondArray[j];
- secondArray[j]=secondArray[t];
- secondArray[t]=temporary;
- interchange=interchange+1;
- }
- long finished = System.nanoTime()-started;
- System.out.print("Selection "+finished*1e-6+" millisecond "+interchange+" "+comparison+"\n");
- }
- static void insertion_sort()
- {
- for(i=1; i<=20000; i++)
- {
- secondArray[i]=firstArray[i];
- }
- secondArray[0]=Integer.MIN_VALUE;
- comparison=0;
- interchange=0;
- long started = System.nanoTime();
- for(j=2; j<=20000; j++)
- {
- i=j-1;
- temporary=secondArray[j];
- comparison=comparison+1;
- while(temporary<secondArray[i])
- {
- interchange=interchange+1;
- secondArray[i+1]=secondArray[i];
- i=i-1;
- comparison=comparison+1;
- }
- secondArray[i+1]=temporary;
- }
- long finished = System.nanoTime()-started;
- System.out.print("Insertion "+finished*1e-6+" millisecond "+interchange+" "+comparison+"\n");
- }
- static void bubble_sort()
- {
- for(i=1; i<=20000; i++)
- {
- secondArray[i]=firstArray[i];
- }
- secondArray[0]=Integer.MIN_VALUE;
- comparison=0;
- interchange=0;
- long started = System.nanoTime();
- k=20000;
- while(k!=0)
- {
- t=0;
- for(j=1; j<= k-1; j++)
- {
- comparison=comparison+1;
- if(secondArray[j]>secondArray[j+1])
- {
- temporary=secondArray[j];
- secondArray[j]=secondArray[j+1];
- secondArray[j+1]=temporary;
- t=j;
- interchange=interchange+1;
- }
- }
- k=t;
- }
- long finished = System.nanoTime()-started;
- System.out.print("Bubble "+finished*1e-6+" millisecond "+interchange+" "+comparison+"\n");
- }
- public static void main(String[] args)
- {
- Random random = new Random();
- for(i=1; i<=20000; i++)
- {
- firstArray[i]=random.nextInt();
- }
- System.out.print("Sort \tTime \tMovement \tComparison\n");
- selection_sort();
- insertion_sort();
- bubble_sort();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement