YChalk

Insertion Sort

May 13th, 2022 (edited)
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12.  
  13. class Result {
  14.  
  15.     /*
  16.      * Complete the 'insertionSort' function below.
  17.      *
  18.      * The function is expected to return an INTEGER.
  19.      * The function accepts INTEGER_ARRAY arr as parameter.
  20.      */
  21.  
  22.     public static int insertionSort(List<Integer> arr) {
  23.     // Write your code here
  24.     int result = 0;
  25.    
  26.     int[] cumulative = new int[arr.size()];
  27.    
  28.     int current;
  29.     int shifts;
  30.     int compare;
  31.    
  32.     for (int i = 1; i < arr.size(); i++) {
  33.         current = arr.get(i);
  34.         shifts = 0;
  35.         for (int j = i-1; j >= 0; j--) {
  36.             compare = arr.get(j);
  37.             if (compare > current) {
  38.                 if (compare == current + 1) {
  39.                     shifts += cumulative[j] + 1;
  40.                     break;
  41.                 }
  42.                 shifts++;
  43.             } else if (compare == current) {
  44.                 shifts += cumulative[j];
  45.                 break;
  46.             }
  47.         }
  48.         cumulative[i] = shifts;
  49.         result += shifts;
  50.     }
  51.    
  52.     //System.out.println(Arrays.toString(cumulative));
  53.     return result;
  54.     int result = 0;
  55.    
  56.     List<Integer> sorted = new ArrayList<>(arr);
  57.     Collections.sort(sorted);
  58.    
  59.     int[] occurrences = new int[sorted.get(sorted.size()-1)+1];
  60.    
  61.     for (Integer i : sorted) {
  62.         occurrences[i]++;
  63.     }
  64.    
  65.     /*LinkedList<Integer> sorted = arr.stream()
  66.                                     .distinct().sorted()
  67.                                     .collect(Collectors.toCollection(LinkedList::new));*/
  68.    
  69.     /*Map<Integer,Integer> occurences =
  70.         sorted.stream().distinct()
  71.         .collect(Collectors.toMap(i -> i, i -> 0));*/
  72.        
  73.     /*Map<Integer,List<Integer>> indexes =
  74.         sorted.stream().distinct()
  75.         .collect(Collectors.toMap(i -> i, i -> new ArrayList<>()));*/
  76.    
  77.     int index = 0;
  78.     int min = nextMin(occurrences, 0);
  79.        
  80.     /*for (Integer i : sorted) {
  81.         indexes.get(i).add(index);
  82.         index++;
  83.     }*/    
  84.        
  85.     /*for (Integer i : arr) {
  86.         compareIndex = indexes.get(i).get(0);
  87.         if (index > compareIndex) {
  88.             result += index - compareIndex;
  89.         }
  90.         index++;
  91.         indexes.get(i).remove(0);
  92.     }*/
  93.    
  94.     /*for (Integer i : arr) {
  95.         compareIndex = indexes.get(i).get(0);
  96.         if (index > compareIndex) {
  97.             result += index - compareIndex;
  98.         } else {
  99.             result -= compareIndex - index;
  100.             try {
  101.                result += arr.subList(index + 1, arr.size())
  102.                             .stream().filter(e -> e < i).count();
  103.             } catch (IndexOutOfBoundsException e) {
  104.                 break;
  105.             }
  106.         }
  107.         index++;
  108.         indexes.get(i).remove(0);
  109.     }*/
  110.    
  111.     for (Integer i : arr) {
  112.         System.out.println(min);
  113.         if (i == min) {
  114.             occurrences[i]--;
  115.             if (occurrences[min] == 0) {
  116.                 min = nextMin(occurrences, min);
  117.             }
  118.             continue;
  119.         }
  120.        
  121.         occurrences[i]--;
  122.         /*if (occurrences[min] == 0) {
  123.             min = nextMin(occurrences, min);
  124.         }*/
  125.        
  126.         try {
  127.             result += arr.stream().skip(index+1).filter(e -> e < i).count();
  128.         } catch (IndexOutOfBoundsException e) {
  129.            
  130.         }
  131.         index++;
  132.     }
  133.    
  134.     /*for (int i = 0; i < arr.size() - 1; i++) {
  135.         final int current = arr.get(i);
  136.         result += arr.stream().skip(i+1).filter(e -> e < current).count();
  137.     }*/
  138.    
  139.     return result;
  140.  
  141.     }
  142.    
  143.     public static int nextMin(int[] occurrences, int start) {
  144.         for (int i = start; i < occurrences.length; i++) {
  145.             if (occurrences[i] > 0) {
  146.                 return i;
  147.             }
  148.         }
  149.         return -1;
  150.     }
  151.  
  152. }
  153.  
  154.  
  155.  
  156. public class Solution {
  157.     public static void main(String[] args) throws IOException {
  158.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  159.         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  160.  
  161.         int t = Integer.parseInt(bufferedReader.readLine().trim());
  162.  
  163.         IntStream.range(0, t).forEach(tItr -> {
  164.             try {
  165.                 int n = Integer.parseInt(bufferedReader.readLine().trim());
  166.  
  167.                 List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  168.                     .map(Integer::parseInt)
  169.                     .collect(toList());
  170.  
  171.                 int result = Result.insertionSort(arr);
  172.  
  173.                 bufferedWriter.write(String.valueOf(result));
  174.                 bufferedWriter.newLine();
  175.             } catch (IOException ex) {
  176.                 throw new RuntimeException(ex);
  177.             }
  178.         });
  179.  
  180.         bufferedReader.close();
  181.         bufferedWriter.close();
  182.     }
  183. }
  184.  
Add Comment
Please, Sign In to add comment