Insertion Sort Advanced Analysis

Sort by

recency

|

358 Discussions

|

  • + 0 comments

    Implement a prototype of a UDP network protocol.

    There is a 2d array of size n x 2, requests. At time t = requests[i][0], requests[i][1] packets are to be sent over the network. The network can hold at most max_packets packets in the pipeline. It delivers the data to the client at rate packets per second, i.e. rate packets are removed from the queue and delivered to the client every second.

    If the number of packets exceeds max_packets at any time, the packets remaining at that time are dropped.

    Given the array requests, and the integers, max_packets, and rate, find the total number of packets that are dropped.

    Example

    Suppose requests = [[1, 8], [4, 9], [6, 7]], rate = 2, and max_packets= 10.

  • + 1 comment

    Curious why this solution in Python gets a runtime error for about half of the test cases - I am guessing it's something to do with there being an earlier potential exit point for the loop than simply (s < lenA) or that I shouldn't be using functions like index or min in the solution and instead stick to primitive operations. I'm pretty much a novice, so any advice would help:

    def insertionSort(arr):
        # Write your code here
        moves = 0
        s = 0
        a = arr[:]
        lenA = len(arr) - 1
        while s < lenA:
            p = a.index(min(a))
            a.pop(p)
            moves += p
            s += 1
        return moves
    
  • + 0 comments

    Ruby solution:

    def merge(arr, temp_arr, left, mid, right)
        i = left    # Starting index for left subarray
        j = mid + 1 # Starting index for right subarray
        k = left    # Starting index for sorted subarray
        inv_count = 0
      
        while i <= mid && j <= right
          if arr[i] <= arr[j]
            temp_arr[k] = arr[i]
            i += 1
          else
            temp_arr[k] = arr[j]
            inv_count += (mid - i + 1) # Count inversions
            j += 1
          end
          k += 1
        end
      
        while i <= mid
          temp_arr[k] = arr[i]
          i += 1
          k += 1
        end
      
        while j <= right
          temp_arr[k] = arr[j]
          j += 1
          k += 1
        end
      
        (left..right).each do |idx|
          arr[idx] = temp_arr[idx]
        end
      
        inv_count
      end
      
      def merge_sort(arr, temp_arr, left, right)
        inv_count = 0
        if left < right
          mid = (left + right) / 2
      
          inv_count += merge_sort(arr, temp_arr, left, mid)
          inv_count += merge_sort(arr, temp_arr, mid + 1, right)
      
          inv_count += merge(arr, temp_arr, left, mid, right)
        end
        inv_count
      end
      
      # Wrapper to call merge sort
      def count_inversions(arr)
        temp_arr = Array.new(arr.length)
        merge_sort(arr, temp_arr, 0, arr.length - 1)
      end
    
    
    def insertionSort(arr)
        count = count_inversions(arr)
    end
    
  • + 0 comments

    Just have modify mergesort and count the inversion(shift) .

    def insertionSort(arr):#by using mergesort count = 0 if len(arr) > 1: mid = len(arr)//2 left_list = arr[:mid] right_list = arr[mid:] count = count + insertionSort(left_list) count = count + insertionSort(right_list) count = count + merge(left_list,right_list,arr) return count def merge(left_list,right_list,arr): i=0 j=0 k=0 count = 0 mid = len(arr)//2 while i

  • + 0 comments

    what the hell , if you are getting wrong answer and if you think your code is correct , then the convert the integer to long in pre written code , they said that the range is in integer's range but it isn't.