Click here to Skip to main content
15,615,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
import numpy as np
import sys
import timeit
import DSAsorts
import random

REPEATS = 3           #No times to run sorts to get mean time
NEARLY_PERCENT = 0.10 #% of items to move in nearly sorted array
RANDOM_TIMES = 100    #No times to randomly swap elements in array

def usage():
    print(" Usage: java TestHarness n xy [xy ...]")
    print("        where")
    print("        n is number of integers to sort")
    print("        x is one of")
    print("           b - bubblesort")
    print("           i - insertion sort")
    print("           s - selection sort")
    print("           q - quicksort")
    print("           m - mergesort")
    print("        y is one of")
    print("           a - 1..n ascending")
    print("           d - 1..n descending")
    print("           r - 1..n in random order")
    print("           n - 1..n nearly sorted (10% moved)")

def doSort(n, sortType, arrayType):
        A = np.arange(1, n+1, 1)   #create array with values from 1 to n
        if arrayType == 'a':
        elif arrayType =='d':  #convert to descending
            for i in range(0, int(n/2)):
                temp = A[i]
                A[i] = A[n-i-1]
                A[n-i-1] = temp
            print("Descending: ", A)
        elif arrayType == 'r':
            for i in range(RANDOM_TIMES*n):
                x = int(random.random()*n)
                y = int(random.random()*n)
                temp = A[x]
                A[x] = A[y]
                A[y] = temp
            print("Random: ", A)
        elif arrayType == 'n':
            for i in range(int(n*NEARLY_PERCENT/2+1)):
                x = int(random.random()*n)
                y = int(random.random()*n)
                temp = A[x]
                A[x] = A[y]
                A[y] = temp
            print("Nearly sorted: ", A)
            print("Unsupported array type")

        if sortType == "b":
        elif sortType == "s":
        elif sortType == "i":
        elif sortType == "m":
        elif sortType == "q":
            print("Unsupported sort algorithm")

        for i in range(n-2):
            if (A[i] > A[i+1]):
                raise ValueError("Array not in order")

#main program

if len(sys.argv) < 3:
    for aa in range(2, len(sys.argv)):
        n = int(sys.argv[1])
        sortType = sys.argv[aa][0]
        arrayType = sys.argv[aa][1]

        runningTotal = 0

        for repeat in range(REPEATS):
             startTime = timeit.default_timer()
             doSort(n, sortType, arrayType)
             endTime = timeit.default_timer()

             runningTotal += (endTime - startTime)
        print(sortType + arrayType + " " + str(n) + " " + str(runningTotal/(REPEATS - 1)))`

This is the test code that I need to run after importing a second file that the holds the code that I will be testing.S

What I have tried:

<pre>def bubbleSort(A):
    n = len(A)
    for i in range(n):
        swapped = False
        for j in range(0, n-i-1):
            if A[j] > A[j+1]:
                A[j], A[j+1] = A[j+1], A[j]
                swapped = True
        if not swapped:
    return A

def insertionSort(A):
    for i in range(1, len(A)):
        key = A[i]
        j = i - 1
        while j >= 0 and key < A[j]:
            A[j + 1] = A[j]
            j -= 1
        A[j + 1] = key
    return A

def selectionSort(A):
    for i in range(len(A)):
        min_idx = i
        for j in range(i+1, len(A)):
            if A[min_idx] > A[j]:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        # start the next pass after the first element
        i += 1
    return A
def mergeSort(A):
    """ mergeSort - front-end for kick-starting the recursive algorithm

def mergeSortRecurse(A, leftIdx, rightIdx):

def merge(A, leftIdx, midIdx, rightIdx):

def quickSort(A):
    """ quickSort - front-end for kick-starting the recursive algorithm

def quickSortRecurse(A, leftIdx, rightIdx):

def doPartitioning(A, leftIdx, rightIdx, pivotIdx):

This is the code for the sorting methods that I have created, not running the test code does work but the output ends up being:[the output](

Problem is I don't think this is the correct output that should be displayed. Does anyone know what changes I should make?
Updated 23-Jan-23 3:08am

Since we have no idea what your data contains or what output you actually get from the sort, we can't really help you.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. Start here: pdb — The Python Debugger — Python 3.11.1 documentation[^]

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer
I don't think this is the correct output that should be displayed.
That is exactly the output that should be displayed, as shown by the code at:
#main program

if len(sys.argv) < 3:

But since you wrote the code that should be obvious.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900