public class InsertSort {
public static void main(String args[])
{
int[] num = {9,5,7,8,1,4,2,3,6};
int[] num1 = {5,2,4,6,1,3};
int[] num2 = {1,2,3,4,5};
inSort(num2);
for(int i=0; i System.out.print(num2[i]+" ");
}
static void insertionSort(int num[])
{
int i, j, key;
int nc=0,ni=0;
for (i = 1; i < num.length; i++)
{
System.out.println("||||| i="+i+"|||||");
key = num[i];
System.out.println("\tkey "+key+"="+num[i]+"");
j = i;
if(((j > 0) && (num[j-1] > key))==false)
nc++;
while ((j > 0) && (num[j-1] > key))
{
System.out.println("\t\t num["+j+"]="+num[j-1]);
num[j] = num[j-1];
j = j-1;
ni++;
nc++;
if(((j > 0) && (num[j-1] > key))==false)
nc++;
}
ni++;
System.out.println("\t"+num[j]+"->"+key);
num[j] = key;
}
System.out.println("intercambios -> "+ni);
System.out.println("comparaciones-> "+nc);
}
static void inSort(int num[])
{
int key,i;
int nc=0,ni=0;
for(int j=1; j {
System.out.println("||||| j="+j+"|||||");
key=num[j];
System.out.println("\tkey "+key+"="+num[j]+"");
i=j-1;
if(((i>=0) && (num[i])>key)==false)
nc++;
while((i>=0) && (num[i])>key)
{
System.out.println("\t\t num["+(i+1)+"]="+num[i]);
num[i+1]=num[i];
i=i-1;
ni++;
nc++;
if(((i>=0) && (num[i])>key)==false && i==-1)
nc++;
}
if(num[i+1]>key)
ni++;
System.out.println("\t"+num[i+1]+"->"+key);
num[i+1]=key;
}
System.out.println("intercambios -> "+ni);
System.out.println("comparaciones-> "+nc);
}
}
public static void main(String args[])
{
int[] num = {9,5,7,8,1,4,2,3,6};
int[] num1 = {5,2,4,6,1,3};
int[] num2 = {1,2,3,4,5};
inSort(num2);
for(int i=0; i
}
static void insertionSort(int num[])
{
int i, j, key;
int nc=0,ni=0;
for (i = 1; i < num.length; i++)
{
System.out.println("||||| i="+i+"|||||");
key = num[i];
System.out.println("\tkey "+key+"="+num[i]+"");
j = i;
if(((j > 0) && (num[j-1] > key))==false)
nc++;
while ((j > 0) && (num[j-1] > key))
{
System.out.println("\t\t num["+j+"]="+num[j-1]);
num[j] = num[j-1];
j = j-1;
ni++;
nc++;
if(((j > 0) && (num[j-1] > key))==false)
nc++;
}
ni++;
System.out.println("\t"+num[j]+"->"+key);
num[j] = key;
}
System.out.println("intercambios -> "+ni);
System.out.println("comparaciones-> "+nc);
}
static void inSort(int num[])
{
int key,i;
int nc=0,ni=0;
for(int j=1; j
System.out.println("||||| j="+j+"|||||");
key=num[j];
System.out.println("\tkey "+key+"="+num[j]+"");
i=j-1;
if(((i>=0) && (num[i])>key)==false)
nc++;
while((i>=0) && (num[i])>key)
{
System.out.println("\t\t num["+(i+1)+"]="+num[i]);
num[i+1]=num[i];
i=i-1;
ni++;
nc++;
if(((i>=0) && (num[i])>key)==false && i==-1)
nc++;
}
if(num[i+1]>key)
ni++;
System.out.println("\t"+num[i+1]+"->"+key);
num[i+1]=key;
}
System.out.println("intercambios -> "+ni);
System.out.println("comparaciones-> "+nc);
}
}
No hay comentarios:
Publicar un comentario