0% found this document useful (0 votes)
46 views

Public Class Public Static Void Int: Package

This document contains code for implementing the shell sort algorithm in Java. It defines a shell class with a main method that creates an array of integers and calls the ordenacionShell method to sort it. The ordenacionShell method implements the shell sort algorithm by starting with a large interval size and iteratively reducing it, sorting sublists at each interval size using an insertion sort approach. It prints output at each step to show the progress of the algorithm.

Uploaded by

Aldrin Pulido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Public Class Public Static Void Int: Package

This document contains code for implementing the shell sort algorithm in Java. It defines a shell class with a main method that creates an array of integers and calls the ordenacionShell method to sort it. The ordenacionShell method implements the shell sort algorithm by starting with a large interval size and iteratively reducing it, sorting sublists at each interval size using an insertion sort approach. It prints output at each step to show the progress of the algorithm.

Uploaded by

Aldrin Pulido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

package U5;

public class shell {


public static void main(String []args) {
int a[]= {6, 5, 2, 3, 4, 0};
shell s = new shell();
s.ordenacionShell(a);
}
public static void ordenacionShell(int a[]){
int intervalo, i, j, k, aux;
int n= a.length;
intervalo = n / 2;
System.out.print("a = ");
for(int x=0; x<a.length;x++) System.out.print(" " + a[x]);
System.out.print("\n");
System.out.println("inter i j k
n a[j] a[k] -> j i aux a[i] a[j]");
System.out.println("- - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -");
System.out.println(intervalo + "\t\t\t\t" + n);
while (intervalo > 0){
for (i = intervalo; i < n; i++){
j = i - intervalo;
System.out.println(intervalo + "\t" + i + "\t" +
j );
while (j >= 0){
k = j + intervalo;
System.out.println(intervalo + "\t\t" + j
+ "\t" + k +"\t\t\t" + a[j] + "\t" + a[k]);
if (a[j] <= a[k]) {
j = -1; // par de elementos
ordenado
System.out.println("\t\t" + j + "
Not change");
}
else{
intercambiar(a, j, j+1);
j -= intervalo;
System.out.println(intervalo);
}
System.out.println("- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
}
System.out.println("- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
}
intervalo = intervalo / 2;
System.out.println(intervalo + " - - - - - - in while");
}
System.out.print("a = ");
for(int x=0; x<a.length;x++) System.out.print(" " + a[x]);
}

public static void intercambiar(int []a, int i, int j){


int aux = a[i];
a[i] = a[j];
a[j]= aux ; // j+1 porque es lo se envia como parametro
System.out.println("\t\t\t\t\t\t\t\t\t"+ i + "\t" + j + "\t" +
aux + "\t" + a[i] + "\t" + a[j]);
}
}

You might also like