Advertisement
karenaaa

Untitled

Apr 16th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. package tarefa06;
  2.  
  3. public class KVector<Type extends Comparable<Type>> {
  4. Object[] vector;
  5.  
  6. @SuppressWarnings("unchecked")
  7. KVector(){
  8. vector = new Object[2];
  9. }
  10.  
  11.  
  12. public void putSort(Type element) {
  13. boolean find = false;
  14.  
  15. if (vector[0]==null){
  16. vector[0]=element;
  17. return;
  18. }
  19.  
  20. if (element.compareTo((Type)vector[0])<0){
  21. moveAfter(0);
  22. }
  23.  
  24. for (int i = (vector.length/2); find==false; ) {
  25. System.out.println(i);
  26.  
  27. if (element.compareTo((Type)vector[i+1])>0 && element.compareTo((Type)vector[i])<0){
  28. moveAfter(i);
  29. vector[i+1]=element;
  30. find = true;
  31. }
  32. else if ((element.compareTo((Type)vector[i+1]) > 0) && (element.compareTo((Type)vector[i]) > 0)){
  33. i = i + i/2;
  34. }
  35. else {
  36. i/=2;
  37. }
  38. }
  39. }
  40.  
  41. @SuppressWarnings("unchecked")
  42. private void moveAfter(int limit){
  43. Object[] temp = new Object[vector.length];
  44.  
  45. if (vector.length>3){
  46. for(int i=0; i<limit; i++){
  47. temp[i] = vector[i];
  48. }
  49.  
  50. for(int i=limit+1; i<temp.length; i++) {
  51. temp[i+2] = vector[i];
  52. }
  53.  
  54. vector = temp;
  55. }
  56.  
  57. }
  58.  
  59. /* @Override
  60. public String toString() {
  61. String str = "";
  62. for (int i=0; i<vector.length; i++) {
  63. str = "aaaaaa " + (String)vector[i];
  64. }
  65.  
  66. return str;
  67. }
  68.  
  69. public void get (){
  70. for (int i = 0; i<vector.length; i++)
  71. System.out.println(vector[i]);
  72. } */
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement