-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsercionQSMOD.java
More file actions
152 lines (125 loc) · 3.35 KB
/
Copy pathInsercionQSMOD.java
File metadata and controls
152 lines (125 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Grupo 3
//Programa que imprime las ejecuciones individuales.
import java.util.Arrays;
import java.lang.management.*;
import java.math.BigInteger;
public class InsercionQSMOD {
private static long numComparaciones;
private static long numAsignaciones;
private static int TAM_INSERCION;
/*
* Argumento 1 -> numero de vectores a ordenar
* Argumento 2 -> tamaño de vector al que detener Quicksort y empezar insercion
* Argumento 3 -> tamaño total de los vectores
*/
public static void main(String args[]) {
int numVect = Integer.parseInt(args[0]);
TAM_INSERCION = Integer.parseInt(args[1]);
int tam = Integer.parseInt(args[2]);
BigInteger tiempoMedia = BigInteger.valueOf(0);
long comparacionesMedia = 0;
long asignacionesMedia = 0;
int[] array;
for (int n = 0; n < numVect; n++) {
array = new int[tam];
numAsignaciones = 0;
numComparaciones = 0;
long tiempoInicio = getTime();
for (int i = 0; i < tam; i++) {
array[i] = (int) (Math.random() * tam);
}
quickSort(array, 0, tam - 1);
insertion(array);
long tiempoFinal = getTime();
/*tiempoMedia = tiempoMedia.add(
BigInteger.valueOf(tiempoFinal-tiempoInicio));
comparacionesMedia+=numComparaciones;
asignacionesMedia+=numAsignaciones;
*/
System.out.println(tam+","+TAM_INSERCION+","+BigInteger.valueOf(tiempoFinal-tiempoInicio)+","+numComparaciones+","+numAsignaciones);
}
/*
* Impresion de estadisticas
*/
}
/*
* Devuelve la mediana de tres elementos del vector elegidos al azar entre low y high
*/
public static int pivote(int[] arr, int low, int high) {
int longitud = high - low;
int a = (int) (Math.random() * longitud) + low;
int b = (int) (Math.random() * longitud) + low;
int c = (int) (Math.random() * longitud) + low;
int array[] = new int[3];
array[0] = arr[a];
array[1] = arr[b];
array[2] = arr[c];
Arrays.sort(array);
return array[1];
}
/*
* Algortimo de ordenacion Quicksort
*/
public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
int pivot = pivote(arr, low, high);
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
numComparaciones++;
i++;
}
numComparaciones++;
while (arr[j] > pivot) {
numComparaciones++;
j--;
}
numComparaciones++;
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
numAsignaciones += 3;
}
}
// recursively sort two sub parts
if (low - j < TAM_INSERCION)
quickSort(arr, low, j);
if (high - i > TAM_INSERCION)
quickSort(arr, i, high);
}
/*
* Algortimo de ordenacion por insercion
*/
public static void insertion(int arr[]) {
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
numAsignaciones++;
int j = i - 1;
/*
* Move elements of arr[0..i-1], that are greater than key, to one position
* ahead of their current position
*/
while (j >= 0 && arr[j] > key) {
numComparaciones++;
arr[j + 1] = arr[j];
j = j - 1;
numAsignaciones++;
}
arr[j + 1] = key;
numAsignaciones++;
}
}
public static long getTime(){
ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
return bean.isCurrentThreadCpuTimeSupported( ) ?
bean.getCurrentThreadCpuTime() : 0L;
}
}