Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Insertion {
  4.  
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int tc = sc.nextInt();
  8. int[][] o = new int[tc][2];
  9. for(int i=0; i<tc; i++){
  10. o[i][0] = sc.nextInt();
  11. o[i][1] = sc.nextInt();
  12. }
  13.  
  14. array_sort(o);
  15. array_print(o);
  16.  
  17. }
  18.  
  19. private static void array_print(int[][] o) {
  20.  
  21. for(int i=0; i<o.length; i++){
  22. System.out.println(o[i][0]+" "+o[i][1]);
  23. }
  24. }
  25.  
  26. private static void array_sort(int[][] o) {
  27.  
  28. for(int i=1; i<o.length; i++){
  29.  
  30. int[] key = {o[i][0], o[i][1]};
  31. int pos = i;
  32.  
  33. while(pos>0 && (o[pos-1][0]>=key[0])){
  34. // x좌표가 같다. y좌표로 비교한다.
  35. if(o[pos-1][0]==key[0]){
  36. while(pos>0 && o[pos-1][0]==key[0] && o[pos-1][1]>=key[1]){
  37. o[pos][0] = o[pos-1][0];
  38. o[pos][1] = o[pos-1][1];
  39. pos--;
  40. }
  41. }
  42. // x좌표만으로 정렬 가능하다.
  43. else{
  44. o[pos][0] = o[pos-1][0];
  45. o[pos][1] = o[pos-1][1];
  46. pos--;
  47. }
  48. }
  49.  
  50. o[pos][0]=key[0];
  51. o[pos][1]=key[1];
  52. }
  53.  
  54. }
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement