Post a Reply
19895 views

Efficient Code Thread

  1. 7 years ago
    Edited 7 years ago by SharpSerac

    i don't know how many coders we got on the forums but i wrote a pretty efficient sorting algorithm that i think is the best out there due to the sheer versatility and usability it has.

    in the code block you'll see it's algorithm then an example, you could copy and paste it into an online python 3 interpreter and it'll work for ya and it'll tell you the run time

    show me your efficient code guys, doesn't have to be sorting but just anything you're really really proud of that you wrote well

    import random
    import time
    
    #only brainlets write inefficient sorting algorithms
    # this sorting algorithm has an efficiency of O(log(n)) prove me wrong
    # >>>protip: you can't
    #            ,+@;                  
    #      .@@@@@@@#                   
    #     .@@@@@@@#                    
    #    `@@@@@@@@@;                   
    #   .@@@@@@@@@@'#                  
    #  ;@@@@@@;:@#`,;;                 
    #  ``+  @@@@: ''+',                
    #    +  @'+#`; :,:@                
    #    ,   +,`` .' ';                
    #    ..  .#  ` ,  `                
    #    :@   :,:      ,               
    #    ,@     `      . +             
    #    .@     :; `  ,  @@@@+`        
    #     #     ` ,#    ,@@@@@@@@'     
    #             @@ .` #@@@@@@@++@    
    #            @@@'  ,@@@@@@+@;#'@   
    #          `@@@@@  :@@@@@@:@@@@':  
    #          @#'@@@' @@@@@@@@@@;@@@  
    #          @#,@@@@@@@@@@@@@@@#@@@: 
    #          @,.@@@@@@@@@@@@@@@@@@@@ 
    #         ,@@:@@@@@@@@@@@@@@@@@@@@ 
    #         @@@+@@@@@@@@@@@@@@@@@@@@.
    #         @@@#@@@@@@@@@@@@@@@@@@@@@
    #   `#    @@@#@@@@@@@@@@@@@@@@@@@@@
    #  ;: ,   @@@@@@@@@@@@@@@@@@@@@@@@@
    #   :   + @@@@@@@@@@@@@@@@@@@@@@@@@
    #     '  `;@@@@@@@@@@@@@@@@@@@@@@@@
    #      ..  '  @@@@@@@@@@@@#'+@@@@@@
    #        +  ,  #@@@@@@@@@@@@@@@@@@@
    #         `.#,  :@';:'@@@@;@'@@@@@@
    #          ,     `@@@:@@@@@#@:@@@@@
    
    start_time = time.time()
    def efficientSort(sort):
        temp = sort
        sortedbool = False
        while not sortedbool:
            print(temp)
            var1 = random.randint(0, len(temp)-1)
            while True:
                var2 = random.randint(0, len(temp)-1)
                if var1 != var2:
                    break
            temp1 = temp[var1]
            temp[var1] = temp[var2]
            temp[var2] = temp1
            sortedbool = True
            for i in range(0, len(temp)-1):
                if temp[i] > temp[i+1]:
                    sortedbool = False
            
    
    sort = [7,6,5,4,3,2,1,0]
    efficientSort(sort)
    print("Runtime: ", time.time() - start_time)
  2. Edited 7 years ago by onionpaste

    @SharpSerac
    Wow, your algorithm is so fast!

    lol, all it does is switch random elements around until it happens upon the sorted array.

    var1 = random.randint(0, len(temp)-1)
            while True:
                var2 = random.randint(0, len(temp)-1)
                if var1 != var2:
                    break

    This part finds two different locations in the array.

    temp1 = temp[var1]
            temp[var1] = temp[var2]
            temp[var2] = temp1

    This code swaps the two elements.

    for i in range(0, len(temp)-1):
                if temp[i] > temp[i+1]:
                    sortedbool = False

    This last bit checks if the array has been sorted, and if so, it stops rearranging elements. If not, it continues finding two random elements and swapping them until the array has been sorted.

    Efficiency at its finest! Nice job.