Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labs.course15_16.lab2;
- /* This program can be used to sort n elements with
- * a "bad" algorithm (quadratic).
- * It is the DIRECT INSERTION */
- public class Insertion1{
- static int []v;
- public static void main (String arg [] ){
- int nTimes= Integer.parseInt (arg[0]); //size of the problem
- long t1,t2;
- v = new int [nTimes];
- int counter =0;
- for (int i = 10000; i <=1280000;i*=2){
- v = new int [i];
- //Vector.inverselySorted(v);
- for (int repetition=1; repetition<=nTimes; repetition++){
- Vector.inverselySorted(v);
- t1=System.currentTimeMillis();
- insertion(v);
- t2=System.currentTimeMillis();
- counter+= t2-t1;
- }
- System.out.println("n: "+i+" "+"TIME: " +(t2-t1));
- }
- }
- public static void insertion(int[] elements) {
- int j;
- int pivot;
- for (int i = 1; i < elements.length; i++) {
- pivot = elements[i];
- j = i-1;
- while (j >= 0 && pivot < elements[j]) {
- elements[j+1] = elements[j];
- j--;
- }
- elements[j+1] = pivot;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement