Insertion Sort - Part 1

Sort by

recency

|

891 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanatoin here : https://youtu.be/xFqneyHR96g

    void printArr(vector<int> arr){
        for(int i = 0; i < arr.size(); i++){cout << arr[i] << " ";}
        cout << endl;
    }
    void insertionSort1(int n, vector<int> arr) {
        int last = arr[arr.size()-1];
        for(int i = arr.size() - 1; i >= 0; i--){
            if(last < arr[i-1]) {
                arr[i] = arr[i-1];
                printArr(arr);
            }
            else{
                arr[i] = last;
                printArr(arr);
                break;
            }
        }
    }
    
  • + 0 comments

    Sorting One common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.

    Insertion Sort These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with a nearly sorted list.

    Insert element into sorted list Given a sorted list with an unsorted number in the rightmost cell, can you write some simple code to insert into the array so that it remains sorted?

    Since this is a learning exercise, it won't be the most efficient way of performing the insertion. It will instead demonstrate the brute-force method in detail.

    Assume you are given the array indexed . Store the value of . Now test lower index values successively from to until you reach a value that is lower than , at in this case. Each time your test fails, copy the value at the lower index to the current index and print your array. When the next lower indexed value is smaller than , insert the stored value at the current index and print the entire array.

    Example

    Start at the rightmost index. Store the value of . Compare this to each element to the left until a smaller value is reached. Here are the results as described:

    1 2 4 5 5 1 2 4 4 5 1 2 3 4 5 Function Description

    Complete the insertionSort1 function in the editor below.

    insertionSort1 has the following parameter(s):

    n: an integer, the size of arr: an array of integers to sort Returns

    None: Print the interim and final arrays, each on a new line. No return value is expected. Input Format

    The first line contains the integer , the size of the array . The next line contains space-separated integers .

    Constraints

    Output Format

    Print the array as a row of space-separated integers each time there is a shift or insertion.

    Sample Input

    5 2 4 6 8 3 Sample Output

    2 4 6 8 8 2 4 6 6 8 2 4 4 6 8 2 3 4 6 8 Explanation

    is removed from the end of the array. In the st line , so is shifted one cell to the right. In the nd line , so is shifted one cell to the right. In the rd line , so is shifted one cell to the right. In the th line , so is placed at position .

    Next Challenge

    In the next Challenge, we will complete the insertion sort.

  • + 0 comments

    My answer in Typescript, simple, not minimized

    function insertionSort1(n: number, arr: number[]): void {
        /**
         * simple as question requested.
         * 
         * + number [i] store index of arr
         * + number [x] store sorting element
         * 
         * index [i] move from end of [arr] to 0, check previous element [i - 1] is 
         * lower than [x] then replace [arr][i] by [x] then stop
         * orelse keep moving [index] replace current [arr][i] by [i - 1]
         */
        let i = arr.length - 1
        let x = arr[i]
    
        while (x != 0) {
            if ((arr[i - 1] ?? 0) < x) {
                arr[i] = x
                x = 0
            } else {
                arr[i] = arr[i - 1]
                i--
            }
            console.log(arr.join(' '))
        }
    }
    
  • + 0 comments

    python3

    def insertionSort1(n, arr):
        # Write your code here
        
        x = arr[-1]
        i = len(arr) - 2
        
        while x < arr[i] and i >= 0:
            arr[i+1] = arr[i]
            print(*arr)
            i = i - 1
            
        if i != len(arr) - 2:
            arr[i+1] = x
        
        print(*arr)
    
  • + 0 comments

    JAVA int key = arr.get(n - 1); int j = n - 2;

        while (j >= 0 && arr.get(j) > key) {
            arr.set(j + 1, arr.get(j)); 
            j--; 
            for (int k = 0; k < arr.size(); k++) {
            System.out.print(arr.get(k) + " ");
        }
        System.out.println();
        }
    
        arr.set(j + 1, key);
    
          for(int i=0;i<arr.size();i++) {
         System.out.print(arr.get(i) + " ");
     }