Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Insertion {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int tc = sc.nextInt();
- int[][] o = new int[tc][2];
- for(int i=0; i<tc; i++){
- o[i][0] = sc.nextInt();
- o[i][1] = sc.nextInt();
- }
- array_sort(o);
- array_print(o);
- }
- private static void array_print(int[][] o) {
- for(int i=0; i<o.length; i++){
- System.out.println(o[i][0]+" "+o[i][1]);
- }
- }
- private static void array_sort(int[][] o) {
- for(int i=1; i<o.length; i++){
- int[] key = {o[i][0], o[i][1]};
- int pos = i;
- while(pos>0 && (o[pos-1][0]>=key[0])){
- // x좌표가 같다. y좌표로 비교한다.
- if(o[pos-1][0]==key[0]){
- while(pos>0 && o[pos-1][0]==key[0] && o[pos-1][1]>=key[1]){
- o[pos][0] = o[pos-1][0];
- o[pos][1] = o[pos-1][1];
- pos--;
- }
- }
- // x좌표만으로 정렬 가능하다.
- else{
- o[pos][0] = o[pos-1][0];
- o[pos][1] = o[pos-1][1];
- pos--;
- }
- }
- o[pos][0]=key[0];
- o[pos][1]=key[1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement