Insertion Sort - Part 2

Sort by

recency

|

568 Discussions

|

  • + 0 comments

    Do not let the sample explanation confuse you? You don't need any further inspection if the current value is greater than the one preceding. The solution to this problem is same as insertionsort1 except for printing and scope.

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/419S35Kb4Nw

    void insertionSort2(int n, vector<int> arr) {
        for (int i = 1; i < n; i++) {
            int current = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > current) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = current;
            for(int i = 0; i < n; i++) cout << arr[i] << " ";
            cout << endl;
        }
    }
    
  • + 0 comments

    my Java 8 solution:

        public static void insertionSort2(int n, List<Integer> arr) {
            for (int i=1; i < n; i++) {
                for (int j=0; j < i; j++) {
                    if (arr.get(i) < arr.get(j)) {
                        int toInsert = arr.remove(i);
                        arr.add(j, toInsert);
                    }
                }
                StringBuilder sb = new StringBuilder();
                arr.forEach(item -> sb.append(item + " "));
                System.out.println(sb.toString().trim());
            }
        }
    
  • + 0 comments

    My java solution to convert list to arr and print arr

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;

    class Result {

    /*
     * Complete the 'insertionSort2' function below.
     *
     * The function accepts following parameters:
     *  1. INTEGER n
     *  2. INTEGER_ARRAY arr
     */
    
    
    public static void print(int[] arr){
    
    
       for (int i : arr){
    
        System.out.print(i + " ");
       }
            System.out.println("");
    
    }
    
    // {12, 11, 7, 19, 2}
    // {12, 12, 7, 19, 2}
    public static void insertionSort2(int n, int[] arr) {
    // Write your code here
    
        for (int i = 1; i < n; i++){
            int j = i -1;
            int key = arr[i];
    
            while ( j>= 0 && arr[j] > key){
                arr[j + 1] = arr[j];
    
                j--;
            }
    
            arr[j+1] = key;
            Result.print(arr);
        }
    
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
    
        int[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt).mapToInt(Integer::intValue).toArray();
    
        Result.insertionSort2(n, arr);
    
        bufferedReader.close();
    }
    

    }

  • + 0 comments

    def insertionSort2(n, arr):

    for i in range(1, n):
        for j in range(i, 0, -1):
            if arr[j]<arr[j-1]:
                arr[j], arr[j-1] = arr[j-1], arr[j
            else:
                break
        print(*arr)