Algorithms | Apple | AssemblyLanguage | BASH | Bash Cookbook | C-Programmming | CommonLisp | Computer Security | CuisSmalltalk | Databases | Emacs | Exploitation | Firewalls | Fortran | Functional Programming | Git | GNUSmalltalk | Haskell | Social Engineering | Javascript | Julia | Linux | Machine Learning | Metasploit |Machine Learning | Networking | Operating Systems | Open Data Science | Odin | Picolisp | Pharo Smalltalk | Purescript | Python | Racket | Scheme | Unity | vim | V | Web Development | Windows WSL | Zig

Sorting

Python Implementation

import argparse
import json

parser = argparse.ArgumentParser()
parser.add_argument("-a", "--array", help="input a python array, do not use spaces")
parser.add_argument("-b", "--bubble", help="use bubble sort", action="store_true")
parser.add_argument("-s","--selection", help="use selection sort", action="store_true")
args = parser.parse_args()

def bubbleSort(arr):
  index = len(arr)-1
  for i in range(index):
    for j in range(0, index-i):
      if arr[j] > arr[j+1]:
        arr[j+1], arr[j] = arr[j], arr[j+1]

def selectionSort(arr):
    arr_length = len(arr)
    for i in range(arr_length):
        min_index = i
        for j in range(i + 1, arr_length):
            if arr[min_index] > arr[j]:
                min_index = j
        arr[min_index], arr[i] = arr[i], arr[min_index]

if __name__ == "__main__":
  arr = [1, 3 ,2, 8, 4, -10, -3, 9, 0]
  if args.array:
    arr = json.loads(args.array)
  print("Input Array:")
  print(arr)
  if args.bubble:
    print("Using Bubble Sort!")
    bubbleSort(arr)
  elif args.selection:
    print("Using Selection Sort")
    selectionSort(arr)
  else:
    print("No sorting algorithm given, using python built-in,")
    arr.sort()
  print("Sorted Array:")
  print(arr)




http:///wiki/?sorting

20jun23   admin