Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/jcmouse/public_html/app/code/include/model/database/db.class.php on line 18

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/jcmouse/public_html/app/code/include/model/database/db.class.php on line 18

Deprecated: Function eregi_replace() is deprecated in /home/jcmouse/public_html/app/code/include/model/database/posts.php on line 322

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/jcmouse/public_html/app/code/include/model/database/db.class.php on line 18

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/jcmouse/public_html/app/code/include/model/database/db.class.php on line 18
Metodos de ordenacion del vectores en Java (eclipse) - Code Army!
APUNTES SCHOOL/UNIV | Hace 12 años          

Metodos de ordenacion del vectores en Java (eclipse)


Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/jcmouse/public_html/app/code/include/model/database/db.class.php on line 18

Bueno muchachos, este fue una tarea de progra super facilona, estos son los metodos de ordenamiento de vectores mas conocidos, el mergeSort me esta fallando, si tienen ideas me dices, saludos!

  1. import java.util.Scanner;
  2. public class Tarea3 {
  3. public static void main(String []args){
  4. int n=0,opcion;
  5. int a[]= new int[10];int b[]=new int[10];
  6. Scanner lector = new Scanner(System.in);
  7. do{
  8. System.out.println("Vectores
  9. 1.Lectura
  10. 2.Listado
  11. 3.Bubble sort
  12. 4.Insertion sort.
  13. 5.Merge sort.
  14. 6.Quick sort.7.Seleccion sort.
  15. 8.Shell sort.
  16. 9.Salir.");
  17. opcion= lector.nextInt();
  18. switch(opcion)
  19. {
  20. case 1: do{System.out.println("Total de elementos");
  21. n=lector.nextInt();}while((n>=10)||(n<=0));
  22. a=lectura(n);
  23. break;
  24. case 2: listado(a,n);
  25. break;
  26. case 3: a=bubblesort(a,n);
  27. break;
  28. case 4: a=insertion(a,n);
  29. break;
  30. case 5: mergeSort(a,b,0,(n-1));
  31. break;
  32. case 6: quickSort(a,0,(n-1));
  33. break;
  34. case 7: seleccionDirecta(a);
  35. break;
  36. case 8: shellSort(a);
  37. break;
  38. }
  39. }while(opcion!=9);
  40. }
  41. public static int[] lectura(int x){
  42. Scanner lector = new Scanner(System.in);int b[]= new int[x];int f;
  43. for(int i=0;i<x;i++){
  44. f=i+1;
  45. System.out.println("Ingresando dato ["+f+"]");
  46. b[i]= lector.nextInt();
  47. }
  48. return (b);
  49. }
  50. public static void listado(int[] w,int d)
  51. {
  52. System.out.println("El vector es: ");
  53. for(int x:w){
  54. System.out.print(x+" ");
  55. }
  56. System.out.println();
  57.  
  58. }
  59. public static int[] bubblesort(int[]a,int n){
  60. for(int i=1;i<a.length;i++){
  61. for(int j=0;j<a.length-i;j++){
  62. if(a[j]>a[j+1]){
  63. int aux=a[j];
  64. a[j]=a[j+1];
  65. a[j+1]=aux;
  66. }
  67. }
  68. }
  69. return(a);
  70. }
  71. public static int[] insertion(int[]a,int n){
  72. for(int i=1;i<a.length;i++){
  73. int aux=a[i];
  74. int j=i-1;
  75. while(j>=0&&aux<a[j]){
  76. a[j+1]=a[j];
  77. j--;
  78. }
  79. a[j+1]=aux;
  80. }
  81. return(a);
  82. }
  83. public static void mergeSort(int a[],int tmpArray[],int left,int right){
  84. if(left<right){
  85. int center=(left+right)/2;
  86. mergeSort(a,tmpArray,left,right);
  87. mergeSort(a,tmpArray,center+1,right);
  88. merge(a,tmpArray,left,center+1,right);
  89. }
  90. }
  91. private static void merge(int a[],int tmpArray[],int leftPos,int rightPos,int rightEnd){
  92. int leftEnd=rightPos-1;
  93. int tmpPos=leftPos;
  94. int numElements=rightEnd-leftPos+1;
  95. while(leftPos<=leftEnd&&rightPos<=rightEnd){
  96. if(a[leftPos]<(a[rightPos]))
  97. tmpArray[tmpPos++]=a[leftPos++];
  98. else
  99. tmpArray[tmpPos++]=a[rightPos++];
  100. }
  101. while(leftPos<=leftEnd)
  102. tmpArray[tmpPos++]=a[leftPos++];
  103. while(rightPos<=leftEnd)
  104. tmpArray[tmpPos++]=a[rightPos++];
  105. for(int i=0;i<numElements;i++,rightEnd--)
  106. a[rightEnd]=tmpArray[rightEnd];
  107. }
  108. public static void quickSort(int[]a,int izq,int der){
  109. int i=izq;
  110. int j=der;
  111. int pivote=a[(izq+der)/2];
  112. do{
  113. while(a[i]<pivote)i++;
  114. while(a[j]>pivote)j--;
  115. if(i<=j){
  116. int aux=a[i];
  117. a[i]=a[j];
  118. a[j]=aux;
  119. i++;
  120. j--;
  121. }
  122. }while(i<=j);
  123. if(izq<j)quickSort(a,izq,j);
  124. if(i<der)quickSort(a,i,der);
  125. }
  126. public static void seleccionDirecta(int[]a){
  127. for(int i=0;i<a.length;i++){
  128. int menor=a[i];
  129. int pos=i;
  130. for(int j=i+1;j<a.length;j++){
  131. if(a[j]<menor){
  132. menor=a[j];
  133. pos=j;
  134. }
  135. }
  136. a[pos]=a[i];
  137. a[i]=menor;
  138. }
  139. }
  140. public static void shellSort(int a[]){
  141. for(int gap=a.length/2;gap>0;gap=gap==2?1:(int)(gap/2.2)){
  142. for(int i=gap;i<a.length;i++){
  143. int tmp=a[i];
  144. int j;
  145. for(j=i;j>=gap&&tmp<a[j-gap];j-=gap){
  146. a[j]=a[j-gap];
  147. }
  148. a[j]=tmp;
  149. }
  150. }
  151. }
  152. }

[:D][:D][:D][:D][:D][:D][:D][:D][:D][:D][:D][:D] (Que genial ese emoticon!)

Tags
Fuentes de Información
  • El contenido del post es de mi autoría, y/o, es un recopilación de distintas fuentes.
Dar Puntos
Quitar Puntos
 
15 Puntos
score +15/-0
14951
Visitas
Solución Ex
Apuntes School/Univ
Ordenar digi
Apuntes School/Univ
Comentarios
marcelolobo
Soldado
 
 Bolivia
Pais
 
 10
Puntos
 
 0
Posts
 
 0
Preguntas
 
 0
Respuestas
 
 Hombre
Sexo
Análisis de
Apuntes School/Univ
X

Se parte de Code Army!

Esta acción es solo para usuarios registrados