Guest User

Untitled

a guest
Jan 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Test
  4. {
  5.     public static void sortLinear(Comparable[] x)
  6.     {
  7.         for(int i=1;i<x.length;i++)
  8.         {
  9.             int pos = linearSearch(x,i);
  10.             Comparable tmp = x[i];
  11.             for(int j=i;j>pos;j--)
  12.                 x[j] = x[j-1];
  13.             x[pos] = tmp;
  14.         }
  15.     }
  16.    
  17.     private static int linearSearch(Comparable[] x, int sorted)
  18.     {
  19.         for(int i=0;i<sorted;i++)
  20.             if(x[i].compareTo(x[sorted])>0)
  21.                 return i;
  22.         return sorted;
  23.     }
  24.  
  25.     public static void main(String[] args)
  26.     {
  27.         Comparable[] x = new Comparable[50];
  28.         Random randomGen = new Random();
  29.  
  30.         for(int i=0;i<50;i++)
  31.         {
  32.             x[i] = randomGen.nextInt(5000);
  33.             System.out.print(x[i] + " ");
  34.         }
  35.         System.out.println();
  36.  
  37.         sortLinear(x);
  38.  
  39.         for(int i=0;i<50;i++)
  40.             System.out.print(x[i] + " ");
  41.         System.out.println();
  42.     }
  43. }
Add Comment
Please, Sign In to add comment