Sort by

recency

|

948 Discussions

|

  • + 0 comments

    MERGE SORT IS still the fastest algorithm since o(n) complexity

    def merge(left, right):
        merged = []
        i = j = 0
        while i < len(left) and j < len(right):
            if compare_numbers(left[i], right[j]) <= 0:
                merged.append(left[i])
                i += 1
            else:
                merged.append(right[j])
                j += 1
        merged.extend(left[i:])
        merged.extend(right[j:])
        return merged
    def compare_numbers(a, b):
        # First, compare by length.
        if len(a) != len(b):
            return len(a) - len(b)
        if a < b:
            return -1
        elif a > b:
            return 1
        else:
            return 0
            
    def merge_sort(arr):
        if len(arr) <= 1:
            return arr
        mid = len(arr) // 2
        left = merge_sort(arr[:mid])
        right = merge_sort(arr[mid:])
        return merge(left, right)
        
    

    this is insertion sort (0)^2 ` def compare(res, current): if len(res) == 0: res = [current] return res for idx, pip in enumerate(res): if pip >= current: rep = idx if rep<0: rep = 0 res.insert(rep, current) return res res.append(current) print("final", res) return res

    def bigSorting(unsorted): # Write your code here res =[] for datas in unsorted: data = int(datas) res = compare(res,data) fix = [] for bub in res: fix.append(str(bub)) print(bub) return fix `

  • + 0 comments

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

    vector<string> bigSorting(vector<string> u) {
        sort(u.begin(), u.end(), [](string l, string r){
            if(l.size() == r.size()) return l < r;
            return l.size() < r.size();
        });
        return u;
    }
    
  • + 1 comment

    Python3, tried this way

    from functools import cmp_to_key
    
    def compare_big(num1, num2):
        if len(num1) > len(num2):
            return 1
        elif len(num1) < len(num2):
            return -1
        else:
            for d1, d2 in zip(num1, num2):
                if d1 > d2:
                    return 1
                elif d1 < d2:
                    return -1
            return 0
    
    def bigSorting(unsorted):
        return sorted(unsorted, key=cmp_to_key(compare_big))
        # Write your code herefrom functools import cmp_to_key
    
    def compare_big(num1, num2):
        if len(num1) > len(num2):
            return 1
        elif len(num1) < len(num2):
            return -1
        else:
            for d1, d2 in zip(num1, num2):
                if d1 > d2:
                    return 1
                elif d1 < d2:
                    return -1
            return 0
    
    def bigSorting(unsorted):
        return sorted(unsorted, key=cmp_to_key(compare_big))
        # Write your code here
    

    B

  • + 0 comments
    function bigSorting(unsorted) {
      return unsorted.sort((a, b) => {
        if (a.length !== b.length) {
          return a.length - b.length;
        }
    
        for (let i = 0; i < a.length; i++) {
          if (a[i] !== b[i]) return a[i] - b[i];
        }
      });
    }
    
  • + 0 comments
    def bigSorting(unsorted):
        return sorted(unsorted, key=lambda x: (len(x), x))