Advertisement
karenaaa

Untitled

Apr 16th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. package tarefa06;
  2.  
  3. public class KVector<Type extends Comparable<Type>> {
  4. Type[] vector;
  5.  
  6. @SuppressWarnings("unchecked")
  7. KVector(){
  8. vector = (Type[]) new Object[2];
  9. }
  10.  
  11.  
  12. public void putSort(Type element) {
  13. boolean find = false;
  14.  
  15. for (int i = vector.length/2; find==false; ) {
  16. if (element.compareTo(vector[i+1])>0 && element.compareTo(vector[i])<0){
  17. moveAfter(i);
  18. vector[i+1]=element;
  19. find = true;
  20. }
  21. else if ((element.compareTo(vector[i+1]) > 0) && (element.compareTo(vector[i]) > 0)){
  22. i = i + i/2;
  23. }
  24. else {
  25. i/=2;
  26. }
  27. }
  28. }
  29.  
  30. @SuppressWarnings("unchecked")
  31. private void moveAfter(int limit){
  32. Type[] temp = (Type[]) new Object[vector.length+1];
  33.  
  34. for(int i=0; i<limit; i++){
  35. temp[i] = vector[i];
  36. }
  37.  
  38. for(int i=limit+2; i<temp.length; i++) {
  39. temp[i+2] = vector[i];
  40. }
  41.  
  42. vector = temp;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement