public class MergeS {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] num = {9,5,7,8,1,4,2,3,6};
mergeSort(num, 0, num.length - 1);
for(int i=0; i {
System.out.print(num[i]+" ");
}
System.out.println();
System.out.println(Integer.MAX_VALUE);
}
static void mergeSort(int[] array, int p, int r){
if(p < r){
int q = (p + r) / 2;
mergeSort(array, p, q);
mergeSort(array, q + 1, r);
merge(array, p, q, r);
}
}
static void merge(int[] array, int p, int q, int r){
int n1 = q - p + 1;
int n2 = r - q;
int[] left = new int[n1 + 1];
int[] right = new int[n2 + 1];
for(int i = 0; i < n1; i++){
left[i] = array[p + i];
}
for(int j = 0; j < n2; j++){
right[j] = array[q + j + 1];
}
left[n1] = Integer.MAX_VALUE; // null indicates infinity
right[n2] = Integer.MAX_VALUE;
for(int i = 0, j = 0, k = p; k <= r; k++){
if(left[i] <= right[j]){
array[k] = left[i++];
}else{
array[k] = right[j++];
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] num = {9,5,7,8,1,4,2,3,6};
mergeSort(num, 0, num.length - 1);
for(int i=0; i
System.out.print(num[i]+" ");
}
System.out.println();
System.out.println(Integer.MAX_VALUE);
}
static void mergeSort(int[] array, int p, int r){
if(p < r){
int q = (p + r) / 2;
mergeSort(array, p, q);
mergeSort(array, q + 1, r);
merge(array, p, q, r);
}
}
static void merge(int[] array, int p, int q, int r){
int n1 = q - p + 1;
int n2 = r - q;
int[] left = new int[n1 + 1];
int[] right = new int[n2 + 1];
for(int i = 0; i < n1; i++){
left[i] = array[p + i];
}
for(int j = 0; j < n2; j++){
right[j] = array[q + j + 1];
}
left[n1] = Integer.MAX_VALUE; // null indicates infinity
right[n2] = Integer.MAX_VALUE;
for(int i = 0, j = 0, k = p; k <= r; k++){
if(left[i] <= right[j]){
array[k] = left[i++];
}else{
array[k] = right[j++];
}
}
}
}
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
void insertionSort(int numbers[], int array_size)
{
int i, j, index;
for (i = 1; i < array_size; i++)
{
index = numbers[i];
j = i;
while ((j > 0) && (numbers[j - 1] > index))
{
numbers[j] = numbers[j - 1];
j = j - 1;
}
numbers[j] = index;
}
}
No hay comentarios:
Publicar un comentario