We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Sorting
  4. Running Time of Algorithms
  5. Discussions

Running Time of Algorithms

Problem
Submissions
Leaderboard
Discussions

Sort 256 Discussions, By:

recency

Please Login in order to post a comment

  • alban_tyrex
    2 months ago+ 0 comments

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

    int runningTime(vector<int> arr) {
        int i,j;
        int value;
        int result = 0;
        for(i=1;i<arr.size();i++)
        {
            value=arr[i];
            j=i-1;
            while(j>=0 && value<arr[j])
            {
                arr[j+1]=arr[j];
                j=j-1;
                result ++;
            }
            arr[j+1]=value;
        }
        return result;
    }
    
    0|
    Permalink
  • TuanBao
    3 months ago+ 0 comments

    This is my Java 8 solution reused the previous challenge method. Feel free to ask me any questions.

    public static int runningTime(List<Integer> arr) {
        int n = arr.size();
        int shifts = 0;
        
        for(int i = 1; i < n; i++) {
            int value = arr.get(i);
            int pos = i - 1;
            
            while(pos >= 0 && value < arr.get(pos)) {
                arr.set(pos + 1, arr.get(pos));
                
                pos--;
                shifts++;
            }
            arr.set(pos + 1, value);
        }
        return shifts;
    }
    
    0|
    Permalink
  • nazalint
    3 months ago+ 0 comments
    def runningTime(arr):
        k = 0
        for i in range(1,  len(arr)):
            for j in range(i):
                if arr[i-j] < arr[i-j-1]:
                    arr[i-j], arr[i-j-1] = arr[i-j-1], arr[i-j]
                    k += 1
        return k
    
    0|
    Permalink
  • voduynhan_123
    3 months ago+ 0 comments

    JavaScript solution

    function runningTime(arr) {
        // Write your code here
        const n = arr.length
        let i = 0
        
        let shift = 0
        
        while (i < n-1) {
            let temp = arr[i+1]
            
            if (temp < arr[i]){
                let  j = 0;
                while (arr[j] <= temp) j++
                
                for (let k = i+1; k > j; k--) {
                    arr[k] = arr[k-1]
                    shift++
                }
                
                arr[j] = temp
            }
            
            i++
        }
    
        return shift;
    }
    
    0|
    Permalink
  • borhan_hkn_41
    3 months ago+ 0 comments

    This challenge specifically mentions about Insertion Sort Algorithm. My first solution was using Bubble sort algorithm which wasn't the requested algorithm in this challenge.

    I suggest you to study for it from following link: -> https://www.geeksforgeeks.org/insertion-sort/

    It might help if you want to understand how this algorithm works a little better.

    This code includes Python3 solution:

    def runningTime(arr):
        # Write your code here
        counter = 0
        for i in range(1, len(arr)):
            key = arr[i]
            j = i - 1
            while j >= 0 and key < arr[j]:
                arr[j + 1] = arr[j]
                j -= 1
                counter += 1 
            arr[j + 1] = key
    
        return counter
    
    0|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy