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
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Sorting
  4. Running Time of Algorithms
  5. Discussions

Running Time of Algorithms

Problem
Submissions
Leaderboard
Discussions

Sort 231 Discussions, By:

recency

Please Login in order to post a comment

  • augustoribeiro_1
    1 week ago+ 0 comments
    public static int runningTime(List<Integer> arr) {
            
            int n = arr.size();
            int swaps = 0;
                
            for(int i = 0; i < n; i++) {
                
                for(int j = 0; j < n - i - 1; j++) {
                    Integer e1 = arr.get(j);
                    Integer e2 = arr.get(j + 1);
                    
                    if(e1 > e2) {
                        
                        //swap
                        arr.set(j, e2);
                        arr.set(j + 1, e1);
                        swaps++;
                    }
                    
                }
            }
            
            return swaps;
        }
    
    0|
    Permalink
  • peddireddysriji1
    1 week ago+ 0 comments

    Just find number of invesions !!!

    0|
    Permalink
  • alex_hong_ronin
    4 weeks ago+ 0 comments

    In case, you are looking for C Solution

    int runningTime(int arr_count, int* arr) {
        int i, j, key, c=0;
        
        for (i=1; i<arr_count; ++i)
        {
            key = arr[i];
            j = i-1;
            
            while(j >= 0 && arr[j] > key)
            {
                arr[j+1]=arr[j];
                j=j-1;
                c++;
            }
            arr[j+1]=key;
        }
        return c;
    }
    
    0|
    Permalink
  • davepaul321
    1 month ago+ 0 comments
    def runningTime(arr):
        # Write your code here
        count=0
        for i in range(len(arr)-1):
            j=i
            while arr[j]>arr[j+1]and j>=0:
                count+=1
                #print(str(i))
                #print(count)
                arr[j+1],arr[j]=arr[j],arr[j+1]
                j-=1
                
        return count
    
    0|
    Permalink
  • lokzy
    1 month ago+ 0 comments

    Python3

    def runningTime(arr):
            shifts = 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]
                            shifts +=1
                            j=j-1
                    arr[j+1]=key
            return shifts
    
    0|
    Permalink
Load more conversations

Need Help?


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