Insertion Sort - Part 1

Sort by

recency

|

883 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

    My C code 😎😂

    void insertionSort1(int n, int arr_count, int* arr) {
        int cle = arr[n - 1],i = n - 2;
    
        while(cle<arr[i]){
            arr[i + 1] = arr[i];
            i--;
            for(int i = 0;i<n;i++){
                printf("%d ",arr[i]);
            }
            printf("\n");
        }
        arr[i +1] = cle;
        for(int i = 0;i<n;i++){
            printf("%d ",arr[i]);
        }
        printf("\n");
    }
    
  • + 0 comments

    Java easy code:

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class MyClass {
        
        public static void main(String args[]) {
          
            // Initialize Scanner to take input from the user
            Scanner in = new Scanner(System.in);
            
            // Read the size of the array
            int n = in.nextInt();
            int[] arr = new int[n];
            
            // Input all elements of the array
            for(int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            
            // Store the last element of the array (key to be inserted)
            int curr = arr[n-1];
            int i = 0;
            
            // Loop to shift elements to the right if they are greater than 'curr'
            for(i = n - 2; i >= 0; i--) {
                // If the current element is smaller than 'curr', place 'curr' here
                if(arr[i] < curr) {
                    arr[i+1] = curr;
                    break; // Exit the loop after insertion
                } else {
                    // Shift the element to the right
                    arr[i+1] = arr[i];
                }
                
                // Print the array after each shift
                for(int j = 0; j < n; j++) {
                    System.out.print(arr[j] + " ");
                }
                System.out.println();
            }
            
            // If no valid position was found in the loop, place 'curr' at the beginning
            if (i == -1) {
                arr[0] = curr;
            }
            
            // Print the final state of the array
            for(int j = 0; j < n; j++) {
                System.out.print(arr[j] + " ");
            }
        }
    }
    
  • + 0 comments

    Why the below code is not correct .

    Every Custom TC is working fine , but the Problem TCs are not passing

    def insertionSort1(n, arr): # Write your code here j=n-2 temp=arr[n-1] while(j>=0): if(temp<=arr[j]): arr[j+1]=arr[j] print(' '.join(map(str,arr))) j-=1 else: arr[j+1]=temp print(' '.join(map(str,arr))) j-=1

  • + 0 comments
    
    
    def insertionSort1(n, arr):
        # Write your code here
        for i in range(n - 1, 0, -1):
            val = arr[i]
            j = i - 1
            while j >= 0 and val < arr[j]:
                arr[j+1] = arr[j]
                print(*arr)
                j -= 1
            arr[j + 1] = val
        print(*arr)