Advertisement
rootUser

Time,movement,comparison for Bubble,Selection,Insertion sort

Jul 9th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.37 KB | None | 0 0
  1. package sorted;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Main
  6. {
  7.     static int[] firstArray = new int[20001];
  8.     static int[] secondArray = new int[20001];
  9.     static int i, j, k, temporary, comparison, interchange, t;
  10.  
  11.  
  12.     static void selection_sort()
  13.     {
  14.         for(i=1; i<=20000; i++)
  15.         {
  16.             secondArray[i]=firstArray[i];
  17.         }
  18.  
  19.         secondArray[0]=Integer.MIN_VALUE;
  20.         interchange=0;
  21.         comparison=0;
  22.         long started = System.nanoTime();
  23.         for(j=20000; j>0; j--)
  24.         {
  25.             t=0;
  26.             for(k=2; k<=j; k++)
  27.             {
  28.                 comparison=comparison+1;
  29.                 if(secondArray[t]<secondArray[k])
  30.                 {
  31.                     t=k;
  32.                 }
  33.             }
  34.             temporary=secondArray[j];
  35.             secondArray[j]=secondArray[t];
  36.             secondArray[t]=temporary;
  37.             interchange=interchange+1;
  38.         }
  39.         long finished = System.nanoTime()-started;
  40.        
  41.         System.out.print("Selection         "+finished*1e-6+" millisecond       "+interchange+"                 "+comparison+"\n");
  42.     }
  43.  
  44.     static void insertion_sort()
  45.     {
  46.         for(i=1; i<=20000; i++)
  47.         {
  48.             secondArray[i]=firstArray[i];
  49.         }
  50.         secondArray[0]=Integer.MIN_VALUE;
  51.         comparison=0;
  52.         interchange=0;
  53.         long started = System.nanoTime();
  54.         for(j=2; j<=20000; j++)
  55.         {
  56.             i=j-1;
  57.             temporary=secondArray[j];
  58.             comparison=comparison+1;
  59.             while(temporary<secondArray[i])
  60.             {
  61.                 interchange=interchange+1;
  62.                 secondArray[i+1]=secondArray[i];
  63.                 i=i-1;
  64.                 comparison=comparison+1;
  65.             }
  66.             secondArray[i+1]=temporary;
  67.         }
  68.         long finished = System.nanoTime()-started;
  69.        
  70.         System.out.print("Insertion         "+finished*1e-6+" millisecond       "+interchange+"                 "+comparison+"\n");
  71.     }
  72.  
  73.     static void bubble_sort()
  74.     {
  75.         for(i=1; i<=20000; i++)
  76.         {
  77.             secondArray[i]=firstArray[i];
  78.         }
  79.         secondArray[0]=Integer.MIN_VALUE;
  80.         comparison=0;
  81.         interchange=0;
  82.         long started = System.nanoTime();
  83.         k=20000;
  84.  
  85.         while(k!=0)
  86.         {
  87.             t=0;
  88.             for(j=1; j<= k-1; j++)
  89.             {
  90.                 comparison=comparison+1;
  91.                 if(secondArray[j]>secondArray[j+1])
  92.                 {
  93.                     temporary=secondArray[j];
  94.                     secondArray[j]=secondArray[j+1];
  95.                     secondArray[j+1]=temporary;
  96.                     t=j;
  97.                     interchange=interchange+1;
  98.                 }
  99.             }
  100.             k=t;
  101.         }
  102.         long finished = System.nanoTime()-started;
  103.        
  104.         System.out.print("Bubble            "+finished*1e-6+" millisecond       "+interchange+"                 "+comparison+"\n");
  105.     }
  106.  
  107.     public static void main(String[] args)
  108.     {
  109.         Random random = new Random();
  110.         for(i=1; i<=20000; i++)
  111.         {
  112.             firstArray[i]=random.nextInt();
  113.         }
  114.         System.out.print("Sort              \tTime                  \tMovement              \tComparison\n");
  115.         selection_sort();
  116.         insertion_sort();
  117.         bubble_sort();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement